如何检测sprite kit中的碰撞? [英] How to detect a collision in sprite kit?

查看:135
本文介绍了如何检测sprite kit中的碰撞?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Sprite Kit中制作游戏,我在SpriteNodes,
之间碰撞检测有困难我已经设置了一个称为sprite的sprite节点和一个称为平台的sprite节点。
我想要精灵与平台碰撞时停止下降。
这是我有:

I am making a game in Sprite Kit and I have struggles with the collision detection between SpriteNodes, I've set a sprite node called sprite and a sprite node called platform. I want the sprite to stop falling when collided with the platform. This is what I have:

        SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"bal.png"];
        sprite.position = CGPointMake(self.frame.size.width/4 + arc4random() % ((int)self.frame.size.width/2), (self.frame.size.height/2 + arc4random() % ((int)self.frame.size.height/2)));
        sprite.color = [self randomColor];
        sprite.colorBlendFactor = 1.0;
        sprite.xScale = 0.2;
        sprite.yScale = 0.2;
        [self addChild:sprite];
        sprite.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:sprite.size.width/2];
        self.physicsWorld.gravity = CGVectorMake(0.0f, -4.0f);

        SKSpriteNode *platform = [SKSpriteNode spriteNodeWithImageNamed:@"YellowPlatform.png"];
        platform.position = CGPointMake(CGRectGetMidX(self.frame), -200+CGRectGetMidY(self.frame));
        platform.size = CGSizeMake(180, 10);
        [self addChild:platform];

提前感谢!

推荐答案

从Apple文档中关于 SKNode physicsBody 属性:

From the Apple Documentation about SKNode and the physicsBody property:


默认值为nil,表示节点根本不参与物理模拟。

The default value is nil, which indicates that the node does not participate in the physics simulation at all.

你希望你的球在平台上滚动或你必须设置你的平台的 physicsBody 属性。

If you want your ball to roll on the platform or something you have to set the physicsBody property of your platform.

此外,你必须禁用平台上的动态属性:

Additionally you have to disable the dynamic property on the platform:


一个布尔值,指示物理体是否通过物理模拟

A Boolean value that indicates whether the physics body is moved by the physics simulation.

否则,如果其他物理影响的对象落在它上面,您的平台将会崩溃/移动。

Otherwise your platform would fall down/move if another physics affected object would fall on it.

链接: https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKNode_Ref/Reference/Reference.html#//apple_ref/occ/instp/SKNode/physicsBody
https://developer.apple.com/library/ios/ documentation / SpriteKit / Reference / SKPhysicsBody_Ref / Reference / Reference.html

这篇关于如何检测sprite kit中的碰撞?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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