ios - 基本的 swift/spritekit 游戏 - 重绘问题

标签 ios swift sprite-kit

我一直在玩 swift,并一直在尝试制作一个基于图 block 的游戏,该游戏具有 ScrollView ,因此玩家处于中心位置。我有经理让它开始工作,但我似乎正在重新绘制当前存在的节点之上的所有节点,所以在几步之后我有 1000 多个节点并且游戏逐渐停止。我猜我需要在绘制新节点之前删除所有旧节点?我已经了解了执行此操作的方法,例如移除子项,但最终我在移动后没有绘制任何内容。

我已经包含了与下面的 sprite 绘图相关的部分。

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
    }
}

是我用来在玩家周围绘制方 block 的方法,下面是我用来更改玩家位置的方法,然后调用 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()

    }
}

任何帮助将不胜感激。 请温柔点,这是我第一次快速行动!

最佳答案

您需要使用 SKCameraNode 并将其视为四处移动视口(viewport)而不是生成图 block 并在每一帧重新绘制它们。 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)

现在,当您四处移动播放器时,只需将相机设置为与播放器相同的位置,您就会看到滚动场景。我同意这些评论。如果您复习一下 sprite kit 的一些基础知识,您将会有更好的时间 - 了解它附带的所有东西,并且您可以避免忽略诸如 SKCameraNode 之类的东西。 SpriteKit 可以为您做很多事情,比如这个相机概念,可以帮助您快速轻松地制作游戏。

关于ios - 基本的 swift/spritekit 游戏 - 重绘问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35455420/

相关文章:

ios - 在 UIImage 上使用 Core Graphic 绘制渐变结果不一致

swift - XCTest UITests 在应用启动时失败,并显示过期 token 错误

sprite-kit - SpriteKit 中的物理接触

objective-c - 带有 NSCheckBox 单元格的 NSTableView - 如何拦截行选择?

swift - 如何在 SwiftUI 中检查 EditButton/EditMode 状态

ios - 使用单个 SKShapeNode 渲染多个重叠的多边形?

swift - 如何缩放以在所有设备中保持相同的距离?

ios - [非 A 类型保留] : message sent to deallocated instance, objective-c

iphone - [NSMutableArray array] 与 [[NSMutableArray alloc] init] 之间的区别

ios - 如何触发 Collection View 的 didSelectItemAtIndexPath : on tap while it's scrolling?