我正在开发精灵套件中的游戏,并且一直使用SKSpriteNode作为按钮。我做了很多研究,似乎开发游戏的“最佳实践”方式是在单个视图控制器上使用多个精灵工具包场景并在SKScenes之间进行转换。
但是,SKSpriteNode没有添加到视图控制器的按钮所具有的按下动画。也就是说,如果我向视图控制器添加一个按钮并将其图像更改为Spaceship.png,当我在Build& Run中单击它时,它会动画(看起来像alpha属性正在变化,可能是0.5左右)。此外,如果按住按钮,则在放开之前不会执行其操作。此外,如果按住它并移开鼠标/手指,它将返回其原始状态(不执行操作,alpha设置回1.0)。
我想知道是否有一种方法可以为SKSpriteNode按钮执行这些操作?我找到了改变纹理/图像的代码(不是我想要完成的代码),并且我没有找到允许SKSpriteNode执行的代码,直到你放开或取消触摸效果(前一段的最后两个句子)。
答案 0 :(得分:2)
感谢上面的Sangony!使用你的逻辑我添加了一点(你有alpha淡入淡出,我添加了代码使其取消事件)我只是想在这里发布我的最终代码以防其他人遇到这个:
@implementation LTMyScene
{
SKSpriteNode *spriteA;
SKLabelNode *labelCounter;
int intCounter;
}
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
/* Setup your scene here */
intCounter=0;
self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];
spriteA = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];//[SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(50, 50)];
spriteA.size=CGSizeMake(100, 100);
spriteA.position = CGPointMake(self.size.width/2,self.size.height/2);
[self addChild:spriteA];
labelCounter = [SKLabelNode labelNodeWithFontNamed:@"Thirteen Pixel Fonts"];
labelCounter.fontSize=12.0;
labelCounter.text=[NSString stringWithFormat:@"Counter is: %i",intCounter];
labelCounter.name = @"HighScoreLabel";
labelCounter.verticalAlignmentMode=SKLabelVerticalAlignmentModeCenter;
labelCounter.position=CGPointMake(self.size.width/2,self.size.height/4);
[self addChild:labelCounter];
}
return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint touchLocation = [[touches anyObject] locationInNode:self];
if([spriteA containsPoint:touchLocation]) {
spriteA.alpha = 0.5;
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint touchLocation = [[touches anyObject] locationInNode:self];
if(spriteA.alpha == 0.5) {
if([spriteA containsPoint:touchLocation]) {
intCounter=intCounter+1;
labelCounter.text=[NSString stringWithFormat:@"Counter is: %i",intCounter];
}
spriteA.alpha = 1.0;
}
}
-(void)update:(CFTimeInterval)currentTime {
/* Called before each frame is rendered */
}
@end
答案 1 :(得分:1)
@implementation MyScene
{
SKSpriteNode *spriteA;
}
-(id)initWithSize:(CGSize)size
{
if (self = [super initWithSize:size])
{
spriteA = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(50, 50)];
spriteA.position = CGPointMake(100, 100);
[self addChild:spriteA];
}
return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint touchLocation = [[touches anyObject] locationInNode:self];
if([spriteA containsPoint:touchLocation])
spriteA.alpha = 0.5;
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if(spriteA.alpha == 0.5)
spriteA.alpha = 1.0;
}