저는 건물에있는 SpriteKit 게임을 가지고 있고 다차원 배열에서 레벨을로드 중입니다. loadlevel 함수가 처음으로 작동합니다. physicBody가 초기화 된 후 physicsBody
위의 physicsBody
위의 println
을 할당하면 실패합니다. removeChildrenInArray
으로 두 번째 타일을 모두 제거하면로드 레벨이 실행되어 fatal error: unexpectedly found nil while unwrapping an Optional
이라는 오류가 발생하고 아래의 println
바로 아래를 가리 킵니다. 그리고 println
은 physicsBody
이 nil
임을 나타냅니다. 내 마음 속에 PhysicsBody
을 새로 초기화하는 이유는 nil
이어야합니다. println
은 physicsBody nil
을 인쇄합니다. physicsBody
이 nil
인 이유를 알 수 없습니다. 레벨 맵에 따라 모든 블록 노드를 제거하고 원래 위치에 새 블록 노드를 추가하여 레벨을 재설정하려고합니다. 이 버그에 인 경우PhysicsBody는 Swift와 함께 SpriteKit을 사용하지 않습니다.
func reset() {
tileCount = 0
var removeTiles = [SKSpriteNode]()
// remove all the tiles
for child in scene.children {
var a_tile = child as SKSpriteNode
if a_tile.name.hasPrefix("tile_") {
a_tile.removeFromParent()
a_tile.name = ""
removeTiles.append(a_tile)
}
}
removeTiles.removeAll(keepCapacity: false)
ball!.position = CGPoint(x: 200, y: 200)
ballVel = CGPoint(x: 0, y: -5)
currentLevel++
loadLevel()
lost = false
won = false
}
내 Level
구조체
struct Tile {
let map = ["blue","green","purple","red"]
}
struct Levels {
let data = [
[
[4,4,0,4,0,4,0,4,0,4,0,4,0,0,4,4],
[4,4,1,4,1,4,1,4,1,4,1,4,1,1,4,4],
[4,4,2,4,2,4,2,4,2,4,2,4,2,2,4,4],
[4,4,3,4,3,4,3,4,3,4,3,4,3,3,4,4]
],
[
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[2,2,2,2,2,2,2,4,2,2,2,2,2,2,2,2],
[3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3]
]
]
}
입니다 : 여기
func loadLevel() {
var levels = Levels().data
var frameSize = view.frame.size
var thisLevel = levels[currentLevel]
println("running load level")
for (rowIndex,row) in enumerate(thisLevel) {
for (colIndex,col) in enumerate(row) {
if col == 4 {
continue
}
println("COL: \(col)")
var tile = SKSpriteNode(texture: SKTexture(imageNamed: "brick_\(tileMap[col])"))
tile.name = "tile_\(rowIndex)_\(colIndex)"
tile.position.y = frameSize.height - (tile.size.height * CGFloat(rowIndex)) - (tile.size.height/2)
tile.position.x = tile.size.width * CGFloat(colIndex) + (tile.size.width/2)
var physicsBody = SKPhysicsBody(rectangleOfSize: tile.size)
tile.physicsBody = physicsBody
tile.physicsBody.affectedByGravity = false
tile.physicsBody.categoryBitMask = ColliderType.Block.toRaw()
tile.physicsBody.contactTestBitMask = ColliderType.Ball.toRaw()
tile.physicsBody.collisionBitMask = ColliderType.Ball.toRaw()
scene.addChild(tile)
tileCount++
}
}
}
이 내 리셋 기능의 내용입니다 나의 ColliderType
enum ColliderType:UInt32 {
case Paddle = 1
case Block = 2
case Wall = 3
case Ball = 4
}
입니다 신속한 나는 오전 주위를 돌아 다니기 위해 나는이 일을 할 수 있습니다.
어떤처럼 구성 걸릴의 생성자 중 하나를 사용 이유는 body를 tile.physicsBody가 아닌 var에 먼저 할당하는 이유입니다. – LearnCocos2D
@ LearnCocos2D이 오류에서 벗어나려고 노력합니다. 네가 처음 몇 시간을 말할 때 나는하고 있었다. – Johnston
사실 우선, 우선 플랫폼 엔진에 내장 된 물리 엔진을 사용해서는 안됩니다. 각 타일에 대해 물리 구조를 만드는 것은 너무 집중적이고 결함을 유발할 수 있습니다. 자세한 내용은 다음을 참조하십시오. http://www.learn-cocos2d.com/2013/08/physics-engine-platformer-terrible-idea/h 두 번째로, remove 함수는 scene.removeAllChildren()에 단순화 될 수 있습니다. –