在玩家死后点击重试时,我遇到了一些SKAction问题。这是我遇到困难的射击SKAction。当我运行游戏时,它的拍摄效果很好,但是当我点击“重试”再次播放时,只有一颗子弹发射。之后,SKAction停止。当我再次参加比赛时,所有其他SKActions都有效,但不是那个。
我知道问题是什么。我在一个名为initialize()的函数中有所有相关函数,我在didMove中添加了它。但由于函数有一个参数,我很难调用其中的一些函数。
这些是我的拍摄功能:
func fireMissile() {
let missile = SKSpriteNode(color: .yellow, size: CGSize(width: 20,
height: 5))
missile.name = "Missile"
missile.position = CGPoint(x: player.position.x + 28, y:
player.position.y + 10)
missile.zPosition = 2
missile.physicsBody = SKPhysicsBody(rectangleOf: missile.size)
missile.physicsBody?.isDynamic = false
missile.physicsBody?.categoryBitMask = ColliderType.Bullet
missile.physicsBody?.collisionBitMask = ColliderType.Enemy |
ColliderType.Boat
missile.physicsBody?.contactTestBitMask = ColliderType.Enemy |
ColliderType.Boat
let missileFlightTime = travelTime(to: missileDestination, from:
player.position, atSpeed: missileSpeed)
missile.zRotation = direction(to: missileDestination, from:
missile.position)
self.addChild(missile)
let missileMove = SKAction.move(to: missileDestination,
duration:
TimeInterval(missileFlightTime))
let missileRemove = SKAction.removeFromParent()
missile.run(SKAction.sequence([missileMove, missileRemove]))
}
func travelTime(to target : CGPoint, from : CGPoint, atSpeed speed :
CGFloat) -> TimeInterval {
let distance = sqrt(pow(abs(target.x - from.x),2) +
pow(abs(target.y - from.y),2))
return TimeInterval(distance/speed)
}
func direction(to target : CGPoint, from: CGPoint) -> CGFloat {
let x = target.x - from.x
let y = target.y - from.y
var angle = atan(y / x)
if x < 0 {
angle = angle + CGFloat.pi
}
return angle
}
然后我们有了didMove函数:
func initialize() {
score = 0
physicsWorld.contactDelegate = self
createPlayer()
createBG()
createWall()
spawnEnemiesLeft()
spawnEnemiesRight()
spawnBoat()
fireMissile()
createlabel()
}
当我尝试添加“func travelTime()”和func“direction()”时,我还需要为“目标”和“从”添加CGPoint,我不知道该怎么做键入那里,我不知道那是否还有路要走,但应该是。