菜鸟教程小白 发表于 2022-12-11 19:53:56

ios - 当 Action 已经在 SpriteKit 中运行时停止 Action


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

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

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

<pre><code>func activateShield() {

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

    let animateShield = SKAction.sequence([
      SKAction.wait(forDuration: 0, withRange: 0),
      SKAction.animate(with: , timePerFrame: 0.10)])
    let animateRepeatShield = SKAction.repeatForever(animateShield)

    shield = SKSpriteNode(texture: shield1)
    shield.name = &#34;ShieldActive&#34;
    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)
}
</code></pre>

<p>这是我的代码中包含 SKActions 的另一部分:</p>

<pre><code>    let spawnShield = SKAction.run(spawnAShield)
    let waitToSpawnShield = SKAction.wait(forDuration: 37.5, withRange: 15) //Spawn time between 30 - 45 seconds
    let spawnSequenceShield = SKAction.sequence()
    let spawnForeverShield = SKAction.repeatForever(spawnSequenceShield)
    self.run(spawnForeverShield, withKey: &#34;spawningShieldPowerUps&#34;)
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>与 Jean-Baptiste 的回答类似,您也可以运行等待操作并在完成闭包中执行停用。一旦等待时间结束,关闭就会触发。</p>

<blockquote>
<p>edited to show the code in a func that could be called with different durations</p>
</blockquote>

<pre><code>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()
    }
}
</code></pre>

<blockquote>
<p>edit 2</p>
</blockquote>

<pre><code>func activateShield() {

    let shield1 = SKTexture(imageNamed: &#34;shield-1&#34;)
    let shieldImages =

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

    shield = SKSpriteNode(texture: shield1)
    shield.name = &#34;ShieldActive&#34;
    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()
    }
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 当 Action 已经在 SpriteKit 中运行时停止 Action ,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/49412340/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/49412340/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 当 Action 已经在 SpriteKit 中运行时停止 Action