我正在开发一款游戏,该游戏中的角色将像旧游戏机一样由一组4个按钮控制。我在spritekit中使用了Tile Map来创建地图。一切正常(玩家按下按钮时移动,场景跟随玩家),但当角色移动时我的四个按钮也移动。直到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节点(自身)。子节点的位置相对于其附加到的父节点。移动SKScene节点时,附加到该节点的所有节点都遵循套件,因为它们是子节点。
例如,将节点放置在(0,0),然后将节点附加到该节点的位置(10,0)。将父节点移动到(10,0),(10,0)的子节点将移动到(20,0),因为它的原点是相对于其父节点而不是一般场景的。
要解决您的问题,您需要创建另一个级别的节点。让我用另一个图像来说明。
[如果仅将移动应用于地图节点,则只有子级(连接到地图节点的节点)将移动。
因此,总的来说,您的代码将如下所示。
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元素不会移动。