检测对等轴测纹理的单击/触摸 [英] Detect click/touch on isometric texture

查看:88
本文介绍了检测对等轴测纹理的单击/触摸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难在一个简单的等距Sprite Kit游戏中实现点击处理.

I am having a hard time trying to implement click handling in a simple isometric Sprite Kit game.

我有一个游戏场景(SKScene),其中的地图(SKNode)包含多个Tile对象(SKSpriteNode).

I have a Game Scene (SKScene) with a Map (SKNode) containing multiple Tile objects (SKSpriteNode).

这是地图的屏幕截图:

我希望能够检测到用户单击的图块,因此我在Tile对象上实现了mouseDown.这是我在Tile.m中的mouseDown:

I want to be able to detect the tile the user clicked on, so I implemented mouseDown on the Tile object. Here is my mouseDown in Tile.m :

-(void)mouseDown:(NSEvent *)theEvent
{
    [self setColorBlendFactor:0.5];
}

该代码似乎可以正常工作,但有一个小故障:节点重叠,并且在节点的透明部分检测到click事件.示例(添加了rect只是为了说明问题.在逻辑中未使用它们):

The code seems to work fine but there is a glitch : the nodes overlap and the click event is detected on the transparent part of the node. Example (the rects have been added to illustrate the problem only. They are not used in the logic) :

如您所见,如果我单击图块7的左上角,则图块8变为透明.

As you can see, if I click on the top left corner of the tile 7, the tile 8 becomes transparent.

我尝试过将所有节点放置在单击位置,并检查单击是否在CGPath内而没有成功(我认为坐标中有问题).

I tried something like getting all the nodes at click location and checking if click is inside a CGPath without success (I think there was something wrong in the coordinates).

因此,我的问题是如何仅检测纹理上的单击而不检测透明部分上的单击?也许我对这个问题的解决方法是错误的?

So my question here is how to detect the click only on the texture and not on the transparent part? Or maybe my approach of the problem is wrong?

任何建议将不胜感激.

Any advice would be appreciated.

对于对我最终使用的解决方案感兴趣的任何人,请参见我的回答在这里

Edit : for anyone interested in the solution I finally used, see my answer here

推荐答案

我对现在"这样的问题的解决方案是:

My solution for such a problem 'right now' is:

  • 在您的场景中,获得位于该点击位置即

  • in your scene get all nodes which are in that position of your click, i.e.

[myScene nodesAtPoint:[theEvent lactionInNode:myScene]]

  • 不要忘记检查您是否未单击场景的根 像这样的东西:

  • don't forget to check if your not clicking the root of your scene something like that:

    if (![[myScene nodeAtPoint:[theEvent locationInNode:myScene]].name isEqual: @"MyScene"])
    

    然后遍历可能节点的数组并检查Texture (NOT myNode.alpha)

    then go through the Array of possible nodes and check the alpha of the Texture (NOT myNode.alpha)

    要获取Alpha,请尝试.

    To get the alpha try this.

    我的看起来像这样:

    -(void)mouseDown:(NSEvent *)theEvent {
         /* Called when a mouse click occurs */
        if (![[self nodeAtPoint:[theEvent locationInNode:self]].name isEqual: self.name]]) {
    
            /* find the node you clicked */
            NSArray *clickedNodes = [self nodesAtPoint:[theEvent locationInNode:self]];
    
            SKNode *clickedNode = [self childNodeWithName:[clickedNodes getClickedCellNode]];
    
            clickedNodes = nil;
    
            /* call the mouseDown method of your Node you clicked to to node specific actions */
            if(clickedNode) {
               [clickedNode mouseDown:theEvent];
            }
            /* kill the pointer to your clicked node */
            clickedNode = nil;
        }
    }
    

    这篇关于检测对等轴测纹理的单击/触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆