initialize SKNode with multiple SKSpriteNodes

duxfox--

I'm building a game where the characters have health bars. I did some quick research and found that subclassing an SKNode and adding SKSpriteNode members for the character and the health bar respectively is a good approach. So currently I have this:

class GameCharacter: SKNode {

 let characterBody = SKSpriteNode()
 let characterHealthBar = SKSpriteNode()

}

however I'm not sure how to init this so that both the sprite nodes appear. Effectively what I want is the character body on top, and the health bar underneath it.

I know this is the init method for SKSpriteNode to initialize with a size and texture:

override init() {
 let texture = SKTexture(imageNamed: "CharacterStanding.png")
 super.init(texture: texture, color: UIColor.clearColor(), size: texture.size())
}

does SKNode have a similar init method? or is it sufficient to init the two SKSpriteNodes within the SKNode and then just make a new GameCharacter object in the game scene?

or I guess what I'm trying to ask is; how do I init this GameCharacter(SKNode) object and both character and health bar appear?

duxfox--

heres a solution I found so far:

in the init method just do the following:

override init() {
 super.init()
 self.addChild(characterBody)
 characterHealthBar.position = 
     CGPoint(x: characterBody.position.x, y: characterBody.position.y - 45)
 self.addChild(characterHealthBar)
}

and this will init the node with the character and the health bar below him/her

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

tmuxinator initialize pane with multiple commands

From Dev

Possible to initialize multiple variables from a tuple?

From Dev

Camera preview on multiple views - initialize/release handling

From Dev

Initialize multiple C++-Arrays with common data

From Dev

How to subclass an SKNode to initialize it with a predetermined size

From Dev

SpriteKit SKSpriteNodes management

From Dev

Python: Multiple ways to initialize a class

From Dev

The usage of global static pointer with multiple initialize?

From Dev

'SKNode!' is not a subtype of 'SKNode'

From Dev

initialize multiple variables with constructor overloading

From Dev

SKNode! is not convertible to "SKNode"

From Dev

SKNode lifetime

From Dev

JSP/JSTL : Initialize Multiple variables in one statement

From Dev

Scaling of SkSpriteNodes with SpriteKit

From Dev

Is it possible to initialize multiple lazy collections in parallel?

From Dev

How to initialize multiple variables together?

From Dev

Python initialize multiple variables to the same initial value

From Dev

Initialize UserForm multiple times

From Dev

SpriteKit: Add Multiple SKSpriteNodes To The Scene

From Dev

Initialize member variables with multiple constructor calls

From Dev

VHDL initialize variable to multiple values

From Dev

Use loop to initialize multiple objects

From Dev

initialize multiple variables with constructor overloading

From Dev

SKNode! is not convertible to "SKNode"

From Dev

Calling SKSpriteNodes based on of their name

From Dev

initialize multiple bools in c++

From Dev

Initialize UserForm multiple times

From Dev

Initialize constructor with multiple parameters

From Dev

SpriteKit: A gap between SKSpriteNodes

Related Related

HotTag

Archive