通过Y移动SKSpriteNode-Swift 2 + SpriteKit [英] Move SKSpriteNode by Y - Swift 2 + SpriteKit
问题描述
我正在开发2D游戏,并且正在使用Swift和SpriteKit.首先,我开始创建动画对象并向下移动.为了创建这些对象",我使用了宇宙飞船(Xcode的资产图片).
I am working on a 2D game, and I'm using the Swift and SpriteKit. To begin, I started creating animated objects and moving to down. To create these "objects", I used the Spaceship (Asset image of Xcode).
查看我的代码:
import SpriteKit
class GameScene: SKScene {
var spaceship = SKSpriteNode()
var screenSize:CGSize!
var gameStarted:Bool = false
var moveAndRemove = SKAction()
override func didMoveToView(view: SKView) {
/* Setup your scene here */
screenSize = self.frame.size
self.createSpaceship()
}
func createSpaceship() -> Void {
spaceship = SKSpriteNode()
spaceship.texture = SKTexture(imageNamed: "Spaceship")
spaceship.size = CGSize(width: 70, height: 100)
spaceship.setScale(1.0)
spaceship.position = CGPoint(x: screenSize.width / 3, y: screenSize.height - 75)
spaceship.zPosition = 1
self.addChild(spaceship)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if gameStarted == false {
gameStarted = true
let spawn = SKAction.runBlock { () in
self.createSpaceship()
}
let delay = SKAction.waitForDuration(1.5)
let spawnDelay = SKAction.sequence([spawn, delay])
let spanDelayForever = SKAction.repeatActionForever(spawnDelay)
self.runAction(spanDelayForever)
let distance = CGFloat(screenSize.height + spaceship.frame.height)
let moveTargets = SKAction.moveToY(spaceship.frame.origin.y - distance, duration: 8.0)
let removeTargets = SKAction.removeFromParent()
moveAndRemove = SKAction.sequence([moveTargets,removeTargets])
}
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}
但是当我运行游戏时,太空飞船仍然停留在展示位置.我找不到它可以解决的问题.
But when I run the game, Spaceship stands still on display. I can not find it can solve.
有人可以帮助我吗?
太空船必须下降(Y),创建副本,然后在Y中下降.
推荐答案
您需要在每个太空飞船上运行moveAndRemove
操作.建议您将动作定义移至createSpaceship
方法,并在创建的每个太空飞船上运行moveAndRemove
动作.例如,
You need to run the moveAndRemove
action on each spaceship. I suggest you move the action definition to the createSpaceship
method and run the moveAndRemove
action on each spaceship that you create. For example,
func createSpaceship() {
// This can be a local variable and combined into a single statement
let spaceship = SKSpriteNode(imageNamed: "Spaceship")
spaceship.size = CGSize(width: 70, height: 100)
spaceship.setScale(1.0)
spaceship.position = CGPoint(x: screenSize.width / 3, y: screenSize.height - 75)
spaceship.zPosition = 1
let distance = CGFloat(screenSize.height + spaceship.frame.height)
let moveTargets = SKAction.moveToY(spaceship.frame.origin.y - distance, duration: 8.0)
let removeTargets = SKAction.removeFromParent()
let moveAndRemove = SKAction.sequence([moveTargets,removeTargets])
// Add the action to the list of actions executed by the spaceship
spaceship.runAction(moveAndRemove)
self.addChild(spaceship)
}
这篇关于通过Y移动SKSpriteNode-Swift 2 + SpriteKit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!