I have created a hero node and enemy node, when they collide,game over.But I have problem with precise collision. Sometimes there is overlap.Sometimes collision happens when one node doesn't reach the other's frame. can anybody give me some advise? appreciate your help~
my hero is affected by gravity, when tap, it will change the direction of gravity.
following is some of my code:
-(void)addRectHero {
rectHero = [SKSpriteNode spriteNodeWithImageNamed:@“hero.png”];
rectHero.scale = 0.2;
rectHero.position = CGPointMake(self.size.width/2, self.size.height*0.3);
rectHero.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:rectHero.size];
rectHero.physicsBody.restitution = 0.0;
rectHero.physicsBody.allowsRotation = NO;
rectHero.physicsBody.dynamic = YES;
rectHero.physicsBody.affectedByGravity=NO;
rectHero.physicsBody.categoryBitMask = heroCategory;
rectHero.physicsBody.contactTestBitMask = enemyCategory;
rectHero.zPosition = 100;
rectHero.physicsBody.collisionBitMask = enemyCategory;
rectHero.physicsBody.usesPreciseCollisionDetection = YES;
[self addChild:rectHero];
}
-(void)createEnemy{
randomStartPoint =(float)((random()%(int)(self.size.height/3))+self.size.height/3);
startPoint = CGPointMake(self.size.width + 50, randomStartPoint);
enemyNode = [SKSpriteNode spriteNodeWithImageNamed:@"enemy_4.png"];
enemyNode.scale =0.5;
enemyNode.position = startPoint;
enemyNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:enemyNode.size];
enemyNode.physicsBody.dynamic = NO;
enemyNode.physicsBody.categoryBitMask = enemyCategory;
enemyNode.physicsBody.contactTestBitMask = heroCategory;
enemyNode.physicsBody.collisionBitMask = heroCategory;
enemyNode.physicsBody.usesPreciseCollisionDetection = YES;
enemyNode.physicsBody.allowsRotation = NO;
enemyNode.physicsBody.restitution = 0.0;
enemyNode.name = @"enemy";
[self addChild:enemyNode];
}
-(void)didBeginContact:(SKPhysicsContact *)contact{
if (!_gameIsOn) {
return;
}
SKPhysicsBody *firstBody, *secondBody;
if (contact.bodyA.categoryBitMask<contact.bodyB.categoryBitMask) {
firstBody = contact.bodyA;
secondBody = contact.bodyB;
}
else
{
firstBody=contact.bodyB;
secondBody=contact.bodyA;
}
if (firstBody.categoryBitMask == heroCategory && secondBody.categoryBitMask == enemyCategory) {
[self runAction:sound_play completion:^{
[self gameOver];
}];
[self stopTimer];
}
}
I create a lot of enemies in update method: (I also tried self repeatActionForever method to create enemy but the problem still exists)
-(void)update:(CFTimeInterval)currentTime {
if (_lastTime)
{
_dt = currentTime - _lastTime;
}
else
{
_dt = 0;
}
_lastTime = currentTime;
if( currentTime - _lastEnemyAdded > 1 && _gameIsOn)
{
_lastEnemyAdded = currentTime + 1;
[self createEnemy];
}
}