![](/img/trans.png)
[英]Swift Sprite-Kit: How do I increase my game's score when screen is touched?
[英]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.