• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-11 19:53:56 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有一个太空游戏,我现在每天都在扩展,但我遇到了一个我无法解决的问题。现在我为我的宇宙飞船添加了一个 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap