我正在制作一款游戏,在这款游戏中,角色将由一组4个按钮控制,就像在旧的游戏机中一样。我使用spritekit中的瓷砖地图来创建地图。一切都很好(玩家在按下按钮时移动,场景跟着玩家移动),除了角色移动时我的4个按钮也会移动。直到4个按钮移动到屏幕外,我再也无法控制它了。我们如何将这4个按钮固定在屏幕的右下角?下面是我用来创建受控按钮的代码。
func controlButton() {
button = SKNode()
moveUpButton = SKSpriteNode(imageNamed: "moveup")
moveUpButton.alpha = 1
moveUpButton.setScale(1.5)
moveUpButton.position = CGPoint(x: 400 - self.frame.size.width/2, y: 0 - self.frame.size.height/2)
moveUpButton.zPosition = 2
moveLeftButton = SKSpriteNode(imageNamed: "moveleft")
...
moveRightButton = SKSpriteNode(imageNamed: "moveright")
...
moveDownButton = SKSpriteNode(imageNamed: "movedown")
...
button.addChild(moveUpButton)
button.addChild(moveLeftButton)
button.addChild(moveRightButton)
button.addChild(moveDownButton)
self.addChild(button)
}
这里是我用来创建瓦片地图的代码,为包含墙的瓦片添加带有物理体的节点。
func setUpSceneWithMap(map: SKTileMapNode) {
let tileMap = map
tileMap.setScale(1)
tileMap.position = CGPoint(x: 0 - self.frame.size.width/2, y: 0 - self.frame.size.height/2)
let tileSize = tileMap.tileSize
let halfWidth = CGFloat(tileMap.numberOfColumns) / 2.0 * tileSize.width
let halfHeight = CGFloat(tileMap.numberOfRows) / 2.0 * tileSize.height
for col in 0..<tileMap.numberOfColumns {
for row in 0..<tileMap.numberOfRows {
let tileDefinition = tileMap.tileDefinition(atColumn: col, row: row)
let isEdgeTile = tileDefinition?.userData?["isWalls"] as? Bool
if (isEdgeTile ?? false) {
let x = CGFloat(col) * tileSize.width - halfWidth
let y = CGFloat(row) * tileSize.height - halfHeight
let rect = CGRect(x: 0, y: 0, width: tileSize.width, height: tileSize.height)
let tileNode = SKShapeNode(rect: rect)
tileNode.position = CGPoint(x: x, y: y)
tileNode.physicsBody = SKPhysicsBody.init(rectangleOf: tileSize, center: CGPoint(x: tileSize.width / 2.0, y: tileSize.height / 2.0))
tileNode.physicsBody?.categoryBitMask = gamePhysics.Wall
tileNode.physicsBody?.collisionBitMask = gamePhysics.Player
tileNode.physicsBody?.contactTestBitMask = gamePhysics.Player
tileNode.physicsBody?.isDynamic = false
tileMap.addChild(tileNode)
}
}
}
}
下面是我将地图添加到场景中的代码。
func createScene() {
self.physicsWorld.contactDelegate = self
for node in self.children {
if (node is SKTileMapNode){
if let theMap:SKTileMapNode = node as? SKTileMapNode {
setUpSceneWithMap(map: theMap)
}
}
}
createPlayer()
createCamera()
}
基本上,SpriteKit订阅了一个子-父节点结构。
让我用一张图片来说明。
其中每个圆圈代表一个节点。目前,你正在移动SKScene节点(self)。子节点的位置是相对于它所附着的父节点而言的。当您移动 SKScene 节点时,所有附加在它上面的节点也会跟着移动,因为它们是子节点。
例如,将一个节点定位在(0,0),然后在(10,0)位置附加一个节点到该节点上。将父节点移动到(10,0),位于(10,0)的子节点将移动到(20,0),因为它的原点是相对于它的父节点,而不是相对于一般场景。
要解决你的问题,你需要再创建一个级别的节点。让我用另一张图片来说明。
如果你只对Map Node应用移动,那么只有子节点(连接到Map Node的节点)会移动。
因此,总的来说,你的代码看起来像这样。
class GameScene: SKScene
{
let MapNode,
UINode = SKNode; //This is a fancy way to declare multiple variables of the same type
func controlButton()
{
... //Your other code here
UINode.addChild(button) //Instead of self.addChild(button)
}
func setUpSceneWithMap(map: SKTileMapNode)
{
//Your other code in here
MapNode.addChild(map)
// I don't know where you are adding your tile map to your scene
// However, it most likely is self.addChild(map) which needs to be changed to MapNode.addChild(map)
}
}
将所有场景元素附加到MapNode上,将所有UI元素附加到UINode上。只对MapNode应用位置变化。除非你想让UI元素移动,否则它们不会移动。