gpt4 book ai didi

swift - 蛇在垂直方 block 中移动

转载 作者:可可西里 更新时间:2023-11-01 01:18:29 24 4
gpt4 key购买 nike

我正在尝试使用 SpriteKit 制作蛇游戏(就像旧诺基亚手机中的游戏)。问题是,当蛇的方向改变时,蛇在一个静止的垂直 block 中移动,并没有得到 L 形(如图所示)。为了进入上下文,我有一个名为 SKDSpriteNode 的类,它与 SKSpriteNode 相同,但有一个额外的 direction 属性; Snake 类有 directionlength 属性。有一个 snakeBody 属性,类型为 [SKDSpriteNode],包含蛇体的节点。

蛇的方向在游戏开始时分配给.up,然后由用户的滑动分配。顺便说一句,changeDirection 方法在蛇的方向改变时被调用(使用 didSet)。这是代码和图片:

extension ClassicLevelScene {

func startGame() { snake.direction = .up; moveSnake() }

func checkPlacement(for node: SKDSpriteNode) -> SKAction {
return SKAction.run({
if !(node.isInsideFrame(of: self)) { self.gameOver() }
})
}

func getMovement(for node: SKDSpriteNode) -> (once: SKAction, repetitive: SKAction) {
let movement = SKAction.move(by: node.direction.getVector(withIntensity: movementSpeed), duration: 0.5)
let moveAction = SKAction.sequence([movement, checkPlacement(for: node)])
let repetitiveMoveAction = SKAction.repeatForever(moveAction)
return (moveAction, repetitiveMoveAction)
}

func moveSnake() {
for node in snakeBody {
node.removeAllActions()
node.run(getMovement(for: node).1)
}
}

func moveOnce() {
for node in snakeBody {
node.removeAllActions()
node.run(getMovement(for: node).0)
}
}

func changeDirection() {
for i in 0..<snake.length {
if i == 0 {
snakeBody[i].direction = snake.direction
snakeBody[i].run(SKAction.move(by: snakeBody[i].direction.getVector(withIntensity: movementSpeed), duration: 0.5))
} else {
snakeBody[i].direction = snakeBody[i-1].direction
snakeBody[i].run(SKAction.move(to: snakeBody[i-1].oldPosition, duration: 0.5))
}
}
}

screenshot

最佳答案

我需要发布一个答案来向您展示代码,但您应该只移动头部,让尾部拖在后面。现在有更好的方法可以做到这一点,但是为了简化您已经编写的代码,它应该是这样的

extension ClassicLevelScene {

func startGame() { snake.direction = .up; moveSnake() }

func checkPlacement(for node: SKSpriteNode) -> SKAction {
return SKAction.run({
if !(node.isInsideFrame(of: self)) { self.gameOver() }
})
}

func getMovement(for node: SKDSpriteNode) -> (once: SKAction, repetitive: SKAction) {
let movement = SKAction.move(by: node.direction.getVector(withIntensity: movementSpeed), duration: 0.5)
let moveTail = SKAction.run({self.moveTail()})
let moveAction = SKAction.sequence([movement, moveTail,checkPlacement(for: node)])
let repetitiveMoveAction = SKAction.repeatForever(moveAction)
return (moveAction, repetitiveMoveAction)
}

func moveTail()
{
for i in 1..<snake.length {
snakeBody[i].direction = snakeBody[i-1].direction
snakeBody[i].position = snakeBody[i-1].oldPosition
}
}

func moveSnake() {
let head = snakeBody[0] {
head.removeAllActions()
head.run(getMovement(for: head).1)
}
}

func moveOnce() {
let head = snakeBody[0] {
head.removeAllActions()
head.run(getMovement(for: head).0)
}
}

func changeDirection() {
let head = snakeBody[0] {
head.removeAllActions()
head.direction = snake.direction
head.run(SKAction.move(by: head.direction.getVector(withIntensity: movementSpeed), duration: 0.5))
}
}

关于swift - 蛇在垂直方 block 中移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45121946/

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