我試圖使用SKSpriteNode作爲從一個場景到下一個場景的過渡。我怎樣才能做到這一點?你如何確定哪個SKSpriteNode被點擊?
[編輯]請注意,這是用於OSX而不是iOS。 iOS的touchesBegan方法在OSX中似乎不起作用。
我試圖使用SKSpriteNode作爲從一個場景到下一個場景的過渡。我怎樣才能做到這一點?你如何確定哪個SKSpriteNode被點擊?
[編輯]請注意,這是用於OSX而不是iOS。 iOS的touchesBegan方法在OSX中似乎不起作用。
好了,下面是OSX的解決方案。
您必須首先初始化場景(自我對象),以便監控點擊。
self.userInteractionEnabled = YES; //do this somewhere in initialization
在mouseDown事件處理程序中,檢查是否已觸摸某個節點(特別是本例中的SKSpriteNode)。
-(void)mouseDown:(NSEvent *)theEvent {
CGPoint location = [theEvent locationInNode:self]; //get location of touch
SKSpriteNode *spriteTouched = (SKSpriteNode*)[self nodeAtPoint:location]; //get a node if touched at that location
//DO SOMETHING WITH THE NODE
...
}
首先您需要爲節點設置一個名稱。 node.name = @"node's name";
然後在的touchesBegan方法補充一點:
for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self];
if([[self nodeAtPoint:location].name isEqualToString:@"node's name"]){
//present scene code here
}
}
我的touchesBegan方法是 - (void)touchesBeganWithEvent:(NSEvent *)事件,我相信是 - (void)touchesBegan的OSX版本:(NSSet *)與事件觸發:(UIEvent *)事件,我認爲這是iOS版本。我見過很多關於後者的網站。有很多關於如何爲iOS做這些事情的信息,但看起來並不像OS X那麼多。 – 02fentym
試試這個,它的工作對我來說
-(void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
for (UITouch *aTouch in touches) {
if (aTouch.tapCount >= 1) {
// The view responds to the tap
// do something here...
}
}
不幸的是,這僅適用於iOS,不適用於OSX。 – 02fentym
我會與精靈套件蘋果文檔開始,它有許多關於如何操作觸摸和精靈例子。 https://developer.apple.com/library/ios/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Introduction/Introduction.html –