2016-05-04 2 views
2

ich mit diesem Tutorial gerade arbeite: https://www.raywenderlich.com/118225/introduction-sprite-kit-scene-editorWenn ich drücken Programm mein Spiel Lasten laufen, aber es startet nicht

Wenn ich Programm in Xcode laufen drücken, wird der Simulator das Spiel geladen und erscheint das erste Bild zeigt des Spiels, aber es ist eingefroren. Wenn ich klicke, sollte sich das Spieler-Sprite dorthin bewegen, wo ich geklickt habe, und die KI-Sprites sollen versuchen, den Spieler zu fangen, aber nichts passiert. Klicks funktionieren nicht und ich habe versucht, es einfach eine Weile sitzen zu lassen, um zu sehen, ob es gerade nicht geladen wurde oder etwas, aber das hat auch nicht funktioniert.

der gesamte Code für das Programm ist so weit hier:

import SpriteKit 

class GameScene: SKScene, SKPhysicsContactDelegate { 

    let playerSpeed: CGFloat = 150.0 
    let zombieSpeed: CGFloat = 75.0 

    var goal: SKSpriteNode? 
    var player: SKSpriteNode? 
    var zombies: [SKSpriteNode] = [] 

    var lastTouch: CGPoint? = nil 

    override func didMoveToView(view: SKView) { 
    physicsWorld.contactDelegate = self 
    updateCamera() 
    } 

    override func touchesBegan(touches: Set<UITouch>, withEvent event:  UIEvent?) { 
    handleTouches(touches) 
    } 

    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    handleTouches(touches) 
    } 

    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    handleTouches(touches) 
    } 

    private func handleTouches(touches: Set<UITouch>) { 
    for touch in touches { 
     let touchLocation = touch.locationInNode(self) 
     lastTouch = touchLocation 
    } 
    } 

    override func didSimulatePhysics() { 
    if let _ = player { 
     updatePlayer() 
     updateZombies() 
    } 
    } 

    private func shouldMove(currentPosition currentPosition: CGPoint, touchPosition: CGPoint) -> Bool { 
    return abs(currentPosition.x - touchPosition.x) > player!.frame.width/2 || 
     abs(currentPosition.y - touchPosition.y) > player!.frame.height/2 
    } 

    func updatePlayer() { 
    if let touch = lastTouch { 
     let currentPosition = player!.position 
     if shouldMove(currentPosition: currentPosition, touchPosition: touch) { 

     let angle = atan2(currentPosition.y - touch.y, currentPosition.x - touch.x) + CGFloat(M_PI) 
     let rotateAction = SKAction.rotateToAngle(angle + CGFloat(M_PI*0.5), duration: 0) 

     player!.runAction(rotateAction) 

     let velocotyX = playerSpeed * cos(angle) 
     let velocityY = playerSpeed * sin(angle) 

     let newVelocity = CGVector(dx: velocotyX, dy: velocityY) 
     player!.physicsBody!.velocity = newVelocity; 
     updateCamera() 
     } else { 
     player!.physicsBody!.resting = true 
     } 
    } 
    } 

    func updateCamera() { 
    if let camera = camera { 
     camera.position = CGPoint(x: player!.position.x, y: player!.position.y) 
    } 
    } 

    func updateZombies() { 
    let targetPosition = player!.position 

    for zombie in zombies { 
     let currentPosition = zombie.position 

     let angle = atan2(currentPosition.y - targetPosition.y, currentPosition.x - targetPosition.x) + CGFloat(M_PI) 
     let rotateAction = SKAction.rotateToAngle(angle + CGFloat(M_PI*0.5), duration: 0.0) 
     zombie.runAction(rotateAction) 

     let velocotyX = zombieSpeed * cos(angle) 
     let velocityY = zombieSpeed * sin(angle) 

     let newVelocity = CGVector(dx: velocotyX, dy: velocityY) 
     zombie.physicsBody!.velocity = newVelocity; 
    } 
    } 

    func didBeginContact(contact: SKPhysicsContact) { 
    var firstBody: SKPhysicsBody 
    var secondBody: SKPhysicsBody 

    if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask { 
     firstBody = contact.bodyA 
     secondBody = contact.bodyB 
    } else { 
     firstBody = contact.bodyB 
     secondBody = contact.bodyA 
    } 

    if firstBody.categoryBitMask == player?.physicsBody?.categoryBitMask && secondBody.categoryBitMask == zombies[0].physicsBody?.categoryBitMask { 
     gameOver(false) 
    } else if firstBody.categoryBitMask == player?.physicsBody?.categoryBitMask && secondBody.categoryBitMask == goal?.physicsBody?.categoryBitMask { 
     gameOver(true) 
    } 

    player = self.childNodeWithName("player") as? SKSpriteNode 

    for child in self.children { 
     if child.name == "zombie" { 
      if let child = child as? SKSpriteNode { 
       zombies.append(child) 
      } 
     } 
    } 
    goal = self.childNodeWithName("goal") as? SKSpriteNode 
    } 

    private func gameOver(didWin: Bool) { 
    print("- - - Game Ended - - -") 
    let menuScene = MenuScene(size: self.size) 
    menuScene.soundToPlay = didWin ? "fear_win.mp3" : "fear_lose.mp3" 
    let transition = SKTransition.flipVerticalWithDuration(1.0) 
    menuScene.scaleMode = SKSceneScaleMode.AspectFill 
    self.scene!.view?.presentScene(menuScene, transition: transition) 
    } 
} 

Der Rest der in dem Programm abgeschlossen Dinge wie Sprites hinzugefügt und so wurde in GameScene.sks getan, so dass kein Code dort ist es .

+0

Ich würde das Debuggen starten, indem ich print-Anweisungen hinzufüge, um zu sehen, ob meine Funktionen aufgerufen werden. –

Antwort

1

Ihr Setup-Code ist an der falschen Stelle. Bewegen Sie diese didBeginContact-didMoveToView:

player = self.childNodeWithName("player") as? SKSpriteNode 
for child in self.children { 
    if child.name == "zombie" { 
     if let child = child as? SKSpriteNode { 
      zombies.append(child) 
     } 
    } 
} 
goal = self.childNodeWithName("goal") as? SKSpriteNode 

Wenigstens das ist der Unterschied, den ich sehe, wenn Ihr Code auf das Beispielprojekt in dem Tutorial zu vergleichen.

+0

Danke für die Hilfe, es hat wie ein Zauber funktioniert! Ich kann mich nicht erinnern, dass ich es jemals an den falschen Ort gebracht habe, aber du hattest Recht. – cbarlow17

Verwandte Themen