I'm trying to create a shield power up for my iOS spritekit game and instead of refreshing the current shield, it stacks another shield on top. My code:
-(void)didMoveToView:(SKView *)view {
...
self.shieldIsActive = NO;
...
}
-(void)didBeginContact:(SKPhysicsContact *)contact{
...
else if (firstBody.categoryBitMask == CollisionCategoryLaser | firstBody.categoryBitMask == CollisionCategoryPlasmaShot |firstBody.categoryBitMask == CollisionCategoryProjectile && secondBody.categoryBitMask == CollisionCategoryShieldIcon){
// Projectile OR Plasma Shot Or Laser hits Shield Icon
ShieldIconNode *shieldIcon = (ShieldIconNode *)secondBody.node;
ShieldNode *shield = [ShieldNode shieldAtPosition:CGPointMake(CGRectGetMidX(self.frame)+4, CGRectGetMidY(self.frame)-210)];
[shieldIcon removeFromParent];
if (firstBody.categoryBitMask == CollisionCategoryProjectile){
ProjectileNode *projectile = (ProjectileNode *)firstBody.node;
[projectile removeFromParent];
}
if (!self.shieldIsActive){
[self addChild:shield];
self.shieldIsActive = YES;
}
// Remove shield after a period of time. BUG: Adds multiple shields if one is already active
if (self.shieldIsActive){
SKAction *wait = [SKAction waitForDuration:ShieldTimer];
[self runAction:wait completion:^{[shield removeFromParent];}];
self.shieldIsActive = NO;
...
}
(Projectile OR Plasma Shot OR Laser AND Shield Icon)
or(Projectile OR Plasma Shot OR Laser) AND Shield Icon
– Dominique Vial