我正在尝试创建一个用户可以刷一个节点的游戏,一旦它刷过一个新节点,就会在屏幕底部创建并推动所有其他节点,有点像反向俄罗斯方块。这是一个非常基本的图片,可以给你一个想法:
我能够弄清楚如何在屏幕外滑动节点,但似乎无法弄清楚如何让所有其他节点向上移动并在底部创建一个新节点。我试过做一个" addChild"对于我刚刷过的节点,它可以再次出现在底部,但不断收到错误,说明节点已经有父节点。到目前为止,这是我的代码:
import SpriteKit
let plankName = "woodPlank"
class PlankScene: SKScene {
var plankWood : SKSpriteNode?
override func didMove(to view: SKView) {
plankWood = childNode(withName: "woodPlank") as? SKSpriteNode
let swipeRight : UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(PlankScene.swipedRight))
swipeRight.direction = .right
view.addGestureRecognizer(swipeRight)
}
func swipedRight(sender: UISwipeGestureRecognizer) {
if sender.direction == .right {
let moveOffScreenRight = SKAction.moveTo(x: 400, duration: 0.5)
let nodeFinishedMoving = SKAction.removeFromParent()
plankWood?.run(SKAction.sequence([moveOffScreenRight, nodeFinishedMoving]))
plankWood?.position = CGPoint(x: 0, y: -250)
addChild(plankWood!)
}
}
}
答案 0 :(得分:0)
正如@KnightOfDragon所说,您可以使用plankWood
创建.copy()
精灵的副本,这样您就可以创建一个函数来添加类似这样的板条:
func addPlank() {
let newPlank = plankWood.copy() as! SKSpriteNode
newPlank.position = CGPoint(x: 0, y: -250) //This should be your first plank position
addChild(newPlank)
}
然后你应该有一个SKSpriteNode
数组拿着你的木板,你可以有这样的功能来移动你的其他木板:
func movePlanksUp() {
for node:SKSpriteNode in yourPlankArray {
node.runAction(SKAction.moveBy(CGVector(dx: 0, dy: 250), duration: 0.10))
}
}
另外,如果您要创建plankArray
,则应将此行代码添加到addPlank()
函数中:yourPlankArray.append(newPlank)
我希望这有助于随时问我任何问题。