【问题标题】:Sprite-Kit Change Image of Node when screen gets touched当屏幕被触摸时,Sprite-Kit 更改节点的图像
【发布时间】:2014-04-11 13:57:50
【问题描述】:

有一个通过点击屏幕来控制的英雄。我希望每次触摸屏幕时英雄看起来都有些不同。

我所做的是设置两个有点不同的图像。我希望在有触摸事件时改变英雄的形象。

到目前为止,我设置了一个数组来保存信息,但它有点行不通:

 NSMutableArray *heroFrames = [NSMutableArray array];

NSString* textureName = nil;

if (UITouchPhaseBegan) {
    textureName = @"hero1.gif";
}
else  {
    textureName = @"hero2.gif";
}

    SKTexture* texture = [SKTexture textureWithImageNamed:textureName];
    [heroFrames addObject:texture];

[self setHeroFrames:HeroFrames];

self.hero = [SKSpriteNode spriteNodeWithTexture:[_heroFrames objectAtIndex:1]];

运行此程序时出现异常。还有其他想法如何解决我的问题吗?

谢谢大家!

【问题讨论】:

    标签: ios sprite-kit uitouch skspritenode


    【解决方案1】:

    欢迎来到 SO。

    试试这个代码:

    #import "MyScene.h"
    
    @implementation MyScene
    {
        BOOL myBool;
        SKSpriteNode *hero;
        SKTexture *texture1;
        SKTexture *texture2;
    }
    
    -(id)initWithSize:(CGSize)size
    {
        if (self = [super initWithSize:size])
        {
            myBool = false;
            texture1 = [SKTexture textureWithImageNamed:@"hero1.gif"];
            texture2 = [SKTexture textureWithImageNamed:@"hero2.gif"];
            hero = [SKSpriteNode spriteNodeWithTexture:texture1];
            hero.position = CGPointMake(200, 150);
            [self addChild:hero];
        }
        return self;
    }
    
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        if(myBool == true)
        {
            hero.texture = texture1;
            myBool = false;
        } else
        {
            hero.texture = texture2;
            myBool = true;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多