gpt4 book ai didi

swift - 按 Y 移动 SKSpriteNode - Swift 2 + SpriteKit

转载 作者:行者123 更新时间:2023-11-28 11:11:54 26 4
gpt4 key购买 nike

我正在开发一款 2D 游戏,我正在使用 Swift 和 SpriteKit。首先,我开始创建动画对象并向下移动。为了创建这些“对象”,我使用了 Spaceship(Xcode 的 Assets 图像)。

查看我的代码:

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 */
}
}

但是当我运行游戏时,Spaceship 静止不动。我找不到它可以解决。

有人可以帮助我吗?

宇宙飞船必须向下 (Y),创建副本,然后向下移动到 Y。

最佳答案

您需要在每艘飞船上运行moveAndRemove 操作。我建议您将 Action 定义移至 createSpaceship 方法,并对您创建的每个 spaceship 运行 moveAndRemove Action 。例如,

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)
}

关于swift - 按 Y 移动 SKSpriteNode - Swift 2 + SpriteKit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34459414/

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