I am looking to shoot a SKSpritenode (a cannonball) from a another Sprite node (enemy ship). The cannonball should travel directly down toward bottom of screen from the enemy ship node. I can't seem to get positioning right, cannonball seems to shoot from the corner of the screen and not move far, the enemy-ship node is randomly chosen from an array of all halos on screen at once and the cannonball position assigned to the front of halo:
// the cannonball
ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:6];
ball.physicsBody.velocity = CGVectorMake(SHOT_SPEED, SHOT_SPEED);
// we dont want ball to lose speed or momentum when it hots edges so..
ball.physicsBody.restitution=1.0; // max bounceness of node
ball.physicsBody.linearDamping =0.0; // dont reduce linear velocity ove time
ball.physicsBody.friction=0.0;
ball.physicsBody.categoryBitMask = kCCBallCategory;
// ball.physicsBody.contactTestBitMask = kCCEdgeCategory;
ball.physicsBody.collisionBitMask= kCCEdgeCategory;
ball.physicsBody.contactTestBitMask = kCCEdgeCategory | KCCShieldUPCategory | kCCMultiUpCategory ; // | KCCShieldUPCategory notify
// the array of enemy ship nodes
NSMutableArray *allHalos = [NSMutableArray array];
[_mainLayer enumerateChildNodesWithName:@"halos" usingBlock:^(SKNode *node, BOOL *stop) {
[allHalos addObject:node];
}];
if ([allHalos count]>0) {
NSUInteger allHalosInteger = arc4random_uniform([allHalos count]);
SKSpriteNode *shooterHalo=[allHalos objectAtIndex:allHalosInteger];
ball.position = CGPointMake(shooterHalo.position.x, shooterHalo.position.y - shooterHalo.frame.size.height/2 + ball.frame.size.height / 2);
CGPoint bulletDestination = CGPointMake(shooterHalo.position.x, - ball.frame.size.height / 2);
[self fireBullet:ball toDestination:bulletDestination withDuration:2.0 ];
}
-(void)fireBullet:(SKNode*)ball toDestination:(CGPoint)destination withDuration:(NSTimeInterval)duration {
//1
SKAction* bulletAction = [SKAction sequence:@[[SKAction moveTo:destination duration:duration],
[SKAction waitForDuration:3.0/60.0],
[SKAction removeFromParent]]];
[ball runAction:[SKAction group:@[bulletAction, _soundLaser]]];
[self addChild:ball];
}
Any input appreciated.