在spritekit中射击一个射弹

时间:2014-11-20 01:07:46

标签: position sprite-kit skphysicsbody sknode

我希望从另一个Sprite节点(敌舰)射击SKSpritenode(炮弹)。 炮弹应该从敌舰节点直接向屏幕底部下行。 我似乎无法正确定位,炮弹似乎是从屏幕的角落射击而不是向远处移动,敌舰节点是从屏幕上的所有光环阵列中随机选择的,并且分配了炮弹位置在光环的前面:

//炮弹

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

//敌舰船节点阵列

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];
}

任何意见都赞赏。

0 个答案:

没有答案