每隔x秒如何让产卵的敌舰发射子弹?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了每隔x秒如何让产卵的敌舰发射子弹?相关的知识,希望对你有一定的参考价值。

我正在制作一艘太空船游戏,其中敌舰会产生并向玩家移动。

我希望敌人的太空船每x秒发射一次子弹。我尝试使用在恶棍方法中实现的SKAction,但它不起作用。

我想如果我能够获得敌舰的节点位置,我可以做到但我不能,因为敌舰节点在一个功能中,我不能在功能之外得到它的位置。

这是子弹方法:

func Laser(){

    let LaserBullet = SKSpriteNode(imageNamed: "Laser")

    //Physics World
    LaserBullet.physicsBody = SKPhysicsBody(rectangleOf: LaserBullet.size)
    LaserBullet.physicsBody?.categoryBitMask = NumberingPhysics.Laser
    LaserBullet.physicsBody?.contactTestBitMask = NumberingPhysics.UpperV | NumberingPhysics.YellowUpper | NumberingPhysics.UpperBlue
    LaserBullet.physicsBody?.affectedByGravity = false
    LaserBullet.physicsBody?.isDynamic = true
    LaserBullet.zPosition = 1
    LaserBullet.position = CGPoint(x: SpaceShip.position.x, y: SpaceShip.position.y). // I can put the villains location here if I can get it but I don't know how  

    let LaserThrown = SKAction.moveTo(y: frame.maxY + SpaceShip.size.height, duration: 1)

    let LaserEnded = SKAction.removeFromParent()

    run(LaserSound)
    addChild(LaserBullet)
    LaserBullet.run(SKAction.sequence([LaserThrown,LaserEnded]))
}

这是敌人的方法:

func RedSpawn(){
    let VillainR = Villain()

    VillainR.zPosition = 2

    VillainR.zRotation = CGFloat(Double.pi)

    let min = 0 + VillainR.size.width
    let max = self.frame.width - VillainR.size.width

    let spawn = UInt32(max-min)

    VillainR.position = CGPoint(x: CGFloat(arc4random_uniform(spawn)),y: self.size.height)

    let movement = SKAction.moveTo(y: self.frame.minY + 20, duration: 6.5)
    let remove = SKAction.removeFromParent()

    VillainR.run(SKAction.sequence([movement,remove]))

    //Physics World
    VillainR.physicsBody = SKPhysicsBody(rectangleOf: VillainR.size)
    VillainR.physicsBody?.categoryBitMask = NumberingPhysics.UpperV
    VillainR.physicsBody?.contactTestBitMask = NumberingPhysics.Laser | NumberingPhysics.SpaceShip
    VillainR.physicsBody?.affectedByGravity = false
    VillainR.physicsBody?.isDynamic = true
    VillainR.physicsBody?.collisionBitMask = 0

    addChild(VillainR)
}

这是敌人的阶级(我不认为你们需要这个):

public class Villain: SKSpriteNode {  
    var life = 1   //Number of lives for the red villain
    var hitThisFrame = false    //Not used

   init(){
     let texture = SKTexture(imageNamed: "Villain")
     // print("number of lives: ", life)
     super.init(texture: texture, color: SKColor.clear, size: texture.size())
   }

   required public init?(coder aDecoder: NSCoder) {
      fatalError("init(coder:) has not been implemented")
   }
}

我尝试到处寻找,但我找不到答案。

先感谢您 !!

答案

如果我找到你,你希望每隔X秒执行一次随机的spawnRed。正如我在你的代码中看到的那样,你的spawnRed位置是一个随机值(至少是x位置值)。我建议你使用SKAction,这是我的想法。

你的恶棍功能应该是这样的(设置和创建恶棍是单独的功能):

func villains(){
let VillainR = Villain()

VillainR.zPosition = 2

VillainR.zRotation = CGFloat(Double.pi)

let min = 0 + VillainR.size.width
let max = self.frame.width - VillainR.size.width

let spawn = UInt32(max-min)

VillainR.position = CGPoint(x: CGFloat(arc4random_uniform(spawn)),y: self.size.height)

//Physics World
VillainR.physicsBody = SKPhysicsBody(rectangleOf: VillainR.size)
VillainR.physicsBody?.categoryBitMask = NumberingPhysics.UpperV
VillainR.physicsBody?.contactTestBitMask = NumberingPhysics.Laser | NumberingPhysics.SpaceShip
VillainR.physicsBody?.affectedByGravity = false
VillainR.physicsBody?.isDynamic = true
VillainR.physicsBody?.collisionBitMask = 0

let m = SKAction.moveTo(y: self.frame.minY + 20, duration: 6.5)
let remove = SKAction.removeFromParent()
VillainR.run(SKAction.sequence([movement,remove]))

addChild(VillainR)
}

对于重复的SKAction,我认为你应该将它设置在你的didMove()函数内,或者作为另一个在didMove()函数内执行的函数,我更喜欢将它设置在不同的函数中,因为它的舒适性和美感:

    func spawnReds() {
    let wait = SKAction.wait(forDuration: 3) //the time in seconds between each action
    let action = SKAction.run {
    self.villains() //create a villain
    }
    run(SKAction.repeatForever((SKAction.sequence([wait, action])))) // repeat this action forever = the villains function will executed every 3 seconds, which means that every 3 seconds a new villains will shout.

}

如果你在单独的函数中执行它,你应该调用didMove中的函数,如下所示:

override func didMove(to view: SKView) {
spawnReds()
}

你可以看看apple doc了解更多信息。

以上是关于每隔x秒如何让产卵的敌舰发射子弹?的主要内容,如果未能解决你的问题,请参考以下文章

Cocos2d-x《雷电大战》-子弹无限发射

弹丸/子弹方向 Cocos2d

unity怎么用C#写个发射子弹的脚本?

按住开火按钮时,以一定间隔连续发射子弹

如何使用laravel每隔x秒自动刷新div

Ruby‘s Adventrue游戏制作笔记Unity角色攻击——发射子弹