gpt4 book ai didi

ios - 不知道如何将触摸作为单个实体处理

转载 作者:可可西里 更新时间:2023-11-01 02:19:10 24 4
gpt4 key购买 nike

我有一个 iPad 游戏,两个不同的玩家可以同时与环境互动。当两个人同时在同一台设备上玩游戏时,我遇到了一个我不知道如何解决的问题。我希望游戏以下一种方式运行:当玩家触摸一个 sprite 并在另一个 sprite 中完成触摸时,应用程序必须能够知道它是同一位玩家做的。

我的应用程序现在执行以下操作:假设 player1 触摸了一个 Sprite 。然后,player2 触碰另一个。他们都没有完成触摸。现在,玩家 1 在第三个 Sprite 中结束了他的触摸。但是,使用我现在拥有的代码,当我需要传递第一个和第三个 Sprite 时,它会用第二个和第三个 Sprite 调用函数“action”,我吓坏了很少,因为我不知道该怎么做。这是您需要的代码:

var globalReference: Int = 0

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch: UITouch! = touches.first as UITouch!
let touchLocation = touch.locationInNode(self)
var spriteTouched: Int? = 0

if self.nodeAtPoint(touchLocation).name != nil {
spriteTouched = Int(self.nodeAtPoint(touchLocation).name!)
globalReference = spriteTouched
}
}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch: UITouch! = touches.first as UITouch!
let touchLocation = touch.locationInNode(self)
var spriteTouched: Int? = 0
if self.nodeAtPoint(touchLocation).name != nil {
spriteTouched = Int(self.nodeAtPoint(touchLocation).name!){
if(globalReference != spriteTouched) {
action1(globalReference, spriteTouched)
} else {
action2(globalReference)
}
}
}
}

我知道触摸了什么 Sprite 的方法是使用“.name”,其中名称始终是 Int。我使用变量globalReference来知道在touchesEnded中touchesBegan中触摸了什么sprite,好吧,这个实现是我真的不知道如何解决。考虑极少数情况,例如当您不触摸已解决的 Sprite 时。如果有人能在这方面帮我一点忙,我将不胜感激……

谢谢!

PS:是的,我知道这是一个很难的问题......只是一个挑战 :)

最佳答案

UITouch 对象是持久的。保留对在 touchesBegan with the touch 中找到的 Sprite 名称的引用。

类属性:

var touchesToSprites = [UITouch:Int]()

开始接触:

touchesToSprites[touch] = spriteTouched

在 touchesEnded 中:

action1( touchesToSprites[touch], spriteTouched )
// remove touchesToSprites[touch] when done

关于ios - 不知道如何将触摸作为单个实体处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32379278/

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