我发现 UIButtons
不能很好地与 SKScene
一起工作,所以我试图继承 SKNode
来制作一个SpriteKit
中的按钮。
我希望它的工作方式是,如果我在 SKScene
中初始化一个按钮并启用触摸事件,那么该按钮将在我的 SKScene
中调用一个方法它被按下。
如果有任何建议可以引导我找到此问题的解决方案,我将不胜感激。谢谢。
最佳答案
您可以使用 SKSpriteNode 作为您的按钮,然后当用户触摸时,检查该节点是否被触摸。使用 SKSpriteNode 的 name 属性来识别节点:
//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;
}
将节点添加到您的场景中:
[self addChild: [self fireButtonNode]];
handle 触摸:
//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...
}
}
https://stackoverflow.com/questions/19082202/
相关文章:
objective-c - 如何在 Swift 项目中使用 Objective-C CocoaPod
objective-c - 基于 View 的 NSTableView 具有动态高度的行
objective-c - Objective-c 中的 "instance variable"和
ios - 自定义 UITableViewCell 选择样式?
iphone - NSLocale currentLocale 总是返回 "en_US"而不是用户的
objective-c - NSString 到 CFStringRef 和 CFStringRef