[英]move Sprite only on x-axis
I am working with Ray Wenderlich's 2D Game Development Book and I make use of a function out of this book to control the movement of a sprite, here called 'passenger'. 我正在与雷·温德利希(Ray Wenderlich)的2D游戏开发手册一起工作,并且利用本书中的功能来控制精灵的移动,此处称为“乘客”。 The example in the book is moving the sprite on ax and y axis, where as I only would need to move the sprite along the x axis, but I can not get it working.
书中的示例是将精灵沿x和y轴移动,因为我只需要沿x轴移动精灵,但我无法使其工作。
Here is 'my' code - well the code I am using 这是“我的”代码-我正在使用的代码
func move(sprite: SKSpriteNode, velocity: CGPoint){
let amountToMove = CGPoint(x: velocity.x * CGFloat(dt),
y: velocity.y * CGFloat(dt))
sprite.position = CGPoint(
x: sprite.position.x + amountToMove.x,
y: sprite.position.y + amountToMove.y)
}
func movePassengerToward(location: CGPoint) {
let offset = CGPoint(x: location.x - passenger.position.x, y: location.y - passenger.position.y)
let length = sqrt(Double(offset.x * offset.x + offset.y * offset.y))
let direction = CGPoint(x: offset.x / CGFloat(length),
y: offset.y / CGFloat(length))
velocity = CGPoint(x: direction.x * passengerMovePointsPerSec,
y: direction.y * passengerMovePointsPerSec)
}
and I trigger these functions from the update loop like this: 我从更新循环触发这些功能,如下所示:
if let lastTaxiLocation = lastTaxiLocation {
let diff = lastTaxiLocation - passenger.position
if diff.length() <= passengerMovePointsPerSec * CGFloat(dt){
passenger.position = lastTaxiLocation
velocity = CGPoint.zero
} else {
move(sprite: passenger, velocity: velocity)
print("update loop")
}
}
Does anyone have an idea for this issue? 有谁对这个问题有想法吗?
尝试仅在move
功能中设置X组件:
sprite.position.x = sprite.position.x + amountToMove.x
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.