gpt4 book ai didi

ios - 如何在 Sprite 套件游戏的触摸方法中编写 UIbuttons/IBactions

转载 作者:行者123 更新时间:2023-11-29 01:07:34 25 4
gpt4 key购买 nike

在我以前的应用程序(单 View 应用程序)中,我在 IBActions 中使用触摸和向上触摸我的游戏中的按钮。然而,在 SpriteKit 游戏中,您必须完全创建场景,而我很难编写我的 touchesBegan 方法。

这是我的基本运动方法/功能:

func heroMovementLeft () {

hero.position = CGPointMake(hero.position.x - 0.5,hero.position.y);

}

这是我的左箭头节点,我希望在触摸左箭头时调用 HeroMovementLeft 方法

func LeftArrow () {

let LeftArrow = SKSpriteNode(imageNamed: "Left Arrow.png")
LeftArrow.position = CGPointMake(30, 30)
self.addChild(LeftArrow)
}

这是我现在正在为 touchesBegan 方法编写代码的位置

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */

for touch in touches {
}
}

我是否创建 if 语句?我让触摸成为一个变量吗?我需要在 touches 开始方法中写什么,以便在触摸左箭头时我的英雄会移动。我在网上找遍了,找不到这个问题的答案,请帮忙。

最佳答案

你基本上有 2 个选项来识别你的 Sprite

1) 将它们设为场景的全局属性

   class YourScene: SKScene {

var leftArrow: SKSpriteNode!

override func didMoveToView(view: SKView) {

}


func leftArrow() {

leftArrow = SKSpriteNode(imageNamed: "Left Arrow.png")
leftArrow.position = CGPointMake(30, 30)
addChild(leftArrow)
}
}

比开始接触你可以得到这样的位置

  override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */

for touch in touches {
let location = touch.locationInNode(self)
let node = nodeAtPoint(location)

if node == leftArrow {
heroMovementLeft()
}
}
}

选项 2 是给你的 Sprite 命名

 func leftArrow() {
let leftArrow = SKSpriteNode(...)
leftArrow.name = "LeftArrow"
...
}

然后在 touches 开始方法中检查这些名称

  ....
if node.name == "LeftArrow" {
heroMovementLeft()
}

作为旁注,您应该以小写字母开头属性或函数,并且仅以大写字母开头类、结构和协议(protocol)。

关于ios - 如何在 Sprite 套件游戏的触摸方法中编写 UIbuttons/IBactions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36164053/

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