我发现UIButtons
与SKScene
不能很好地配合使用,所以我try 将SKNode
子类化为SpriteKit
中的一个按钮.
我希望它的工作方式是,如果我在SKScene
中初始化一个按钮并启用touch 事件,那么当按下它时,该按钮将调用我的SKScene
中的一个方法.
如果有任何建议能帮助我找到解决这个问题的办法,我将不胜感激.谢谢.
我发现UIButtons
与SKScene
不能很好地配合使用,所以我try 将SKNode
子类化为SpriteKit
中的一个按钮.
我希望它的工作方式是,如果我在SKScene
中初始化一个按钮并启用touch 事件,那么当按下它时,该按钮将调用我的SKScene
中的一个方法.
如果有任何建议能帮助我找到解决这个问题的办法,我将不胜感激.谢谢.
您可以使用SKSpriteNode作为按钮,然后当用户touch 时,判断这是否是被touch 的 node .使用SKSpriteNode的Name属性标识 node :
//fire button
- (SKSpriteNode *)fireButtonNode
{
SKSpriteNode *fireNode = [SKSpriteNode spriteNodeWithImageNamed:@"fireButton.png"];
fireNode.position = CGPointMake(fireButtonX,fireButtonY);
fireNode.name = @"fireButtonNode";//how the node is identified later
fireNode.zPosition = 1.0;
return fireNode;
}
将 node 添加到场景中:
[self addChild: [self fireButtonNode]];
把手触碰:
//handle touch events
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
//if fire button touched, bring the rain
if ([node.name isEqualToString:@"fireButtonNode"]) {
//do whatever...
}
}