我正在制作一个简单的iOS游戏,目标是让天使进入屏幕中间,向四面八方的怪物射箭。但是,当我尝试计算被射击箭头的方向时,Xcode表示“天使”精灵不存在,即使我已经使用过它。这是我的代码:
#import "MyScene.h"
static inline CGPoint rwAdd(CGPoint a, CGPoint b)
{
return CGPointMake(a.x + b.x, a.y + b.y);
}
static inline CGPoint rwSub(CGPoint a, CGPoint b)
{
return CGPointMake(a.x - b.x, a.y - b.y);
}
static inline CGPoint rwMult(CGPoint a, float b)
{
return CGPointMake(a.x * b, a.y * b);
}
static inline float rwLength(CGPoint a)
{
return sqrtf(a.x * a.x + a.y * a.y);
}
static inline CGPoint rwNormalize(CGPoint a)
{
float length = rwLength(a);
return CGPointMake(a.x / length, a.y / length);
}
@implementation MyScene
- (id) initWithSize: (CGSize) size
{
if (self = [super initWithSize:size])
{
self.backgroundColor = [SKColor colorWithRed: 1.0 green: 1.0 blue: 1.0 alpha: 1.0];
SKSpriteNode *angel = [SKSpriteNode spriteNodeWithImageNamed: @"Angel"];
angel.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
angel.xScale = 0.25;
angel.yScale = 0.25;
[self addChild: angel];
}
return self;
}
- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event
{
UITouch * touch = [touches anyObject];
CGPoint location = [touch locationInNode: self];
SKSpriteNode *arrow = [SKSpriteNode spriteNodeWithImageNamed:@"Arrow"];
projectile.position = self.angel.position;
CGPoint offset = rwSub(location, arrow.position);
[self addChild: arrow];
CGPoint direction = rwNormalize(offset);
CGPoint shootAmount = rwMult(direction, 500);
CGPoint realDest = rwAdd(shootAmount, arrow.position);
float velocity = 480.0/1.0;
float realMoveDuration = self.size.width / velocity;
SKAction *actionMove = [SKAction moveTo: realDest duration: realMoveDuration];
SKAction *actionMoveDone = [SKAction removeFromParent];
[arrow runAction: [SKAction sequence: @[actionMove, actionMoveDone]]];
}
@end
当我定义“ arrow.position = self.angel.position”时,Xcode不会将“ angel”识别为精灵。感谢您的任何帮助。
最佳答案
该变量超出范围。您将其设置为子级,因此必须在其他touchesEnded:方法中检索它,您将需要执行以下操作:SKSpriteNode *angel = [self childNodeWithName:@"Angel"];
关于ios - Xcode无法识别已经定义的 Sprite ?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23409799/