OStack程序员社区-中国程序员成长平台

标题: ios - 当 Action 已经在 SpriteKit 中运行时停止 Action [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:53
标题: ios - 当 Action 已经在 SpriteKit 中运行时停止 Action

我有一个太空游戏,我现在每天都在扩展,但我遇到了一个我无法解决的问题。现在我为我的宇宙飞船添加了一个 PowerUp,它给了它一个盾牌。 Shield PowerUps 产生的持续时间为 37.5,范围为 15。这对我来说是完美的。我多么想弄清楚(或被展示)如何才能使这个盾牌只持续一定的秒数。

现在它会一直持续到被击中,这是失去它的一种方式,但我也想在上面设置一个计时器。

这是我的函数,用于激活与 PowerUp 碰撞时调用的 Shield:

func activateShield() {

    let shield1 = SKTexture(imageNamed: "shield-1")
    let shield2 = SKTexture(imageNamed: "shield-2")
    let shield3 = SKTexture(imageNamed: "shield-3")
    let shield4 = SKTexture(imageNamed: "shield-4")
    let shield5 = SKTexture(imageNamed: "shield-5")
    let shield6 = SKTexture(imageNamed: "shield-6")

    let animateShield = SKAction.sequence([
        SKAction.wait(forDuration: 0, withRange: 0),
        SKAction.animate(with: [shield1,shield2,shield3,shield4,shield5,shield6], timePerFrame: 0.10)])
    let animateRepeatShield = SKAction.repeatForever(animateShield)

    shield = SKSpriteNode(texture: shield1)
    shield.name = "ShieldActive"
    shield.setScale(1.5)
    shield.position = player.position
    shield.zPosition = 3
    shield.physicsBody = SKPhysicsBody(rectangleOf: shield.size)
    shield.physicsBody!.affectedByGravity = false
    shield.physicsBody!.categoryBitMask = PhysicsCategories.ShieldActive
    shield.physicsBody!.collisionBitMask = PhysicsCategories.Enemy
    shield.physicsBody!.contactTestBitMask = PhysicsCategories.Enemy | PhysicsCategories.Life
    shield.physicsBody!.isDynamic = true
    shield.physicsBody!.density = 0
    self.addChild(shield)
    shield.run(animateRepeatShield)

    let joint = SKPhysicsJointFixed.joint(withBodyA: player.physicsBody!, bodyB:shield.physicsBody!, anchor:player.position)
    self.physicsWorld.add(joint)
}

这是我的代码中包含 SKActions 的另一部分:

    let spawnShield = SKAction.run(spawnAShield)
    let waitToSpawnShield = SKAction.wait(forDuration: 37.5, withRange: 15) //Spawn time between 30 - 45 seconds
    let spawnSequenceShield = SKAction.sequence([waitToSpawnShield, spawnShield])
    let spawnForeverShield = SKAction.repeatForever(spawnSequenceShield)
    self.run(spawnForeverShield, withKey: "spawningShieldPowerUps")



Best Answer-推荐答案


与 Jean-Baptiste 的回答类似,您也可以运行等待操作并在完成闭包中执行停用。一旦等待时间结束,关闭就会触发。

edited to show the code in a func that could be called with different durations

var duration: Double = 5 //you can change this to any value. you could even change the value via code for different shields durations
deactivateShield(afterDuation: duration)

func deactivateShield(afterDuation: Double) {
    shield.run(.wait(forDuration: duration)) {
        self.deactivateShield() 
    }
}

edit 2

func activateShield() {

    let shield1 = SKTexture(imageNamed: "shield-1")
    let shieldImages = [shield1, SKTexture(imageNamed: "shield-2"), SKTexture(imageNamed: "shield-3"), SKTexture(imageNamed: "shield-4"), SKTexture(imageNamed: "shield-5"), SKTexture(imageNamed: "shield-6")] 

    let animateShield = SKAction.animate(with: shieldImages, timePerFrame: 0.10)])
    let animateRepeatShield = SKAction.repeatForever(animateShield)

    shield = SKSpriteNode(texture: shield1)
    shield.name = "ShieldActive"
    shield.setScale(1.5)
    shield.position = player.position
    shield.zPosition = 3
    shield.physicsBody = SKPhysicsBody(rectangleOf: shield.size)
    shield.physicsBody!.affectedByGravity = false
    shield.physicsBody!.categoryBitMask = PhysicsCategories.ShieldActive
    shield.physicsBody!.collisionBitMask = PhysicsCategories.Enemy
    shield.physicsBody!.contactTestBitMask = PhysicsCategories.Enemy | PhysicsCategories.Life
    shield.physicsBody!.isDynamic = true
    shield.physicsBody!.density = 0
    self.addChild(shield)
    shield.run(animateRepeatShield)

    let joint = SKPhysicsJointFixed.joint(withBodyA: player.physicsBody!, bodyB:shield.physicsBody!, anchor:player.position)
    self.physicsWorld.add(joint)

    //turns off the shield in between 5-10 seconds
    shield.run(.wait(forDuration: 7.5, withRange: 2.5)) {
        self.shield.removeFromParent() 
    }
}

关于ios - 当 Action 已经在 SpriteKit 中运行时停止 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49412340/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4