简体   繁体   English

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

[英]Don't know how to handle touches as single entities

I have a game for iPad where two different players may interact with the environment at the same time.我有一个 iPad 游戏,其中两个不同的玩家可以同时与环境互动。 When the game is played by two people in the same device at once I have a problem that I don't know how to solve.当两个人在同一设备上同时玩游戏时,我遇到了一个我不知道如何解决的问题。 I want the game to behave in the next way: when a player touches a sprite, and finishes the touch in another sprite the app must be able to know that it has been the same player who did it.我希望游戏以下一种方式运行:当玩家触摸一个精灵,并在另一个精灵中完成触摸时,应用程序必须能够知道它是同一个玩家做的。

What my app does now is the following: suppose that player1 touches a sprite.我的应用程序现在所做的如下:假设 player1 触摸一个精灵。 Then, player2 touches another.然后,player2 接触另一个。 Neither of them finishes the touch.他们都没有完成接触。 Now, player1 ends his touch in a third sprite.现在,玩家 1 在第三个精灵中结束他的触摸。 But, with the code that I have right now, what it would do is to invoke the function "action" with the second and the third sprite, when I need to pass the first and the third sprite, and I'm freaking out a little because I don't know how to do it.但是,使用我现在拥有的代码,当我需要传递第一个和第三个精灵时,它会用第二个和第三个精灵调用函数“动作”,我吓坏了很少,因为我不知道该怎么做。 Here's the code you need:这是您需要的代码:

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)
            }
        }
    }
}

The way I get to know what sprite is touched is using ".name" where the name is always an Int.我了解接触什么精灵的方法是使用“.name”,其中名称始终为 Int。 I use the variable globalReference to know what sprite was touched in touchesBegan in touchesEnded, and well, this implementation is what I really don't know how to solve.我用变量globalReference知道touchesBegan在touchesEnded中触动了什么sprite,嗯,这个实现就是我真的不知道怎么解决。 Consider the rare cases like when you don't touch a sprite solved.考虑一些罕见的情况,例如您不触摸已解决的精灵。 I would be grateful if someone could help me a little on this…如果有人能在这方面帮助我一点,我将不胜感激……

Thank you!谢谢!

PS: Yes, I know it is a difficult question… Just a challenge : ) PS:是的,我知道这是一个很难的问题......只是一个挑战:)

UITouch objects are persistent. UITouch 对象是持久的。 Keep a reference to the name of the sprite found in touchesBegan with the touch.保留对 touchesBegan with the touch 中找到的精灵名称的引用。

class property:类属性:

var touchesToSprites = [UITouch:Int]()

in touchesBegan:在接触开始:

touchesToSprites[touch] = spriteTouched

in touchesEnded:在接触结束:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM