genericguy25
genericguy25

Reputation: 602

SKSpriteNode in scene editor not using assigned custom class

I'm using Xcode 8.3.3.

I made a custom "Platform" class for some platforms. This is the first time I'm trying to use a custom class on a sprite made in the scene editor and for some reason the sprite just isn't using it.

It seems pretty cut and dry, I setup a sprite with default settings in the scene editor, then set the custom class designator like this (a screen cap of my actual project):enter image description here

Here is the actual class:

import UIKit
import SpriteKit

class Platform: SKSpriteNode {

init() {

    let texture = SKTexture(imageNamed: "blue_button01")

    super.init(texture: texture, color: SKColor.clear, size: texture.size())

    physicsBody = SKPhysicsBody(rectangleOf: texture.size())
    physicsBody?.categoryBitMask = CollisionTypes.platform.rawValue
    physicsBody?.contactTestBitMask = CollisionTypes.player.rawValue
    physicsBody?.collisionBitMask = CollisionTypes.player.rawValue
    physicsBody?.affectedByGravity = false
    physicsBody?.restitution = 0
    physicsBody?.isDynamic = false
    physicsBody?.allowsRotation = false
    setScale(2)
    zPosition = 2
    physicsBody?.linearDamping = 0.0

}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}



}

There are no errors it runs and complies fine, but when the game loads it loads a red sprite with default settings. I am expecting it to load the sprite with the settings of the "Platform.swift: class but it doesn't.

Any advice would be greatly appreciated.

(I'm aware of other methods to lay out levels, but I really like laying out the basics in the scene editor and then customizing everything in code.)

EDIT:

My end goal for my project's work flow is to open the .sks and place sprites where i want them, how large i want them, and what texture I want them to use, for example with selected blue platform here in the screenshot in the .sks I would place it like so:enter image description here

Then with my custom class I want to set up all the other requirements for the platform like collision bit masks, linear dampening, rotation, etc.

then after the custom class is applied I want to reference and use the sprite with it's custom class applied in my gamescene.swift file to setup things like movement actions.

Upvotes: 1

Views: 305

Answers (1)

Sweeper
Sweeper

Reputation: 271355

When your sprite node is initialised from the .sks file, the init that you defined is not called. Instead, it calls the init(coder:) initializer. You should set up your sprite in this initializer:

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    texture = SKTexture(image: #imageLiteral(resourceName: "Spaceship"))
    let texture = SKTexture(imageNamed: "blue_button01")
    self.texture = texture
    color = .clear
    size = texture.size()
    physicsBody = SKPhysicsBody(rectangleOf: texture.size())
    physicsBody?.categoryBitMask = CollisionTypes.platform.rawValue
    physicsBody?.contactTestBitMask = CollisionTypes.player.rawValue
    physicsBody?.collisionBitMask = CollisionTypes.player.rawValue
    physicsBody?.affectedByGravity = false
    physicsBody?.restitution = 0
    physicsBody?.isDynamic = false
    physicsBody?.allowsRotation = false
    setScale(2)
    zPosition = 2
    physicsBody?.linearDamping = 0.0
}

Upvotes: 4

Related Questions