[英]Basic swift/spritekit game - trouble redrawing
我一直在玩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()
}
}
任何幫助將非常感激。 請溫柔,這是我第一次快速去做!
您需要使用SKCameraNode
並將其視為移動視口而不是生成切片並在每幀重繪它們。 這是一個很好的例子
基本上它的工作原理如下:
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
這樣的SKCameraNode
。 SpriteKit為您做了很多事情,比如這個相機概念,可以幫助您快速輕松地制作游戲。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.