Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't get Custom Class working in SpriteKit Scene (swift & xcode7 beta)

I am not sure if it is a bug or not, but even when I set a Custom Class name on one of the Sprite, it seems to be completely ignored.

enter image description here

I tried with a dragged asset, and then with a empty node, both completely ignores the Monkey class association and just create a raw SKSpriteNode.

The Monkey Node code is as follow.

import Foundation
import SpriteKit

class Monkey: SKSpriteNode{

    let monkeyWalkAtlas = SKTextureAtlas(named: "MonkeyWalk")

    override init(texture: SKTexture?, color: NSColor, size: CGSize) {
        super.init(texture: texture, color: color, size: size)
        print("Monkey.init debug")
    }

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

Note: Custom class on .sks node are new in xcode7.

Edited June 21st 2015

I simplified the setup, but still the issue whe compiling/running in the iOS9 simulator. I created a project with the default SpriteKit template, chnage the GameSence to just have the following (and the empty Monkey class)

import SpriteKit

class GameScene: SKScene {

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        print("touch down")
       /* Called when a touch begins */
        for node in self.children {
            print(node)
        }
    }

    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
    }
}

public class Monkey: SKSpriteNode {}

Dragged an Empty node on the GameScene.sks, set the custom class to Monkey, but when I touch down to print the sub nodes, I get again:

touch down
<SKSpriteNode> name:'SKNode_0' texture:['nil'] position:{753.5, 333.5} scale:{1.00, 1.00} size:{0, 0} anchor:{0, 0} rotation:0.00
like image 709
Jeremy Chone Avatar asked Jun 14 '15 18:06

Jeremy Chone


People also ask

What is the difference between SceneKit and SpriteKit?

You can assume SpriteKit is at Top of the SceneKit, As using SceneKit you can add 3D models into Augmented Reality while SpriteKit is used to add extra sprites onto the model. In short SpriteKit is revolution in Gaming.

Can you use SpriteKit with SwiftUI?

Even though the default Game Xcode Template creates the project based on a UIKit application, you can create a SwiftUI app and put your SpriteKit game inside it without any hustle thanks to the SpriteView view!

What is a node in SpriteKit?

Nodes are the fundamental building blocks of SpriteKit, and SKNode is the base class of all nodes. All of your onscreen assets will be an SKNode or a subclass thereof. SKNode s by themselves do not provide any visual content, however.


1 Answers

In Swift you always have to add the module name before the class name. You can find the module name in the build settings in the field "Product Module Name". If you use a "-" in your project name the module name converts the "-" to a "_".(In my test project called "Test-Game" the module name is "Test_Game" so I would have to use the "Test_Game.Monkey" as a custom class which works for me in the current Xcode Beta)

like image 57
Nightmare_82 Avatar answered Sep 30 '22 05:09

Nightmare_82