我在我的场景中添加了几个精灵(SKSpriteNode),如下所示:
let NPuzzle_Texture = SKTexture(imageNamed: "SKTexture.png")
NPuzzle0 = SKSpriteNode(texture: NPuzzle_Texture)
NPuzzle1 = SKSpriteNode(texture: NPuzzle_Texture)
NPuzzle2 = SKSpriteNode(texture: NPuzzle_Texture)
NPuzzle0.position = CGPoint (x: 100, y:125)
NPuzzle0.position = CGPoint (x: 300, y:125)
NPuzzle0.position = CGPoint (x: 500, y:125)
background.addChild(NPuzzle0)
background.addChild(NPuzzle1)
background.addChild(NPuzzle2)
我还在不同的纹理中添加了几个精灵:
let Grey_Back = SKTexture(imageNamed: "Grey_Back.png")
grey_back = SKSpriteNode(texture: Grey_back)
grey_back.position = CGPoint (x: 1024, y:125)
grey_back.alpha = 0.5
background.addChild(grey_back)
现在我将新的精灵(SKSpriteNode)添加到先前的grey_back中,如下所示:
grey_back.addChild(new_sprite1)
grey_back.addChild(new_sprite2)
grey_back.addChild(new_sprite3)
当我试图查看new_sprite的位置是否与NPuzzle精灵相交时,什么也没发生。但是如果将new_sprite添加到场景中:
background.addChild(new_sprite0)
background.addChild(new_sprite1)
background.addChild(new_sprite2)
它有效。事实上,如果精灵已经添加了不同的父母(background和grey_back),它就无法工作。我不明白的是grey_back是背景的孩子,所以它应该有用。为什么不呢?
以下是检查sprite是否与其他人相交的代码示例:
switch selectedNode.name {
case "new_sprite0":
if selectedNode.frame.intersects(NPuzzle0.frame) && (selectedNode.angle == 0) {
谢谢!
答案 0 :(得分:1)
switch selectedNode.name {
let framePoint = CGPointMake(selectedNode.frame.origin.x, selectedNode.frame.origin.y)
let translatedPoint = selectedNode.parent!.convertPoint(framePoint, toNode:NPuzzle0.parent!)
let translatedFrame = CGRectMake(translatedPoint.x, translatedPoint.y, selectedNode.frame.size.width, selectedNode.frame.size.height)
if translatedFrame.intersects(NPuzzle0.frame) && (selectedNode.zRotation == 0)
{
print("ok")
}
显然,因为帧是相对于父项的,所以必须将节点添加到场景中。你可以看到我也使用强制解包为父...