我正在创建一个涉及玩家使用加电的游戏。每当播放器选择激活通电时,都会运行此功能。它将玩家的图像改变为使用加电的玩家的图像,并改变碰撞物理,使玩家现在对敌人免疫。
只要变量powerActivated为1,它就会这样做,但是,正如您所看到的,它会直接返回到0。我需要它延迟5-10秒,然后转到0。这将允许用户在powerUp消失之前使用几秒钟。
func superAbility(){
powerActivated = 1
if powerActivated == 1 {
player.texture = SKTexture(imageNamed: "heroWithPower")
player.physicsBody!.categoryBitMask = PhysicsCategories.PowerUp
player.physicsBody!.collisionBitMask = PhysicsCategories.None
player.physicsBody!.contactTestBitMask = PhysicsCategories.Enemy
// delay should be here
powerActivated = 0
}
else {
player.texture = SKTexture(imageNamed: "hero")
player.physicsBody!.categoryBitMask = PhysicsCategories.Player
player.physicsBody!.collisionBitMask = PhysicsCategories.None
player.physicsBody!.contactTestBitMask = PhysicsCategories.Enemy
}发布于 2019-04-16 00:55:31
使用SKAction来创建延迟,这样你就可以在游戏时间内等待,而不是实时等待,这样任何外部电话操作都不会破坏你的游戏。
func superAbility(){
player.texture = SKTexture(imageNamed: "heroWithPower")
player.physicsBody!.categoryBitMask = PhysicsCategories.PowerUp
player.physicsBody!.collisionBitMask = PhysicsCategories.None
player.physicsBody!.contactTestBitMask = PhysicsCategories.None //I think you meant to set this to none to be immune to enemies
let deactivateAction = SKAction.run{
[unowned self] in
self.player.texture = SKTexture(imageNamed: "hero")
self.player.physicsBody!.categoryBitMask = PhysicsCategories.Player
self.player.physicsBody!.collisionBitMask = PhysicsCategories.None
self.player.physicsBody!.contactTestBitMask = PhysicsCategories.Enemy
}
let wait = SKAction.wait(forDuration:5)
let seq = SKAction.sequence([wait,deactivateAction])
player.run(seq, withKey:"powerup")
}https://stackoverflow.com/questions/55679176
复制相似问题