Sprite Kit碰撞检测 [英] Sprite Kit Collision Detection

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

问题描述

您好我正在尝试在我的游戏中设置碰撞检测,我想添加碰撞检测,以便当气球击中它们弹出的尖峰时。我看过Ray Wenderliches教程,但我无法理解,因为它不适用于我的情况。任何想法如何为我的情况设置它?

Hi I am trying to setup collision detection in my game and I want to add collision detection so when the balloons hit the spikes they pop. I have looked at Ray Wenderliches tutorial but I couldn't figure it out because it didn't apply to my case. Any ideas how to set it up for my case?

峰值位于屏幕的顶部,气球在底部产生。

The spikes are at the top of the screen and the balloons spawn at the bottom.

推荐答案

设置2个对象之间的upp冲突的基础是首先设置upp常量,表示可能发生碰撞的不同对象。我通常会创建一个constants.h文件,其中我将保留所有将通过游戏/应用程序使用的变量。

The basics for setting upp collision between 2 objects is to first setting upp constant that represent the different objects that can collide. I usually make a constants.h file where i keep all the variables that will be used thru out the game/application.

在constants.h文件中声明以下内容,或者只是将它们声明为类中的全局变量:

Declare following in either a constants.h file or just declare them as global variables in the class:

static const int balloonHitCategory = 1;
static const int spikeHitCategory = 2;

您现在要做的是为气球和尖峰设置物理

What you want to do now is set up the physics for both your balloon and spikes

SKSpriteNode *ballooon = [SKSpriteNode spriteNodeWithImageNamed:@"yourimagefilename"];
ballooon.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:yourSize];

ballooon.physicsBody.categoryBitMask = balloonHitCategory;
ballooon.physicsBody.contactTestBitMask = spikeHitCategory;
ballooon.physicsBody.collisionBitMask =  spikeHitCategory;

您应该设置尺寸并为两个spritenodes设置图像

you should set your size and set your images for both of the spritenodes

SKSpriteNode *spikes = [SKSpriteNode spriteNodeWithImageNamed:@"yourimagefilename"];
spikes.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(yourSizeX, yourSizeY)];

spikes.physicsBody.categoryBitMask = spikeHitCategory;
spikes.physicsBody.contactTestBitMask = balloonHitCategory;
spikes.physicsBody.collisionBitMask =  balloonHitCategory;

碰撞的

设置以下方法:

for collision set up the following method:

-(void)didBeginContact:(SKPhysicsContact *)contact
{
    SKPhysicsBody *firstBody, *secondBody;

    firstBody = contact.bodyA;
    secondBody = contact.bodyB;

   if(firstBody.categoryBitMask == spikeHitCategory || secondBody.categoryBitMask == spikeHitCategory)
   {

       NSLog(@"balloon hit the spikes");
       //setup your methods and other things here 

   }
}

在碰撞发生之前你还应该添加。在你的场景中.h文件添加。

Before the collision will work you should also add the . In your scenes .h file add .

@interface myScene : SKScene <SKPhysicsContactDelegate>

@end

并在init函数的.m文件中添加:

and in the .m file in the init function add:

-(id)initWithSize:(CGSize)size {    
    if (self = [super initWithSize:size]) {
    self.physicsWorld.contactDelegate = self;

    }
return self;
}

有关碰撞处理的更多信息,请查看Apple文档和冒险游戏示例:
https://developer.apple.com/library/ios/documentation/GraphicsAnimation/Conceptual/CodeExplainedAdventure/HandlingCollisions/HandlingCollisions.html#//apple_ref/doc/uid/TP40013140-CH5-SW1

For more about collision handling check out apple documentation and adventure game example: https://developer.apple.com/library/ios/documentation/GraphicsAnimation/Conceptual/CodeExplainedAdventure/HandlingCollisions/HandlingCollisions.html#//apple_ref/doc/uid/TP40013140-CH5-SW1

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

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