Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
369 views
in Technique[技术] by (71.8m points)

ios - Sprite Kit Collision Detection

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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;

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 

   }
}

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

@interface myScene : SKScene <SKPhysicsContactDelegate>

@end

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;
}

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...