gpt4 book ai didi

swift - SpriteKit : Move Whole Scene

转载 作者:行者123 更新时间:2023-11-28 06:55:59 27 4
gpt4 key购买 nike

我想在场景中添加特定数量的方 block 。当我这样做时,广场的一部分在视野之外。是否可以在 y 轴上 move 整个场景?

最佳答案

我试过了,貌似场景本身不能 move 。另一种方法是将 SKNode 添加到代表整个世界的场景中。将所有其他元素添加到世界节点。现在如果你想 move 整个场景,你可以 move 这个节点。我正在我的博客中编写一个关于此的简短教程。这是我当前的代码:

import SpriteKit

class GameScene: SKScene {

// Declare the needed nodes
var worldNode: SKNode?
var spriteNode: SKSpriteNode?
var nodeWidth: CGFloat = 0.0

override func didMoveToView(view: SKView) {

// Setup world
worldNode = SKNode()
self.addChild(worldNode!)

// Setup sprite
spriteNode = SKSpriteNode(imageNamed: "Spaceship")
spriteNode?.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame))
spriteNode?.xScale = 0.1
spriteNode?.yScale = 0.1
spriteNode?.zPosition = 10
self.addChild(spriteNode!)

// Setup backgrounds
// Image of left and right node must be identical
let leftNode = SKSpriteNode(imageNamed: "left")
let middleNode = SKSpriteNode(imageNamed: "right")
let rightNode = SKSpriteNode(imageNamed: "left")

nodeWidth = leftNode.frame.size.width

leftNode.anchorPoint = CGPoint(x: 0, y: 0)
leftNode.position = CGPoint(x: 0, y: 0)
middleNode.anchorPoint = CGPoint(x: 0, y: 0)
middleNode.position = CGPoint(x: nodeWidth, y: 0)
rightNode.anchorPoint = CGPoint(x: 0, y: 0)
rightNode.position = CGPoint(x: nodeWidth * 2, y: 0)

worldNode!.addChild(leftNode)
worldNode!.addChild(rightNode)
worldNode!.addChild(middleNode)


}


var xOrgPosition: CGFloat = 0
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {

for touch in touches {

let xTouchPosition = touch.locationInNode(self).x
if xOrgPosition != 0.0 {
let xNewPosition = worldNode!.position.x + (xOrgPosition - xTouchPosition)

if xNewPosition <= -(2 * nodeWidth) {
// right end reached
worldNode!.position = CGPoint(x: 0, y: 0)
print("Right end reached")
} else if xNewPosition >= 0 {
// left end reached
worldNode!.position = CGPoint(x: -(2 * nodeWidth), y: 0)
print("Left end reached")
} else {

worldNode!.position = CGPoint(x: xNewPosition, y: 0)
}
}
xOrgPosition = xTouchPosition
}
}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
// Reset value for the next swipe gesture
xOrgPosition = 0
}

override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}

关于swift - SpriteKit : Move Whole Scene,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33637654/

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