我是一个初学者,慢慢学习 swift 和 Xcode 的功能,到目前为止,我仍然对这样简单的事情有问题:
func addMonster() {
// Create sprite
let monster = SKSpriteNode(imageNamed: "Enemy")
// Determine where to spawn the monster along the Y axis
let actualY = random(min: monster.size.height/2, max: size.height - monster.size.height/2)
// Position the monster slightly off-screen along the right edge,
// and along a random position along the Y axis as calculated above
monster.position = CGPoint(x: size.width + monster.size.width/2, y: actualY)
// Add the monster to the scene
addChild(monster)
// Determine speed of the monster
let actualDuration = random(min: CGFloat(2.0), max: CGFloat(4.0))
// Create the actions
let actionMove = SKAction.moveTo(CGPoint(x: -monster.size.width/2, y: actualY), duration: NSTimeInterval(actualDuration))
let actionMoveDone = SKAction.removeFromParent()
monster.runAction(SKAction.sequence([actionMove, actionMoveDone]))
}
请帮我垂直而不是水平移动我的怪物
主要将"width
"改为"height
",将"height
"改为"width
"。并定义一个actualX
func addMonster() {
// Create sprite
let monster = SKSpriteNode(imageNamed: "Enemy")
// Determine where to spawn the monster along the Y axis
let actualX = random(min: monster.size.width/2, max: size.width - monster.size.width/2)
// Position the monster slightly off-screen along the right edge,
// and along a random position along the Y axis as calculated above
monster.position = CGPoint(x: actualX, y: size.height + monster.size.height/2)
// Add the monster to the scene
addChild(monster)
// Determine speed of the monster
let actualDuration = random(min: CGFloat(2.0), max: CGFloat(4.0))
// Create the actions
let actionMove = SKAction.moveTo(CGPoint(x:actualX, y: -monster.size.height/2), duration: NSTimeInterval(actualDuration))
let actionMoveDone = SKAction.removeFromParent()
monster.runAction(SKAction.sequence([actionMove, actionMoveDone]))
}
保持不变 x 坐标并更改 y 坐标
let actionMove = SKAction.moveTo(CGPoint(x: actualX, y: -monster.size.height/2), duration: NSTimeInterval(actualDuration)
let actionMoveDone = SKAction.removeFromParent() monster.runAction(SKAction.sequence([actionMove, actionMoveDone]))