检测等距纹理上的点击/触摸 [英] Detect click/touch on isometric texture

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

问题描述

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

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

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

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

这是地图的截图:

我希望能够检测到用户点击的磁贴,所以我在磁贴对象上实现了 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];
}

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

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?

任何建议将不胜感激.

任何对我最终使用的解决方案感兴趣的人,请参阅我的答案

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 版,请尝试 this.

    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天全站免登陆