基本的swift / spritekit游戏 - 麻烦重绘

时间:2016-02-17 11:27:47

标签: ios swift sprite-kit

我一直在玩swift并且一直试图用滚动视图制作一个基于平铺的游戏,所以玩家是中心。我是经理让它有点工作,但我似乎正在重新绘制当前存在的所有节点,所以在几次移动之后我有1000多个节点并且游戏停止了。我猜我需要在绘制新节点之前删除所有旧节点?我已经看过如何做到这一点,比如去除孩子,但最后我在移动后没有画出任何东西。

我已经包含了连接到下面精灵绘图的部分。

func placeTile2D(tile:Tile, direction:Direction, position:CGPoint) {
    let tileSprite = SKSpriteNode(imageNamed: textureImage(tile, direction: direction, action: Action.Idle))
    if (tile == hero.tile) {
        hero.tileSprite2D = tileSprite
        hero.tileSprite2D.zPosition = 1
    }
    tileSprite.position = position
    tileSprite.anchorPoint = CGPoint(x:0,y:0)
    view2D.addChild(tileSprite)

}
func placeAllTiles2D() {
    let playerPosCur = playerPosition
    let playerPCX: Int = (Int(playerPosCur.x))
    let playerPCY: Int = (-Int(playerPosCur.y))
    var w = 1
    var h = 1
    for i in (playerPCY - 4) ..< (playerPCY + 4){
        let row = tiles[i];
        w = 1
        for j in playerPCX - 4 ..< playerPCX + 4 {
            let tile = Tile(rawValue: row[j].0)!
            let direction = Direction(rawValue: row[j].1)!

            let point = CGPoint(x: (w*tileSize.width), y: -(h*tileSize.height))

            if w == 5 {
                if h == 5 {
                    self.placeTile2D(Tile.Player, direction: .n, position: point)
                }
            }

            w = w + 1

            placeTile2D(tile, direction : direction, position : point)
        }
        h = h + 1
    }
}

是我用来在玩家周围绘制图块,然后在下面,如果我用来改变玩家位置,然后调用placeAllTiles2D()函数根据新位置重绘。

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
    if let touch = touches.first {
        let location = touch.locationInNode(view2D)
        let touchPos2D = location
        let moveXR = CGPoint(x:1,y:0)
        let moveXL = CGPoint(x:-1,y:0)
        let moveYU = CGPoint(x:0,y:1)
        let moveYD = CGPoint(x:0,y:-1)
        let beforeMove = playerPosition
        var afterMove = CGPoint(x:0,y:0)

        if touchPos2D.x > self.size.width*3/4 {
            //Move player to right
            afterMove = beforeMove + moveXR
        }
        if touchPos2D.x < self.size.width*1/4 {
            //Move Player to left
            afterMove = beforeMove + moveXL
        }
        if touchPos2D.y > -self.size.height/2 {
            if touchPos2D.x < self.size.width*3/4 && touchPos2D.x > self.size.width*1/4 {
                //Move PLayer Up
                afterMove = beforeMove + moveYU
            }
        }
        if touchPos2D.y < -self.size.height/2 {
            if touchPos2D.x < self.size.width*3/4 && touchPos2D.x > self.size.width*1/4 {
                //Move Player Down
                afterMove = beforeMove + moveYD
            }
        }


        playerPosition = afterMove
        placeAllTiles2D()

    }
}

任何帮助都会非常感激。 请温柔,这是我第一次快速去做!

1 个答案:

答案 0 :(得分:3)

您需要使用SKCameraNode并将其视为移动视口而不是生成切片并在每帧重绘它们。 Here's a good example

基本上它的工作原理如下:

var sceneCam: SKCameraNode! //declare your camera variable in your scene class

然后,在didMoveToView函数中:

sceneCam = SKCameraNode() //initialize your camera
//scaleAsPoint lets you zoom the camera in and out
sceneCam.scaleAsPoint = CGPoint(x: 0.25, y: 0.25) 

camera = sceneCam  //set the scene's camera
addChild(sceneCam) //add camera to scene

//position the camera on the gamescene.
sceneCam.position = CGPoint(x: frame.center.x, y: frame.center.y)

现在,当您移动播放器时,只需将相机设置为与播放器相同的位置,您就可以拥有滚动场景。我同意这些意见。如果你回顾一下精灵工具包的一些基础知识,你将会有更好的时间 - 了解它附带的所有东西,你可以避免忽略SKCameraNode之类的东西。 SpriteKit为您做了很多事情,比如这个相机概念,可以帮助您快速轻松地制作游戏。