Hey people, I'm creating a game in cocos2d, (I'm very new to it, and was trying to solve this thing)
in the game I'm making I created a "Bomb" class, and a "Player" class, I want the bomb to check for collision with the player, if a collision detected, explode.
My problem is that I have no idea how to get the player's position from 开发者_如何学Gothe bomb class, I'd be happy if you guys could help me out here, Thanks!
You did add the CCSprites to a CCLayer, didn't you? Then that CCLayer should have the access to both of them. So, you can use the CCLayer's tick
function to track the positions of the CCSprites and trigger actions if their bounding boxes overlap.
Some sample code to illustrate:
@interface MyLayer : CCLayer {
BombSprite *bomb;
PlayerSprite *player;
}
...
@end
@implementation MyLayer
- (id)init {
if ((self = [super init])) {
bomb = ...
player = ...
[self schedule:@selector(tick:)];
}
return self;
}
- (id)tick:(ccTime)dt {
if (CGRectContainsRect([bomb boundingBox], [player boundingBox])) {
NSLog(@"Collision!");
// call [player didCollideWith:bomb] or something
...
}
}
@end
精彩评论