我很难在一个简单的等距 Sprite Kit 游戏中实现点击处理。
我有一个包含多个 Tile 对象(SKSpriteNode)的 map (SKNode)的游戏场景(SKScene)。
这是 map 的屏幕截图:
我希望能够检测到用户点击的磁贴,所以我在 Tile 对象上实现了 mouseDown。这是我在 Tile.m 中的 mouseDown :
-(void)mouseDown:(NSEvent *)theEvent
{
[self setColorBlendFactor:0.5];
}
该代码似乎工作正常,但有一个小故障:节点重叠,并且在节点的透明部分检测到单击事件。示例(已添加 rects 仅用于说明问题。逻辑中未使用它们):
如您所见,如果我单击图块 7 的左上角,图块 8 将变为透明。
我尝试了一些方法,比如在点击位置获取所有节点并检查点击是否在 CGPath 内但没有成功(我认为坐标有问题)。
所以我的问题是如何只检测纹理而不是透明部分的点击?或者我对问题的处理方法是错误的?
任何意见,将不胜感激。
编辑:对于对我最终使用的解决方案感兴趣的任何人,请参阅 my answer here
最佳答案
我对“现在”此类问题的解决方案是:
[myScene nodesAtPoint:[theEvent lactionInNode:myScene]]
类似的东西:
if (![[myScene nodeAtPoint:[theEvent locationInNode:myScene]].name isEqual: @"MyScene"])
然后遍历可能的节点数组并检查
Texture (NOT myNode.alpha)
的 alpha 要获得 alpha 尝试 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;
}
}
关于sprite-kit - 检测等距纹理上的点击/触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22578564/