1

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.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
dancingbush
  • 2,131
  • 5
  • 30
  • 66
  • Default for `physicsBody.affectedByGravity` is always set to `YES`. Did you make sure your ball is not affected by gravity? – Andriko13 Nov 20 '14 at 03:55
  • Let me get this straight, you're using SKActions on a ball that has a velocity of shot speed? Are you sure these two aren't interfering? – Andriko13 Nov 20 '14 at 03:57
  • Hi, The gravity is set to zero : self.physicsWorld.gravity = CGVectorMake(0,0); tried removing the velocity setting but same outcome. – dancingbush Nov 20 '14 at 05:29
  • You can't use nodes with physics bodies via actions and expect them to behave like physics objects. Apply physics force/impulse to the body and let that do its job. – CodeSmile Nov 20 '14 at 10:42
  • Do u mean use apply force as opposed to moveTo ? – dancingbush Nov 20 '14 at 13:10
  • Have tried: [ball.physicsBody applyForce:CGVectorMake(0, 40)]; SKAction *action = [SKAction moveToX:_cannon.position.x duration:4]; SKAction *remove = [SKAction removeFromParent]; SKAction *delay = [SKAction waitForDuration:2]; [ball runAction:[SKAction sequence:@[ action, remove]]]; [_mainLayer addChild:ball]; Ball just drops straight to bottom of screen, then will slowly move along the x axis. Effect I am looking for is the ball to move in the direction of the x cordinate on the x axis (target) as it falls, not move to x axis then along the axis. – dancingbush Nov 20 '14 at 13:25

0 Answers0