ios - Xcode 无法识别已经定义的 Sprite ?

标签 ios xcode sprite-kit

我正在制作一个简单的 iOS 游戏,目标是让屏幕中间的天使向从四面八方袭来的怪物射箭。但是当我尝试计算箭头的射出方向时,Xcode 说“天使” Sprite 不存在,即使我已经使用过它。这是我的代码:

#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”识别为 Sprite 。非常感谢任何帮助,谢谢。

最佳答案

变量超出范围。您将其设置为 child ,因此您必须在其他 touchesEnded: 方法中检索它,您需要执行以下操作:

SKSpriteNode *angel = [self childNodeWithName:@"Angel"];

关于ios - Xcode 无法识别已经定义的 Sprite ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23409799/

相关文章:

ios - 在 Init 中调用函数设置变量

ios - 如何使用 XCode 在 iOS 应用程序中的推文中上传视频

iphone - 使用 UIApplication sharedApplication 调用电话

ios - 仅在特定场景中显示 adMob 横幅; swift

ios - 在 Swift 3 中,如何在转换到另一个 SKView 时传递变量?

iOS7 "Slide to unlock"UILabel 上的动画

ios - 合并 UIImage 并将其包裹在杯子上

ios - 使用 xcrun 打包时的怪异(使用变量时代码设计失败)

ios - 关闭和打开应用程序时打开最后打开的 View Controller

swift - GameScene.sks 文件未被使用