[英]Unity3D: C# - Can't seem to handle single/double/ hold touches correctly
[英]Don't know how to handle touches as single entities
我有一個 iPad 游戲,其中兩個不同的玩家可以同時與環境互動。 當兩個人在同一設備上同時玩游戲時,我遇到了一個我不知道如何解決的問題。 我希望游戲以下一種方式運行:當玩家觸摸一個精靈,並在另一個精靈中完成觸摸時,應用程序必須能夠知道它是同一個玩家做的。
我的應用程序現在所做的如下:假設 player1 觸摸一個精靈。 然后,player2 接觸另一個。 他們都沒有完成接觸。 現在,玩家 1 在第三個精靈中結束他的觸摸。 但是,使用我現在擁有的代碼,當我需要傳遞第一個和第三個精靈時,它會用第二個和第三個精靈調用函數“動作”,我嚇壞了很少,因為我不知道該怎么做。 這是您需要的代碼:
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)
}
}
}
}
我了解接觸什么精靈的方法是使用“.name”,其中名稱始終為 Int。 我用變量globalReference知道touchesBegan在touchesEnded中觸動了什么sprite,嗯,這個實現就是我真的不知道怎么解決。 考慮一些罕見的情況,例如您不觸摸已解決的精靈。 如果有人能在這方面幫助我一點,我將不勝感激……
謝謝!
PS:是的,我知道這是一個很難的問題......只是一個挑戰:)
UITouch 對象是持久的。 保留對 touchesBegan with the touch 中找到的精靈名稱的引用。
類屬性:
var touchesToSprites = [UITouch:Int]()
在接觸開始:
touchesToSprites[touch] = spriteTouched
在接觸結束:
action1( touchesToSprites[touch], spriteTouched )
// remove touchesToSprites[touch] when done
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.