繁体   English   中英

Sprite-Kit在触摸屏幕时更改节点的图像

[英]Sprite-Kit Change Image of Node when screen gets touched

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

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

到目前为止,我设置了一个数组来保存信息,但它有点无法解决:

 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]];

我在运行时遇到异常..还有其他想法如何实现我的问题?

多谢你们!

欢迎来到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;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM