gpt4 book ai didi

swift - [SpriteKit Swift]尝试生成汽车并以不同的速度将它们沿着屏幕移动

转载 作者:行者123 更新时间:2023-11-30 14:00:11 25 4
gpt4 key购买 nike

所以,我正在为我的 friend 制作一个应用程序,我需要让汽车在道路上的 3 条车道中的 1 条中随机生成,然后以一定的速度向下移动,该速度可以并且会在汽车的生命周期内的多个点发生变化.

let createCarAction = SKAction.runBlock({ //Creates the cars every 4 seconds w/ a waitAction

let laneNum = laneList[Int(arc4random_uniform(3))] //There is an array higher up with the X values of each lane that the cars can use
let newCar: SKSpriteNode
let num = arc4random_uniform(4) //picks random car color
if num == 0 {
newCar = SKSpriteNode(imageNamed: "Audi")
} else if num == 1 {
newCar = SKSpriteNode(imageNamed: "Audi2")
} else if num == 2 {
newCar = SKSpriteNode(imageNamed: "Audi3")
} else {
newCar = SKSpriteNode(imageNamed: "Audi4")
}
newCar.xScale = 0.30
newCar.yScale = 0.30
newCar.position = CGPoint(x: laneNum, y: CGRectGetMaxY(self.frame) + newCar.size.height/2) //puts the car in the picked lane and just above the screen
newCar.zPosition = CGFloat(2)
self.addChild(newCar)
let carAction = SKAction.runBlock({ //this is where it starts going bad. This action is run so many times in the first second the scene starts that it does not switch transition from the scene before it.
if newCar.position.y > (CGRectGetMinY(self.frame)-newCar.size.height) { //if the car is on the screen basically
newCar.position = CGPoint(x: newCar.position.x, y: newCar.position.y-CGFloat(spd))
print("test")
} else { // not on the screen
newCar.removeFromParent()
print("car y: \(CGRectGetMaxY(self.frame) + newCar.size.height/2)")
newCar.removeAllActions() // This does not seem to have any affect as when only one car has been created, carAction is still running seconds after the car has left the screen
}


})
newCar.runAction(SKAction.repeatActionForever(carAction))
self.newCarList.append(newCar)
print("made new car")


})

我的生成工作正常,但是当 newCar.runAction(SKAction.repeatActionForever(carAction)) 发挥作用时,应用程序不会从 MenuScene 移动到 GameScene。我认为汽车会立即移动到底部,然后游戏就会滞后于打印汽车的位置。我在更新功能中有前进的道路。

override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */

if road1.position.y > CGRectGetMinY(self.frame) {
road1.position = CGPoint(x: CGRectGetMidX(self.frame), y: road1.position.y - CGFloat(spd)/2)
road2.position = CGPoint(x: road1.position.x, y: road1.position.y + road1.size.height)

} else {
road1.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
road2.position = CGPoint(x: road1.position.x, y: road1.position.y + road1.size.height)
}
}

我想不出一种不涉及大规模阵列或复杂的 SKAction 来移动汽车的方法,该 SKAction 可以立即移动汽车,而不是与道路保持相同的速度。

提前致谢。

编辑:格式

最佳答案

我最终找到了一种使用 SKAction 的方法。

let carWaitAction = SKAction.waitForDuration(1/60)
let carAction = SKAction.runBlock({

if newCar.position.y > (CGRectGetMinY(self.frame)-newCar.size.height) {
newCar.position = CGPoint(x: newCar.position.x, y: newCar.position.y-CGFloat(spd/3))
} else {
self.score = self.score+5
newCar.removeFromParent()
print("car removed")
newCar.removeAllActions()
print (newCar.hasActions())
}
})
newCar.runAction(SKAction.repeatActionForever(SKAction.group([carWaitAction, carAction])))

关于swift - [SpriteKit Swift]尝试生成汽车并以不同的速度将它们沿着屏幕移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33074123/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com