简体   繁体   English

如何检测 SKSpriteNode 是否在 SKShapeNode 中

[英]How to Detect if a SKSpriteNode is in a SKShapeNode

I put a circular attack oval on a blue space ship and, if any SKSpriteNode enters the SKShapeNode (attack oval) the blue space ship can fire on the SKSpriteNode .我在蓝色宇宙飞船上放置了一个圆形攻击椭圆形,如果任何SKSpriteNode进入SKShapeNode (攻击椭圆形),蓝色飞船可以向SKSpriteNode The problem is that I can't figure out how to do the detection.问题是我不知道如何进行检测。 I have tried physical bodies but, I can't have contact without having a collision.我已经尝试过物理身体,但是,如果没有碰撞,我就无法接触。

Here is an example.这是一个例子。 I want the purple/blue attack circle to detect A,B,C,D,E我想要紫色/蓝色攻击圈检测 A、B、C、D、E

我希望紫色圆圈检测 A、B、C、D、E

Here is the code I think you will need.这是我认为您需要的代码。 RSSprite is short for red ship sprite RSSprite 是红色船精灵的缩写

blueShip.position = CGPoint(x: 800, y: 400)

attackCircle = SKShapeNode(ellipseOfSize: CGSize(width: 1000, height: 400))
attackCircle.position = CGPoint(x: blueShip.position.x, y: blueShip.position.y)

RSSprite!.position = CGPoint(x: 200, y: 700)
RSSprite!.physicsBody = SKPhysicsBody(rectangleOfSize: RSSprite!.size)

In the update method you can check for an intersection of 2 nodes like this:在更新方法中,您可以像这样检查2 个节点交集

if([nodeA intersectsNode:nodeB]) {
    // you have a collision
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM