gpt4 book ai didi

ios - 在 SKScene 中设置按钮

转载 作者:IT老高 更新时间:2023-10-28 11:23:18 24 4
gpt4 key购买 nike

我发现 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...
}
}

关于ios - 在 SKScene 中设置按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19082202/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com