在swift中,如果我在viewDidLoad()函数中调用了函数levelCompleted(),它将按预期工作。但是如果我在其他任何地方调用该函数,函数的结果就是一堆乱七八糟的块,应该按顺序排列。
这是正在使用的代码。
//
// ACLevelScene.swift
// Stick Exscape
//
// Created by Austin Collins on 24/08/2015.
// Copyright (c) 2015 Austin Collins. All rights reserved.
//
import Foundation
import SpriteKit
class ACLevelScene: SKScene, SKPhysicsContactDelegate {
var player: ACPlayer!
let grid: SKSpriteNode = SKSpriteNode()
var doorIsOpen = false
override func didMoveToView(view: SKView) {
self.physicsWorld.contactDelegate = self
// Change scenes anchor point
self.anchorPoint = CGPointMake (0.5,0.5);
// Allow multiable touches
view.multipleTouchEnabled = true
// Show physics for debug
//view.showsPhysics = true
// Background Color
self.backgroundColor = UIColor(red: 46/255, green: 32/255, blue: 0/255, alpha: 1.0)
// Draw the grid
drawGrid()
// Level to load
loadLevel(2)
// Add the notice
addChild(notice)
// When called here it works ax expected
//levelCompleted()
}
func levelCompleted() {
print("Open the menu because level is finished")
print("We also need to save that this level has been completed and in what time.")
print("This can also remove all the grids childs and load the next level.")
grid.removeAllChildren()
loadLevel(1)
}
func loadLevel(level: Float) {
doorIsOpen = false
var x: Int = 0
var y: Int = 0
let levelNumber: Int = Int(level)
let blocks = storedLevels[levelNumber]
let levelGridWidth = storedLevelSettings[levelNumber]?["gridWidth"]
let gridWidth: CGFloat = CGFloat(levelGridWidth!)
let levelGridHeight = storedLevelSettings[levelNumber]?["gridHeight"]
let gridHeight: CGFloat = CGFloat(levelGridHeight!)
grid.size.width = gridWidth*gBlockWidth
grid.size.height = gridHeight*gBlockHeight
// Set color here so levels can define a custom background in the future
grid.color = UIColor.whiteColor()
let backgroundImage = SKTexture(imageNamed: "sky.png")
grid.texture = backgroundImage
for var i: CGFloat = 0; i <= (gridHeight - 1); i += 1 {
y = Int(i)
for var i: CGFloat = 0; i <= (gridWidth-1); i += 1 {
x = Int(i)
let blockNumber = (gridWidth * CGFloat(y)) + CGFloat(x)
if let block = blocks?[Int(blockNumber)] {
addBlockToGrid(CGFloat(block), x: CGFloat(x), y: CGFloat(y), gridHeight: gridHeight)
} else {
addBlockToGrid(0, x: CGFloat(x), y: CGFloat(y), gridHeight: gridHeight)
}
}
}
}
func addPlayer(x: CGFloat, y: CGFloat) {
player = ACPlayer()
player.anchorPoint = CGPointMake(0.5, 0.5)
player.position = CGPointMake(x, y)
player.zPosition = 5
grid.addChild(player)
}
func drawGrid() {
grid.anchorPoint = CGPointMake(0, 1)
grid.position = CGPointMake(-(grid.size.width/2), -(grid.size.height/2))
addChild(grid)
let expand = SKAction.scaleXTo(0.8, y: 0.8, duration: 0)
grid.runAction(expand)
}
func addBlockToGrid(blockType: CGFloat, x: CGFloat, y: CGFloat, gridHeight: CGFloat) {
let xPosition: CGFloat = (x*gBlockWidth) + gBlockWidth/2
let yPosition: CGFloat = (gridHeight - (gBlockHeight*y)) - gBlockWidth/2
var block: SKSpriteNode!
var blockTexture: SKTexture!
if blockType == 0 {
// Air block
// Do nothing
}
else if blockType == 1 {
// Dirt Block
blockTexture = SKTexture(imageNamed: "dirtBlock.png")
block = SKSpriteNode(texture: blockTexture, color: UIColor.clearColor(), size: gBlockSize)
block.position.x = xPosition
block.position.y = yPosition
block.anchorPoint = CGPointMake(0.5, 0.5)
block.physicsBody = SKPhysicsBody(rectangleOfSize: gBlockSize)
block.physicsBody?.affectedByGravity = true
block.physicsBody?.dynamic = false
grid.addChild(block)
}
else if blockType == 2 {
// Grass Block
blockTexture = SKTexture(imageNamed: "grassBlock.png")
block = SKSpriteNode(texture: blockTexture, color: UIColor.clearColor(), size: gBlockSize)
block.position.x = xPosition
block.position.y = yPosition
block.anchorPoint = CGPointMake(0.5, 0.5)
block.physicsBody = SKPhysicsBody(rectangleOfSize: gBlockSize)
block.physicsBody?.affectedByGravity = false
block.physicsBody?.dynamic = false
grid.addChild(block)
}
else if blockType == 3 {
// Ice Block
blockTexture = SKTexture(imageNamed: "iceBlock.png")
block = SKSpriteNode(texture: blockTexture, color: UIColor.clearColor(), size: gBlockSize)
block.position.x = xPosition
block.position.y = yPosition
block.anchorPoint = CGPointMake(0.5, 0.5)
block.physicsBody = SKPhysicsBody(rectangleOfSize: gBlockSize)
block.physicsBody?.affectedByGravity = false
block.physicsBody?.dynamic = false
grid.addChild(block)
}
else if blockType == 4 {
// Stone Block
blockTexture = SKTexture(imageNamed: "stoneBlock.png")
block = SKSpriteNode(texture: blockTexture, color: UIColor.clearColor(), size: gBlockSize)
block.position.x = xPosition
block.position.y = yPosition
block.anchorPoint = CGPointMake(0.5, 0.5)
block.physicsBody = SKPhysicsBody(rectangleOfSize: gBlockSize)
block.physicsBody?.affectedByGravity = false
block.physicsBody?.dynamic = false
grid.addChild(block)
}
else if blockType == 5 {
// Flower Block
blockTexture = SKTexture(imageNamed: "flowerBlock.png")
block = SKSpriteNode(texture: blockTexture, color: UIColor.clearColor(), size: gBlockSize)
block.position.x = xPosition
block.position.y = yPosition
block.anchorPoint = CGPointMake(0.5, 0.5)
grid.addChild(block)
}
else if blockType == 6 {
addPlayer(xPosition, y: yPosition)
}
else if blockType == 7.0 {
// Bottom Of Door
blockTexture = SKTexture(imageNamed: "doorBottomBlock.png")
block = SKSpriteNode(texture: blockTexture, color: UIColor.clearColor(), size: gBlockSize)
block.position.x = xPosition
block.position.y = yPosition
block.anchorPoint = CGPointMake(0.5, 0.5)
block.name = "doorBottom"
block.physicsBody = SKPhysicsBody(rectangleOfSize: gBlockSize)
block.physicsBody?.affectedByGravity = false
block.physicsBody?.dynamic = false
block.physicsBody?.categoryBitMask = doorCategory
block.physicsBody?.contactTestBitMask = playerCategory
block.physicsBody?.collisionBitMask = 1
grid.addChild(block)
}
else if blockType == 7.1 {
// Top Of Door
blockTexture = SKTexture(imageNamed: "doorTopBlock.png")
block = SKSpriteNode(texture: blockTexture, color: UIColor.clearColor(), size: gBlockSize)
block.position.x = xPosition
block.position.y = yPosition
block.anchorPoint = CGPointMake(0.5, 0.5)
block.name = "doorTop"
block.physicsBody = SKPhysicsBody(rectangleOfSize: gBlockSize)
block.physicsBody?.affectedByGravity = false
block.physicsBody?.dynamic = false
block.physicsBody?.categoryBitMask = doorCategory
block.physicsBody?.contactTestBitMask = playerCategory
block.physicsBody?.collisionBitMask = 1
grid.addChild(block)
}
else if blockType == 8 {
// Key
blockTexture = SKTexture(imageNamed: "keyBlock.png")
let keySize = CGSizeMake(gBlockWidth/2, gBlockHeight/2)
block = SKSpriteNode(texture: blockTexture, color: UIColor.clearColor(), size: keySize)
block.position.x = xPosition
block.position.y = yPosition
block.name = "key"
block.anchorPoint = CGPointMake(0.5, 0.5)
block.physicsBody = SKPhysicsBody(rectangleOfSize: keySize)
block.physicsBody?.affectedByGravity = true
block.physicsBody?.dynamic = true
block.physicsBody?.categoryBitMask = keyCategory
block.physicsBody?.contactTestBitMask = playerCategory
let keyBounce = SKAction.moveBy(CGVector(dx: 0, dy: 3), duration: 1)
let keyPutBack = SKAction.moveBy(CGVector(dx: 0, dy: -3), duration: 1)
let keyBounceSequence = SKAction.sequence([keyBounce, keyPutBack])
block.runAction(SKAction.repeatActionForever(keyBounceSequence))
grid.addChild(block)
}
else if blockType == 9 {
// Barrier
block = SKSpriteNode(color: UIColor.clearColor(), size: gBlockSize)
block.position.x = xPosition
block.position.y = yPosition
block.anchorPoint = CGPointMake(0.5, 0.5)
block.physicsBody = SKPhysicsBody(rectangleOfSize: gBlockSize)
block.physicsBody?.affectedByGravity = false
block.physicsBody?.dynamic = false
grid.addChild(block)
}
}
func centerOnNode(node: SKNode) {
let nodePosition: CGPoint = convertPoint(node.position, fromNode: grid)
grid.position = CGPointMake(grid.position.x - nodePosition.x,
grid.position.y - nodePosition.y - gBlockHeight)
}
// Outside to avoid updates
var isZoomedOut = false
func didBeginContact(contact: SKPhysicsContact) {
let bodyOne: SKPhysicsBody = contact.bodyA
let bodyTwo: SKPhysicsBody = contact.bodyB
if (bodyOne.categoryBitMask == playerCategory && bodyTwo.categoryBitMask == keyCategory) || (bodyOne.categoryBitMask == keyCategory && bodyTwo.categoryBitMask == playerCategory) {
let key = grid.childNodeWithName("key")
key!.physicsBody?.dynamic = false
let keyAnimation = SKAction.rotateByAngle(720, duration: 0.3)
key?.runAction(keyAnimation, completion: { () -> Void in
key?.removeFromParent()
self.player.hasKey(true)
self.sendNotice("You got the key!")
let doorBottomTexture = SKTexture(imageNamed: "doorBottomOpenBlock.png")
let doorTopTexture = SKTexture(imageNamed: "doorTopOpenBlock.png")
let doorBottom: SKSpriteNode = self.grid.childNodeWithName("doorBottom") as! SKSpriteNode
let doorTop: SKSpriteNode = self.grid.childNodeWithName("doorTop") as! SKSpriteNode
doorBottom.texture = doorBottomTexture
doorTop.texture = doorTopTexture
self.doorIsOpen = true
})
}
if (bodyOne.categoryBitMask == playerCategory && bodyTwo.categoryBitMask == doorCategory) || (bodyOne.categoryBitMask == doorCategory && bodyTwo.categoryBitMask == playerCategory) {
if doorIsOpen {
self.sendNotice("Level Completed!")
// It does not work when called from here
self.levelCompleted()
} else {
self.sendNotice("The door is locked.")
}
}
}
}
我删除了所有相关函数中未使用的代码。
我想知道是否会发生任何不同的事情取决于被调用的地点或时间。
我也尝试将函数放在viewDidLoad()函数的计时器上,以正常方式调用函数。即使这样,在碰撞方法中调用它时也不会起作用,当计时器用完时它会正常工作。