为什么这个功能在我添加点击框后立即停止工作?
Why this function stop working as soon as I add hit boxes?
我正在尝试创建一个汽车游戏,您可以通过触摸屏幕将汽车从左移到右,但是一旦我设置 "physics definition -> body type" 并且汽车到达最左边或最右边屏幕上,此运动功能停止工作。
我正在使用
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches{
let touchLocation = touch.location(in: self)
if touchLocation.x > centrePoint {
if playerCar.position.x == playerCarAtMaxLeft {
playerCar.position.x = playerCarAtLeft
playerCarMoveRight = true
playerCarMoveLeft = false
} else if playerCar.position.x == playerCarAtLeft {
playerCar.position.x = playerCarAtRight
playerCarMoveRight = true
playerCarMoveLeft = false
} else if playerCar.position.x == playerCarAtRight {
playerCar.position.x = playerCarAtMaxRight
playerCarMoveRight = true
playerCarMoveLeft = false
} else {
playerCarMoveRight = false
playerCarMoveLeft = true
}
} else {
if playerCar.position.x == playerCarAtMaxRight {
playerCar.position.x = playerCarAtRight
playerCarMoveRight = false
playerCarMoveLeft = true
} else if playerCar.position.x == playerCarAtRight {
playerCar.position.x = playerCarAtLeft
playerCarMoveRight = false
playerCarMoveLeft = true
} else if playerCar.position.x == playerCarAtLeft {
playerCar.position.x = playerCarAtMaxLeft
playerCarMoveRight = false
playerCarMoveLeft = true
} else{
playerCarMoveRight = true
playerCarMoveLeft = false
}
}
canMove = true
}
}
playerCar 是一个 SKSpriteNode
playerCarAt...是CGFloat
playerCarMove... 是布尔值
我建议你让汽车在 x 轴上尽可能多地滑动,然后使用 update
方法来跟踪汽车的位置。
override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
}
这样你就可以设置 let maxXPosition
一旦汽车节点到达那个点,你就停止运动。
这样做的一种方法是使汽车节点移动SKAction.moveTo
。
一个简单的版本是:
class GameScene: SKScene {
var car : SKSpriteNode! // Your car sprite
var maxXPosition : CGFloat! // The max point on the x-axis the car is allowed to move to
override func didMove(to view: SKView) {
// Initiate car
car = self.childNode(withName: "//car") as? SKSpriteNode
// Setup the max x position
maxXPosition = 200
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
moveCar(toPosition: location)
}
}
override func update(_ currentTime: TimeInterval) {
// Check the cars position
if car.position.x >= 200 {
// Stop the car is the point has been reached
car.removeAction(forKey: "carDrive")
}
}
func moveCar(toPosition position: CGPoint) {
let moveCarToXPoint = SKAction.moveTo(x: position.x, duration: 1)
car.run(moveCarToXPoint, withKey: "carDrive")
}
}
我正在尝试创建一个汽车游戏,您可以通过触摸屏幕将汽车从左移到右,但是一旦我设置 "physics definition -> body type" 并且汽车到达最左边或最右边屏幕上,此运动功能停止工作。 我正在使用
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches{
let touchLocation = touch.location(in: self)
if touchLocation.x > centrePoint {
if playerCar.position.x == playerCarAtMaxLeft {
playerCar.position.x = playerCarAtLeft
playerCarMoveRight = true
playerCarMoveLeft = false
} else if playerCar.position.x == playerCarAtLeft {
playerCar.position.x = playerCarAtRight
playerCarMoveRight = true
playerCarMoveLeft = false
} else if playerCar.position.x == playerCarAtRight {
playerCar.position.x = playerCarAtMaxRight
playerCarMoveRight = true
playerCarMoveLeft = false
} else {
playerCarMoveRight = false
playerCarMoveLeft = true
}
} else {
if playerCar.position.x == playerCarAtMaxRight {
playerCar.position.x = playerCarAtRight
playerCarMoveRight = false
playerCarMoveLeft = true
} else if playerCar.position.x == playerCarAtRight {
playerCar.position.x = playerCarAtLeft
playerCarMoveRight = false
playerCarMoveLeft = true
} else if playerCar.position.x == playerCarAtLeft {
playerCar.position.x = playerCarAtMaxLeft
playerCarMoveRight = false
playerCarMoveLeft = true
} else{
playerCarMoveRight = true
playerCarMoveLeft = false
}
}
canMove = true
}
}
playerCar 是一个 SKSpriteNode playerCarAt...是CGFloat playerCarMove... 是布尔值
我建议你让汽车在 x 轴上尽可能多地滑动,然后使用 update
方法来跟踪汽车的位置。
override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
}
这样你就可以设置 let maxXPosition
一旦汽车节点到达那个点,你就停止运动。
这样做的一种方法是使汽车节点移动SKAction.moveTo
。
一个简单的版本是:
class GameScene: SKScene {
var car : SKSpriteNode! // Your car sprite
var maxXPosition : CGFloat! // The max point on the x-axis the car is allowed to move to
override func didMove(to view: SKView) {
// Initiate car
car = self.childNode(withName: "//car") as? SKSpriteNode
// Setup the max x position
maxXPosition = 200
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
moveCar(toPosition: location)
}
}
override func update(_ currentTime: TimeInterval) {
// Check the cars position
if car.position.x >= 200 {
// Stop the car is the point has been reached
car.removeAction(forKey: "carDrive")
}
}
func moveCar(toPosition position: CGPoint) {
let moveCarToXPoint = SKAction.moveTo(x: position.x, duration: 1)
car.run(moveCarToXPoint, withKey: "carDrive")
}
}