无法访问在没有游戏崩溃的情况下执行的简单方法

时间:2015-12-14 00:40:07

标签: ios swift


我正在创建一个简单的游戏来学习如何在Swift中使用SKSpriteNodes动画。我的游戏中有一个英雄,它沿着位于屏幕中心的表面运行。英雄可以翻到顶部或底部以避免随机生成的管道。英雄的flip()方法在应用程序出于某种原因运行时会崩溃。我尽我所能重写了它,但它仍然崩溃了。如果有任何不合适的消息,请告诉我。
GameScene.swift

//
//  GameScene.swift
//  marioRunner
//
//  Created by nick on 11/18/15.
//  Copyright (c) 2015 Supreme Leader. All rights reserved.
//

import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate {

    var movingGround: MLMovingGround!
    var hero: MLHero!
    var wallGenerator: MLWallGenerator!
    var isStarted = false

    override func didMoveToView(view: SKView) {
        backgroundColor = UIColor(red: 159.0/255.0, green: 201.0/255.5, blue: 244.0/255.0, alpha: 1.0)
        movingGround = MLMovingGround(size: CGSizeMake(view.frame.width, 20))
        movingGround.position = CGPointMake(0, view.frame.size.height/2)
        addChild(movingGround)

        let hero = MLHero()
        hero.position = CGPointMake(70, movingGround.position.y + movingGround.frame.size.height/2 + hero.frame.size.height/2)
        self.addChild(hero)

        wallGenerator = MLWallGenerator(color: UIColor.clearColor(), size: view.frame.size)
        wallGenerator.position = view.center
        addChild(wallGenerator)
        physicsWorld.contactDelegate = self
    }

    func start() {
        isStarted = true
        movingGround.start()
        wallGenerator.startGeneratingWallsEvery(1)
        hero.flip()
    }

    func gameOver() {
        hero.stop()
    }


    func didBeginContact(contact: SKPhysicsContact) {
        //gameOver()
        hero.body.color = UIColor.orangeColor()
        print("HIT")
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        if isStarted == false{
            start()
        } else {
            hero.flip()
        }
    }

    override func update(currentTime: CFTimeInterval) {

    }
}

MLHero.swift

//
//  MLHero.swift
//  marioRunner
//
//  Created by nick on 12/7/15.
//  Copyright © 2015 Supreme Leader. All rights reserved.
//

import Foundation
import SpriteKit

class MLHero: SKSpriteNode {
    var body: SKSpriteNode!
    var arm: SKSpriteNode!
    var leftFoot: SKSpriteNode!
    var rightFoot: SKSpriteNode!
    var isUpsideDown = false
    init() {
        super.init(texture: nil, color: UIColor.clearColor(), size: CGSizeMake(32, 44))
        loadPhysicsBodyWithSize(size)
        body = SKSpriteNode(color:UIColor.redColor(), size: CGSizeMake(self.frame.size.width, 40))
        body.position = CGPointMake(0, 2)
        addChild(body)
        let skinColor = UIColor(red:207.0/255.0, green:193.0/255.0, blue: 168.0/255.0, alpha: 1.0)
        let face = SKSpriteNode(color: skinColor, size: CGSizeMake(self.frame.size.width, 2))
        face.position = CGPointMake(0, 6)
        body.addChild(face)
        let eyeColor = UIColor.whiteColor()
        let leftEye = SKSpriteNode(color: eyeColor, size: CGSizeMake(6,6))
        let rightEye = leftEye.copy() as! SKSpriteNode
        let pupil = SKSpriteNode(color: UIColor.blackColor(), size: CGSizeMake(3,3))
        pupil.position = CGPointMake(2, 0)
        leftEye.addChild(pupil)
        rightEye.addChild(pupil.copy() as! SKSpriteNode)
        leftEye.position = CGPointMake(-4, 0)
        face.addChild(leftEye)
        rightEye.position = CGPointMake(14, 0)
        face.addChild(rightEye)
    }
    /*func performOneRunCycle() {
        let up = SKAction.moveByX(0, y: 2, duration: 0.05)
        let down = SKAction.moveByX(0, y: -2, duration: 0.05)
    }*/

    func loadPhysicsBodyWithSize(size: CGSize) {
        physicsBody = SKPhysicsBody(rectangleOfSize: size)
        physicsBody?.categoryBitMask = heroCategory
        physicsBody?.contactTestBitMask = wallCategory
        physicsBody?.affectedByGravity = false
    }

    func flip() {
        isUpsideDown = !isUpsideDown
        var scale: CGFloat
        if isUpsideDown {
            scale = -1.0
        } else {
           scale = 1.0
        }
        let translate = SKAction.moveByX(0, y: scale*(size.height + kMLGroundHeight), duration: 0.1)
        let flip = SKAction.scaleYTo(scale, duration: 0.1)

        runAction(translate)
        runAction(flip)
    }

    func stop() {
        //body.removeAllActions()
        print("stopped")
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

这是我收到的崩溃报告:http://i.imgur.com/tyPiiVh.png
它详细说明了英雄对象是零。如果我正确地实例化它,我不确定它是如何可以为零。 如果您需要项目的其他/每个文件,如果这两个文件不够,请告诉我 感谢

0 个答案:

没有答案