GameScene类中必需的init方法

时间:2014-12-26 20:29:06

标签: macos swift sprite-kit skscene

我正在学习swift并使用sprite-kit框架。这些问题对我来说太奇怪了。 代码编译并解决了所有错误,但代码只在我接受init方法的Xcode解决方案时编译。

代码建议:

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

运行应用程序时,调试解释程序会标记致命错误行。我不知道如何解决这个错误。我在iOS上看到了其他sprite-kit示例,并且在gameScene类中没有人需要init方法,默认情况下这个类没有init方法。 我也没有修改appDelegate类,并且初始化了GameScene的属性。此外,所有类属性都在didMovetoView方法中获取值。

这是班上的负责人:

class GameScene: SKScene, SKPhysicsContactDelegate
{

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

    var world : SKNode
    var hud : SKNode
    var cursor : UInt32 = 0
    var Joystick : joystick
    var map : Map
    var exit : SKSpriteNode
    var spriteAtlas : SKTextureAtlas
    var player : SKSpriteNode
    var playerShadow : SKSpriteNode
    var isExitingLevel : Bool
    var playerIdleAnimationFrames : [SKTexture] = []
    var playerWalkAnimationFrames : [SKTexture] = []
    var playerAnimationID : Int = 0; // 0 = idle; 1 = walk
    var lastUpdateTimeInterval : NSTimeInterval = 0.0

    override func didMoveToView(view: SKView)
    {
        // Setup your scene here
        let myLabel = SKLabelNode(fontNamed:"Chalkduster")
        myLabel.text = "Hello, World!";
        myLabel.fontSize = 65;
        myLabel.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame));

        self.addChild(myLabel)


        // Inicio todas las variables de mi juego (nada de método init ni hostias)
        backgroundColor = NSColor(red:175.0/255.0, green:143.0/255.0, blue:106.0/255.0, alpha:1.0);
        isExitingLevel = false;

        // Add a node for the world - this is where sprites and tiles are added
        world = SKNode()

        // Load the atlas that contains the sprites
        spriteAtlas = SKTextureAtlas(named:"sprites")

        Joystick.velocity = CGPoint(x: 0, y: 0)

        // Create a new map
        map = Map()

        // Create the exit
        exit = SKSpriteNode(imageNamed: "exit")
        exit.position = self.map.exitPoint
        exit.physicsBody = SKPhysicsBody(rectangleOfSize: exit.size )
        exit.physicsBody?.categoryBitMask = CollisionType.Exit;
        exit.physicsBody?.collisionBitMask = 0;

        // Create a player node
        player = SKSpriteNode(imageNamed: "idle_0")
        player.position = self.map.spawnPoint;
        player.physicsBody?.allowsRotation = false;
        player.physicsBody = SKPhysicsBody(rectangleOfSize: player.size )
        player.physicsBody?.categoryBitMask = CollisionType.Player;
        player.physicsBody?.contactTestBitMask = CollisionType.Exit;
        player.physicsBody?.collisionBitMask = CollisionType.Wall;

        // Load sprites for player into arrays for the animations
        playerIdleAnimationFrames.append(self.spriteAtlas.textureNamed("idle_0"))

        playerWalkAnimationFrames.extend([self.spriteAtlas.textureNamed("walk_0"), self.spriteAtlas.textureNamed("walk_1"), self.spriteAtlas.textureNamed("walk_2")])

        playerShadow = SKSpriteNode(imageNamed: "shadow")
        playerShadow.xScale = 0.6
        playerShadow.yScale = 0.5
        playerShadow.alpha = 0.4

        playerAnimationID = 0;

        world.addChild(self.map)
        world.addChild(self.exit)
        world.addChild(self.playerShadow)
        world.addChild(self.player)

        // Create a node for the HUD - this is where the DPad to control the player sprite will be added
        self.hud = SKNode()

        // Add the world and hud nodes to the scene
        self.addChild(self.world)
        self.addChild(self.hud)

        // Initialize physics
        self.physicsWorld.gravity = CGVectorMake(0, 0);
    }

提前致谢。

1 个答案:

答案 0 :(得分:1)

我认为问题是,您拥有的唯一初始化程序是必需的init方法,因此每次创建类的项时都会调用它。所以每次都会抛出致命的错误。您需要声明一个init()方法来设置所有属性(我想知道为什么这个代码甚至可以编译)。在这个init方法中,您还必须通过super.init()初始化超类。然后不会调用另一个init函数。但实际上你无法删除这个功能。

必须实现所需的init,因为超类UIView具有此必需的初始化程序。 UIView的每个子类都必须根据需要实现此初始化程序。