Changing texture for subclass of SKSpriteNode? - swift

The update function in the class below, which is a subclass of SKSpriteNode, is supposed to change the texture of the class. According to this SO answer, updating the texture property is sufficient for changing a SKSpriteNode's texture. However, this code doesn't work.
Any ideas why?
class CharacterNode: SKSpriteNode {
let spriteSize = CGSize(width: 70, height: 100)
var level = 0
init(level: Int) {
self.level = level
super.init(texture: nil, color: UIColor.clear, size: spriteSize)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func update(level: Int) {
self.level = level
self.texture = textureForLevel(level: level)
}
func textureForLevel(level: Int) -> SKTexture {
return SKTexture(imageNamed: "TestTexture")
}

Your code works well. In fact this SO answer is correct.
About SKSpriteNode subclass, the update method is a new custom function added by you, because the instance method update(_:) is valid only for SKScene class, this just to say that this function is not performed if it is not called explicitly.
To make a test about your class you can change your code as below( I've changed only textureForLevel method only to make this example):
class CharacterNode: SKSpriteNode {
let spriteSize = CGSize(width: 70, height: 100)
var level = 0
init(level: Int) {
self.level = level
super.init(texture: nil, color: UIColor.clear, size: spriteSize)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func update(level: Int) {
self.level = level
self.texture = textureForLevel(level: level)
}
func textureForLevel(level: Int) -> SKTexture {
if level == 3 {
return SKTexture(imageNamed: "TestTexture3.jpg")
}
return SKTexture(imageNamed: "TestTexture.jpg")
}
}
class GameScene: SKScene {
override func didMove(to view: SKView) {
let characterNode = CharacterNode(level:2)
characterNode.update(level:2)
addChild(characterNode)
characterNode.position = CGPoint(x:self.frame.midX,y:self.frame.midY)
characterNode.run(SKAction.wait(forDuration: 2.0), completion: {
characterNode.update(level:3)
})
}
}
As you can see when you launch GameScene a characterNode sprite will be shown in the center of the screen. After 2 seconds, the texture will change.

Related

SpriteKit isUserInteractionEnabled=false Blocks Touches

I am building a SpriteKit game. The game scene is the parent of a transparent layer above it whose role is to display occasional messages to the player. I want this layer to be transparent and inert most of the time, and of course, never receive touches. As such, I have isUserInteractionEnabled set to false. However, when I add this later to the scene, it blocks all touches below it. What gives?
Edit: the parent of the MessageLayer is the game scene, and the game scene also has isUserInteractionEnabled = false, so I do not believe that the MessageLayer is inheriting the behavior.
Here is my code:
import Foundation
import SpriteKit
class MessageLayer: SKSpriteNode {
init() {
let size = CGSize(width: screenWidth, height: screenHeight)
super.init(texture: nil, color: UIColor.blue, size: size)
self.isUserInteractionEnabled = false
self.name = "MessageLayer"
alpha = 0.6
zPosition = +200
}
override init(texture: SKTexture!, color: UIColor, size: CGSize)
{
super.init(texture: texture, color: color, size: size)
}
required init(coder aDecoder: NSCoder) {
super.init(texture: nil, color: UIColor.clear, size: CGSize(width: 100, height: 100))
}
// MARK: - Functions
func alphaUp() {
alpha = 0.6
}
func alphaDown() {
alpha = 0.0
}
}
Just relay the touches to the scene / other nodes you want to do stuff with.
class TransparentNode: SKSpriteNode {
init(color: SKColor = .clear, size: CGSize) {
super.init(texture: nil, color: color, size: size)
isUserInteractionEnabled = true
}
// touches began on ios:
override func mouseDown(with event: NSEvent) {
// relay event to scene:
scene!.mouseDown(with: event)
}
required init?(coder aDecoder: NSCoder) { fatalError() }
}
class GameScene: SKScene {
lazy var transparentNode: SKSpriteNode = MyNode(size: self.size)
// touches began on ios:
override func mouseDown(with event: NSEvent) {
print("ho")
}
override func didMove(to view: SKView) {
addChild(transparentNode)
isUserInteractionEnabled = false
}
}
And here you can see that the invisible node is sending events properly:

Does running SKAction.repeatForever for an animation cause considerable CPU usage?

While testing my spritekit game, I noticed that using the SkAction.repeatForever(SkAction.animate()) causes considerable CPU usage. I have a basic 3 texture animation that creates a rising smoke effect. I would think this would be pretty simple.
I run it as follows:
self.extractorSprite.run(SKAction.repeatForever(SKAction.animate(with: self.extractorAnimation, timePerFrame: 0.4)))
The problem is that whenever this animation is run, it causes a constant 40% CPU usage which I would think is abnormal. If I comment out this line then CPU usage drops down to 2-7%.
Is there something I am missing about the SKAction? Why is it using so much CPU?
Thanks
* Update *
Here is the code for the extractor, the animation is called inside init()
class extractor: SKNode {
var extractorSprite:SKSpriteNode!
var extractorAtlas:SKTextureAtlas!
var extractorAnimation:Array<SKTexture> = []
var combatStats:unitCombatStats!
var resourceProduction:Int = 2
var resourceCost:Int = 4
var animation:SKAction!
init(extractorAtlas: SKTextureAtlas){
super.init()
self.createExtractor(Atlas: extractorAtlas)
self.startAnimation()
self.combatStats = unitCombatStats()
self.combatStats.setForExtractor()
}
private func createExtractor(Atlas: SKTextureAtlas){
self.extractorAtlas = Atlas
for name in Atlas.textureNames {
self.extractorAnimation.append(SKTexture(imageNamed: name))
}
self.extractorSprite = SKSpriteNode(imageNamed: Atlas.textureNames[0])
self.extractorSprite.size = CGSize(width: 50, height: 50)
self.extractorSprite.anchorPoint = CGPoint(x: 0.5, y: 0.5)
self.addChild(self.extractorSprite)
}
func startAnimation(){
self.extractorSprite.run(SKAction.repeatForever(SKAction.animate(with: self.extractorAnimation, timePerFrame: 0.4, resize: false, restore: true)), withKey: "ext-Animation")
}
func modifyColor(color: SKColor, blendfactor: CGFloat, blendMode: SKBlendMode){
self.extractorSprite.blendMode = blendMode
self.extractorSprite.colorBlendFactor = blendfactor
self.extractorSprite.color = color
}
func modifySpawnPosition(position: CGPoint){
self.extractorSprite.position = position
}
func modifyExtractorSize(newSize: CGSize){
self.extractorSprite.size = newSize
}
func getNodeCopy(andNameIt: String) -> extractor {
let copiedNode = extractor(extractorAtlas: self.extractorAtlas)
copiedNode.name = andNameIt
copiedNode.extractorSprite.size = self.extractorSprite.size
copiedNode.isHidden = false
return copiedNode
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.extractorSprite = aDecoder.decodeObject(forKey: "extractorSprite") as? SKSpriteNode
self.extractorAnimation = (aDecoder.decodeObject(forKey: "extractorAnimation") as? Array<SKTexture>)!
self.extractorAtlas = aDecoder.decodeObject(forKey: "extractorAtlas") as? SKTextureAtlas
}
override func encode(with aCoder: NSCoder) {
aCoder.encode(self.extractorSprite, forKey: "extractorSprite")
aCoder.encode(self.extractorAnimation, forKey: "extractorAnimation")
aCoder.encode(self.extractorAtlas, forKey: "extractorAtlas")
}
}
Let me know if that helps!

Passing Information To A Class On Initialisation

I'm struggling to understand how class initialisation works. From looking at solutions to other problems I have this as an example of the classes in my app.
import Foundation
import SpriteKit
class Ground : SKSpriteNode {
override init(texture: SKTexture!, color: SKColor, size: CGSize) {
super.init(texture: texture, color: color, size: size)
self.zPosition = -20
self.name = "Ground";
}
func configure (size: CGSize, position: CGPoint) {
self.size = size
self.position = position
// Set up the Physics
self.physicsBody = SKPhysicsBody(rectangleOf: size)
self.physicsBody?.contactTestBitMask = PhysicsCategory.Player
self.physicsBody?.categoryBitMask = PhysicsCategory.Ground
self.physicsBody?.collisionBitMask = PhysicsCategory.All
self.physicsBody?.affectedByGravity = false
self.physicsBody?.allowsRotation = false
self.physicsBody?.isDynamic = false
self.physicsBody?.mass = 1.99999
}
convenience init(color: SKColor, isActive: Bool = false) {
let size = CGSize(width: 10, height: 10);
self.init(texture:nil, color: color, size: size)
}
required init?(coder aDecoder: NSCoder) {
// Decoding length here would be nice...
super.init(coder: aDecoder)
}
}
I've put the 'configure' function as a kludge fix to let me pass the scene to the class so I can set sizes depending on the size of the device screen. Ideally I would like to just pull this information on initialisation but everything I try throws up errors that I don't understand.
Im not sure which way would be correct but I was wondering firstly, how would I pass arguments to the class to start with.. e.g..
let myClass = Ground(scene: self)
or can I somehow pull the scene information from directly within the class? I can pass info into functions/methods as I did with 'configure' but I couldn't get it to work on initialisation which would certainly be cleaner.
How would you guys do it?
Size
You should not programmatically change the size of a sprite depending on the current device, just use the same size and then let SpriteKit resizing it for you.
Loading from .sks
This initializer
init?(coder aDecoder: NSCoder)
is used when the Sprite is loaded from a .sks file. However in Xcode 7 you cannot pass values from the .sks to the sprite to set the attributes. I'm not sure you are using a .sks file so for now I am just throwing a fatal_error here. In Xcode 8 however you will be able to pass values from the .sks file to your own class.
required init?(coder aDecoder: NSCoder) {
fatalError("Not implemented")
}
Scene
You don't need to pass a reference of the current scene to your class. Every SKNode has the scene property, just use it to retrieve the scene where the node lives. Of course keep in mind that if you invoke .scene before the node has been added to a scene it will returns nil.
Code
This is the final code
class Ground : SKSpriteNode {
init(color: SKColor = .clearColor(), isActive:Bool = false) {
let texture = SKTexture(imageNamed: "ground")
super.init(texture: texture, color: color, size: texture.size())
self.name = "Ground"
self.zPosition = -20
let physicsBody = SKPhysicsBody(texture: texture, size: texture.size())
physicsBody.contactTestBitMask = PhysicsCategory.Player
physicsBody.categoryBitMask = PhysicsCategory.Ground
physicsBody.collisionBitMask = PhysicsCategory.All
physicsBody.affectedByGravity = false
physicsBody.allowsRotation = false
physicsBody.dynamic = false
physicsBody.mass = 1.99999
self.physicsBody = physicsBody
}
required init?(coder aDecoder: NSCoder) {
fatalError("Not implemented")
}
}
Unless I'm missing something else you're trying to do, you should be able to move the initialization of the PhysicsBody and the other properties from you configure() method and into your init() for the class. Then you can pre-size the texture you pass in on instantiation to result in the proper size for the device.
Also, the position property of the SKSpriteNode can be set by the instantiating class. This means you can accomplish the sizing based on device and the setting of the position property right after instantiating it in your scene and before adding it as a child of the scene.
ADDED:
class Ground : SKSpriteNode {
override init(texture: SKTexture!, color: SKColor, size: CGSize) {
super.init(texture: texture, color: color, size: size)
}
convenience init(texture: String, scene: SKScene, isActive: Bool = false) {
let texture = SKTexture(imageNamed: "\(yourtexturename)")
self.init(texture:teture, color: color, size: scene.size)
self.size = size
self.position = [YOU CAN NOW SET POSITION BASED ON THE SCENE]
self.physicsBody = SKPhysicsBody(rectangleOf: size)
self.physicsBody?.contactTestBitMask = PhysicsCategory.Player
self.physicsBody?.categoryBitMask = PhysicsCategory.Ground
self.physicsBody?.collisionBitMask = PhysicsCategory.All
self.physicsBody?.affectedByGravity = false
self.physicsBody?.allowsRotation = false
self.physicsBody?.isDynamic = false
self.physicsBody?.mass = 1.99999
self.zPosition = -20
self.name = "Ground";
}
required init?(coder aDecoder: NSCoder) {
// Decoding length here would be nice...
super.init(coder: aDecoder)
}
}
You might not need to override the original init() method but I didn't test anything.

add custom property to custom class and initialize class

I am trying to initialize a custom class that inherits from SKSpriteNode. Currently the class definition looks like this
import SpriteKit
class PassengerNode: SKSpriteNode {
let passengerStart: Int = 2
let passengerDestination: Int = 0
init() {
let texture = SKTexture(imageNamed: "passenger1")
super.init(texture: texture, color: UIColor.clearColor(), size: texture.size())
self.zPosition = 100
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func updateTexture(newTexture: SKTexture) {
self.texture = newTexture
}
}
I want to have passengerStart and passsengerDestination assigned to the Object, respectively to the instance of the class. And ideally these both values can be different with every instantiation.
When I instantiate the Class Object I think of something like
let newObject = PassengerNode(start: 1, destination: 0, texture: "passenger1")
But I dont get it working... How do I need to change my init?
Does someone have a helping thought?
Thanks in advance.
You have to write an init method for your subclass which takes those
three parameters:
class PassengerNode: SKSpriteNode {
let passengerStart: Int
let passengerDestination: Int
init(start: Int, destination: Int, texture: String) {
let texture = SKTexture(imageNamed: texture)
passengerStart = start
passengerDestination = destination
super.init(texture: texture, color: UIColor.clearColor(), size: texture.size())
self.zPosition = 100
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// ... more methods ...
}
The init method must initialize the properties of the subclass and
then call super.init.

Error when trying to initialize a class as a SKSpriteNode subclass and use it in GameScene - Swift 2

All the tutorials I have followed, make sprites with all their functions and everything in the same class as everything else - GameScene class. Now I am trying to divide the code from the GameScene to different classes but am having troubles with initializing the first class. Because I have almost no code, I will copy and paste all of it.
Error message:
/Users/Larisa/Desktop/Xcode/Igranje/Poskus2/Poskus2/PlayerUros.swift:17:15: Cannot invoke 'SKSpriteNode.init' with an argument list of type '(texture: SKTexture, position: CGPoint, size: () -> CGSize)'
GameScene class:
import SpriteKit
class GameScene: SKScene {
var player: PlayerUros!
override func didMoveToView(view: SKView) {
let pos = CGPointMake(size.width*0.1, size.height/2)
player = PlayerUros(pos: pos)
addChild(player)
player.rotate()
setupBackground()
}
func setupBackground() {
let BImage = SKSpriteNode(imageNamed: "bg-sky")
BImage.anchorPoint = CGPointMake(0, 0)
BImage.position = CGPointMake(0, 0)
BImage.size = CGSizeMake(self.size.width, self.size.height)
addChild(BImage)
}
}
Player class:
import Darwin
import SpriteKit
class PlayerUros: SKSpriteNode {
init(pos: CGPoint) {
let texture = SKTexture(imageNamed: "hero")
super.init(texture: texture, position: pos, size: SKTexture.size(texture))
}
func rotate() {
let rotate = SKAction.rotateByAngle(CGFloat(M_PI), duration: NSTimeInterval(1.5))
let repeatAction = SKAction.repeatActionForever(rotate)
self.runAction(repeatAction)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
and GameViewController class (not sure if it matters):
import UIKit
import SpriteKit
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let scene = GameScene(size: view.bounds.size)
let skView = view as! SKView
skView.showsFPS = true
skView.showsNodeCount = true
skView.ignoresSiblingOrder = true
scene.scaleMode = .ResizeFill
skView.presentScene(scene)
}
override func prefersStatusBarHidden() -> Bool {
return true
}
}
Try using the line
super.init(texture: texture, color: UIColor.clearColor(), size: texture.size())
instead of
super.init(texture: texture, position: pos, size: SKTexture.size(texture))
in your Player class. It works for me. Does it work for you?