我想让我的炮塔雪碧向怪物射击。我已经让炮塔转向怪物并跟随它直到它超出范围,现在我只需要进行射击。
从炮塔向怪物射击射弹的最佳方法是什么?
我已经完成了这一部分:
-(void)shoot
{
SKSpriteNode *bullet = [SKSpriteNode spriteNodeWithImageNamed:@"CannonMissile-hd.png"]; ... i don't know what to do next
}
另外,我需要它以x秒的间隔拍摄,
感谢
答案 0 :(得分:4)
-(void)shoot
{
SKSpriteNode *turretNode;//I assume you have this node already in the scene . Dont use this line
SKSpriteNode *enemy;//I assume you have this node already in the scene . Dont use this line
SKSpriteNode *bullet = [SKSpriteNode spriteNodeWithImageNamed:@"CannonMissile-hd.png"];
bullet.zPosition = turretNode.zPosition -1;//if you want your bullet not to spawn on top of your turret
[turretNode addChild:bullet];
//you need to set the physics body of the bullet so you can detect contacts
SKAction *move = [SKAction moveTo:enemy.position duration:0.5];//if u have multiple enemies then you have to deceide which one to hit
[bullet runAction:move completion:^{
[bullet removeFromParent];//removes the bullet when it reaches target
//you can add more code here or in the didBeginContact method
}];
//repeat the process
[self performSelector:@selector(shoot) withObject:nil afterDelay:5];//replace 5 with ur x seconds
//that's it
}