Nick Pitoniak
Nick Pitoniak

Reputation: 97

Unable to implement SKSprite class. No error messages, just does not show up


I am creating a game in swift that incorporate a hero that jumps up and down. Unfortunately, the class that creates the hero is not appearing on the screen. I do not get any error messages - just no appearance. Here is my code, I think you will find it is rather simple:

//
//  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 theVar:String!
    var body: SKSpriteNode!
    var arm: SKSpriteNode!
    var leftFoot: SKSpriteNode!
    var rightFoot: SKSpriteNode!
    init() {
        super.init(texture: nil, color: UIColor.clearColor(), size: CGSizeMake(32, 44))
        self.theVar = "whoopwhoop"
        print("MADE IT")
        body = SKSpriteNode(color:UIColor.blackColor(), 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)
    }

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

}

As you can see, the code appears to be operational, and is pretty simple. I got some help from a programming fiend familiar with SKSprites, however he was stumped as well as to why the MLHero would not appear.

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

import SpriteKit

class GameScene: SKScene {

    var movingGround: MLMovingGround!

    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)
        print(hero.position)
    }

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

    override func update(currentTime: CFTimeInterval) {

    }
}

Upvotes: 0

Views: 23

Answers (1)

Luca Angeletti
Luca Angeletti

Reputation: 59536

You are not adding hero to the scene.

In GameScene, at the end of didMoveToView add

self.addChild(hero)

Upvotes: 2

Related Questions