【问题标题】:How do you determine which SKSpriteNode was clicked?您如何确定单击了哪个 SKSpriteNode?
【发布时间】:2014-06-29 03:52:37
【问题描述】:

我正在尝试使用 SKSpriteNode 作为从一个场景到下一个场景的过渡。我该怎么做?

[编辑] 请注意,这是针对 OSX 而不是 iOS。 iOS 的 touchesBegan 方法似乎在 OSX 中不起作用。

【问题讨论】:

标签: objective-c sprite-kit osx-mavericks skspritenode


【解决方案1】:

好的,这是 OSX 的解决方案。

您必须首先初始化场景(self 对象),以便它监视点击。

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

【讨论】:

    【解决方案2】:

    首先,您需要为节点设置一个名称。 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 *)event,我相信它是 OSX 版本的 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event,我认为是iOS版本。我见过很多谈论后者的网站。有很多关于如何为 iOS 做所有这些事情的信息,但对于 OS X 似乎并不多。
    【解决方案3】:

    试试这个,它对我有用

        -(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。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-15
    • 2014-02-04
    • 1970-01-01
    • 1970-01-01
    • 2012-01-11
    • 2011-03-18
    相关资源
    最近更新 更多