I'm making a simple iOS game, and I have some problems.
Indeed, I created my different classes for my elements : planet, bullet, background, and asteroids.
However when I try to initialize them on switch condition (from the GameScene), I have the error message "expected declaration".
The variable "lvlSelected", is global and represents the level asked by the player (from an other scene).
Here's my switch code :
switch lvlSelected {
case 1 :
private var planet = Planet(imageName: "Planet1")
private var bullet = Bullet(imageName: "Bullet1")
private var background = Background(imageName: "Background1")
private var asteroide = Asteroides(img: "Asteroid1")
case 2 :
private var planet = Planet(imageName: "Planet2")
private var bullet = Bullet(imageName: "Bullet2")
private var background = Background(imageName: "Background2")
private var asteroide = Asteroides(img: "Asteroid2")
default :
private var planet = Planet(imageName: "Planet")
private var bullet = Bullet (imageName: "Bullet")
private var background = Background(imageName : "Background")
private var asteroide = Asteroides(img."Asteroid")
}
Thanks for your help
Here's my Planet's class :
class Planet:SKSpriteNode {
init(imageName: String){
let texture = SKTexture(imageNamed : imageName)
super.init(texture: texture, color: .clear, size: texture.size())
self.physicsBody = SKPhysicsBody(circleOfRadius: self.size.width/2, center: self.anchorPoint)
self.physicsBody!.affectedByGravity = false
self.physicsBody!.categoryBitMask = GameScene.physicsCategories.planet
self.physicsBody!.collisionBitMask = GameScene.physicsCategories.none
self.physicsBody!.contactTestBitMask = GameScene.physicsCategories.asteroid
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func addPlanet(parent: GameScene) {
self.setScale(0.55)
self.position = CGPoint(x: parent.size.width / 2, y: -self.size.height)
self.zPosition = 2
self.anchorPoint = CGPoint(x: 0.5, y: 0.5)
parent.addChild(self)
}
This should be the way you want.
private var planet = Planet(imageName: "Planet")
private var bullet = Bullet (imageName: "Bullet")
private var background = Background(imageName : "Background")
private var asteroide = Asteroides(img."Asteroid")
switch lvlSelected {
case 1 :
planet = Planet(imageName: "Planet1")
bullet = Bullet(imageName: "Bullet1")
background = Background(imageName: "Background1")
asteroide = Asteroides(img: "Asteroid1")
break
case 2 :
planet = Planet(imageName: "Planet2")
bullet = Bullet(imageName: "Bullet2")
background = Background(imageName: "Background2")
asteroide = Asteroides(img: "Asteroid2")
break
default :
break
}
If you declare a var inside a switch case will be available inside that case only.
There is no need for a switch in your case, you can use string interpolation:
class GameScene : SKScene
{
private let planet : Planet
private let bullet : Bullet
private let background : Background
private let asteroide : Asteroides
convenience init(lvlSelected : Int){
let level = 1 ... 2 ~= lvlSelected ? "\(lvlSelected)" : ""
planet = Planet(imageName: "Planet\(level)")
bullet = Bullet (imageName: "Bullet\(level)")
background = Background(imageName: "Background\(level)")
asteroid = Asteroides(img: "Asteroid\(level)")
self.init() //whatever init you use here
}
}
~= is the pattern match operator, and #a ... #b means range of numbers between a and b including a and b, so you are asking if a number is in a certain range
(condition) ? (statement 1) : (statement 2) is the ternary operator which is if condition true then statement 1 else statement 2
"\(var)" is string interpolation that tells the string to inject the value of var into the string
Related
Go to mixamo.com, pick a character, tap animations, pick one, simply download as .dae.
Have the file on your Mac desktop; tap file info). It will perfectly animate the character move.
Xcode, drag in the folder. Tap the .dae file, tap the Play icon at the bottom. It will perfectly animate the character move.
Now, add the character to your existing SceneKit scene. For example:
let p = Bundle.main.url(forResource: "File Name", withExtension: "dae")!
modelSource = SCNSceneSource(url: p, options: nil)!
let geom = modelSource.entryWithIdentifier("geometry316",
withClass: SCNGeometry.self)! as SCNGeometry
theModel = SCNNode(geometry: geom)
.. your node .. .addChildNode(theModel)
(To get the geometry name, just look in the .dae text example )
You will PERFECTLY see the character, in T pose
However it seems impossible to run the animation on the character.
Code would look something like ...
theAnime = amySource.entryWithIdentifier("unnamed_animation__0", withClass: CAAnimation.self)!
theModel.addAnimation(theAnime, forKey:"aKey")
No matter what I try it just doesn't animate.
At the moment you addAnimation, the character jumps to a different static position, and does nothing. (If you arrange to "end" the animation removeAllAnimations(), it simply returns to the T-pose.)
Clearly the dae file is perfect since the shows the animation perfectly simply in the Mac finder, and perfectly on the actual screen of the .dae file in Xcode!
In short, from the mixamo image above, has anyone been able to get the animation to run, actually, in a SceneKit scene?
(PS not ARKit .. scene kit.)
First, you need your character in the T-Position only. Download that file as Collada (DAE) with the Skin. Do NOT include any animations to this File. No Further modifications are required to this file then.
Then, for any animation effect you will implement like walking, running, dancing, or whatever - do it like so:
Test/Apply your desired animation in Mixamo on the character, adjust the settings as you want then download it. Here it is very important to Download as Collada (DAE) and choose WITHOUT Skin!!! Leave Framerate and keyframe reduction default.
This will give you a single DAE File for each animation you want to implement. This DAE contains no mesh data and no rig. It only contains the deformations of the Model to which it belongs (this is why you choose to download it without Skin).
Then you need to do two additional operations on all DAE Files which contains animations.
First, you need to pretty-print the XML structure of each DAE containing an animation. You can do this i.Ex. using the XML Tools in Notepad++ or you open a terminal on your Mac and use this command:
xmllint —-format my_anim_orig.dae > my_anim.dae
Then install this Tool here on your Mac.
(https://drive.google.com/file/d/0B1_uvI21ZYGUaGdJckdwaTRZUEk/edit?usp=sharing)
Convert all of your DAE Animations with this converter:
(But do NOT convert your T-Pose Model using this tool!!!)
No we are ready to setup the Animation:
you should organise the DAE's within the art.scnassets folder
Let's configure this:
I usually organise this within a struct called characters. But any other implementation will do
add this:
struct Characters {
// MARK: Characters
var bodyWarrior : SCNNode!
private let objectMaterialWarrior : SCNMaterial = {
let material = SCNMaterial()
material.name = "warrior"
material.diffuse.contents = UIImage.init(named: "art.scnassets/warrior/textures/warrior_diffuse.png")
material.normal.contents = UIImage.init(named: "art.scnassets/warrior/textures/warrior_normal.png")
material.metalness.contents = UIImage.init(named: "art.scnassets/warrior/textures/warrior_metalness.png")
material.roughness.contents = UIImage.init(named: "art.scnassets/warrior/textures/warrior_roughness.png")
material.ambientOcclusion.contents = UIImage.init(named: "art.scnassets/warrior/textures/warrior_AO.png")
material.lightingModel = .physicallyBased
material.isDoubleSided = false
return material
}()
// MARK: MAIN Init Function
init() {
// Init Warrior
bodyWarrior = SCNNode(named: "art.scnassets/warrior/warrior.dae")
bodyWarrior.childNodes[1].geometry?.firstMaterial = objectMaterialWarrior // character body material
print("Characters Init Completed.")
}
}
Then you can init the struct i.Ex. in the viewDidLoad
var characters = Characters()
Pay Attention to use the correct childNodes!
in this case the childNodes[1] is the visible mesh and childNodes[0] then will be the animation Node.
you might also implement this SceneKit extension to your code, it is very useful to import Models. (attention, it will organise the model nodes as Childs from a new node!)
extension SCNNode {
convenience init(named name: String) {
self.init()
guard let scene = SCNScene(named: name) else {return}
for childNode in scene.rootNode.childNodes {addChildNode(childNode)}
}
}
also add that extension below. You'll need it for the animation player later.
extension SCNAnimationPlayer {
class func loadAnimation(fromSceneNamed sceneName: String) -> SCNAnimationPlayer {
let scene = SCNScene( named: sceneName )!
// find top level animation
var animationPlayer: SCNAnimationPlayer! = nil
scene.rootNode.enumerateChildNodes { (child, stop) in
if !child.animationKeys.isEmpty {
animationPlayer = child.animationPlayer(forKey: child.animationKeys[0])
stop.pointee = true
}
}
return animationPlayer
}
}
Handle Character setup and Animation like so:
(here is a simplified version of my Class)
class Warrior {
// Main Nodes
var node = SCNNode()
private var animNode : SCNNode!
// Control Variables
var isIdle : Bool = true
// For Initial Warrior Position and Scale
private var position = SCNMatrix4Mult(SCNMatrix4MakeRotation(0,0,0,0), SCNMatrix4MakeTranslation(0,0,0))
private var scale = SCNMatrix4MakeScale(0.03, 0.03, 0.03) // default size ca 6m height
// MARK: ANIMATIONS
private let aniKEY_NeutralIdle : String = "NeutralIdle-1" ; private let aniMAT_NeutralIdle : String = "art.scnassets/warrior/NeutralIdle.dae"
private let aniKEY_DwarfIdle : String = "DwarfIdle-1" ; private let aniMAT_DwarfIdle : String = "art.scnassets/warrior/DwarfIdle.dae"
private let aniKEY_LookAroundIdle : String = "LookAroundIdle-1" ; private let aniMAT_LookAroundIdle : String = "art.scnassets/warrior/LookAround.dae"
private let aniKEY_Stomp : String = "Stomp-1" ; private let aniMAT_Stomp : String = "art.scnassets/warrior/Stomp.dae"
private let aniKEY_ThrowObject : String = "ThrowObject-1" ; private let aniMAT_ThrowObject : String = "art.scnassets/warrior/ThrowObject.dae"
private let aniKEY_FlyingBackDeath : String = "FlyingBackDeath-1" ; private let aniMAT_FlyingBackDeath : String = "art.scnassets/warrior/FlyingBackDeath.dae"
// MARK: MAIN CLASS INIT
init(index: Int, scaleFactor: Float = 0.03) {
scale = SCNMatrix4MakeScale(scaleFactor, scaleFactor, scaleFactor)
// Config Node
node.index = index
node.name = "warrior"
node.addChildNode(GameViewController.characters.bodyWarrior.clone()) // childNodes[0] of node. this holds all subnodes for the character including animation skeletton
node.childNodes[0].transform = SCNMatrix4Mult(position, scale)
// Set permanent animation Node
animNode = node.childNodes[0].childNodes[0]
// Add to Scene
gameScene.rootNode.addChildNode(node) // add the warrior to scene
print("Warrior initialized with index: \(String(describing: node.index))")
}
// Cleanup & Deinit
func remove() {
print("Warrior deinitializing")
self.animNode.removeAllAnimations()
self.node.removeAllActions()
self.node.removeFromParentNode()
}
deinit { remove() }
// Set Warrior Position
func setPosition(position: SCNVector3) { self.node.position = position }
// Normal Idle
enum IdleType: Int {
case NeutralIdle
case DwarfIdle // observe Fingers
case LookAroundIdle
}
// Normal Idles
func idle(type: IdleType) {
isIdle = true // also sets all walking and running variabled to false
var animationName : String = ""
var key : String = ""
switch type {
case .NeutralIdle: animationName = aniMAT_NeutralIdle ; key = aniKEY_NeutralIdle // ; print("NeutralIdle ")
case .DwarfIdle: animationName = aniMAT_DwarfIdle ; key = aniKEY_DwarfIdle // ; print("DwarfIdle ")
case .LookAroundIdle: animationName = aniMAT_LookAroundIdle ; key = aniKEY_LookAroundIdle // ; print("LookAroundIdle")
}
makeAnimation(animationName, key, self.animNode, backwards: false, once: false, speed: 1.0, blendIn: 0.5, blendOut: 0.5)
}
func idleRandom() {
switch Int.random(in: 1...3) {
case 1: self.idle(type: .NeutralIdle)
case 2: self.idle(type: .DwarfIdle)
case 3: self.idle(type: .LookAroundIdle)
default: break
}
}
// MARK: Private Functions
// Common Animation Function
private func makeAnimation(_ fileName : String,
_ key : String,
_ node : SCNNode,
backwards : Bool = false,
once : Bool = true,
speed : CGFloat = 1.0,
blendIn : TimeInterval = 0.2,
blendOut : TimeInterval = 0.2,
removedWhenComplete : Bool = true,
fillForward : Bool = false
)
{
let anim = SCNAnimationPlayer.loadAnimation(fromSceneNamed: fileName)
if once { anim.animation.repeatCount = 0 }
anim.animation.autoreverses = false
anim.animation.blendInDuration = blendIn
anim.animation.blendOutDuration = blendOut
anim.speed = speed; if backwards {anim.speed = -anim.speed}
anim.stop()
print("duration: \(anim.animation.duration)")
anim.animation.isRemovedOnCompletion = removedWhenComplete
anim.animation.fillsForward = fillForward
anim.animation.fillsBackward = false
// Attach Animation
node.addAnimationPlayer(anim, forKey: key)
node.animationPlayer(forKey: key)?.play()
}
}
you can then initialise the Class Object after you initialised the characters struct.
the rest you'll figure out, come back on me, if you have questions or need a complete example App :)
I am trying to detect if one of the nodes that I have made through a subclass of SKNode (called Achievements) exists and if it doesn't exist then i'm trying to turn off a boolean variable.
What I use to locate the SKShapeNode (called "Indicator")
func checkIndicatorStatus() {
moveableArea.enumerateChildNodes(withName: "Achievement") {
(node, stop) in
let Indicate = node
Indicate.enumerateChildNodes(withName: "Indicator") {
node, stop in
if let Achievement = node as? Achievements {
menuAchieveNotificationOn = false
}
}
}
}
I have enumerated through the nodes specifically and tried searching for it but it doesn't seem to do anything. what am I doing wrong?
Here is my subclass. I have many of them named achievement displayed in my scene.
class Achievements: SKNode {
//Nodes used throughout the SKNode class
var achievementLabel = SKLabelNode()
var achievementTitleLabel = SKLabelNode()
var achievementNode = SKSpriteNode()
var notifyCircle = SKShapeNode()
//Amount Variables used as Achievement Properties
var image: String = ""
var information: String = ""
var title: String = ""
var amount = 0
var neededAmount = 0
var notification:Bool = false
var stage = 0
func getachievementData(AchName: String) {
let getDataRequest:NSFetchRequest<Achievement> = Achievement.fetchRequest()
getDataRequest.predicate = NSPredicate(format: "theSearchName == %#" , AchName)
do {
let searchResults = try CoreDatabaseContoller.getContext().fetch(getDataRequest)
//print("number of results: \(searchResults.count)")
for result in searchResults as [Achievement] {
title = result.theName!
information = result.theDescription!
image = result.theImage!
amount = Int(result.aAmount!)
neededAmount = Int(result.aNeededAmount!)
stage = Int(result.aStage!)
if result.aHasBeenAchieved!.intValue == 1 {
notification = true
}
}
}
catch {
print("ERROR: \(error)")
}
createAchievement()
}
func createAchievement() {
let tex:SKTexture = SKTexture(imageNamed: image)
achievementNode = SKSpriteNode(texture: tex, color: SKColor.black, size: CGSize(width: 85, height: 85)) //frame.maxX / 20, height: frame.maxY / 20))
achievementNode.zPosition = -10
achievementSprites.append(achievementNode)
self.name = "Achievement"
self.addChild(achievementNode)
self.zPosition = -11
createAchievementLabels()
}
func createAchievementLabels() {
achievementTitleLabel = SKLabelNode(fontNamed: "Avenir-Black")
achievementTitleLabel.fontColor = UIColor.black;
achievementTitleLabel.fontSize = 15 //self.frame.maxY/30
achievementTitleLabel.position = CGPoint (x: 0, y: 50)
achievementTitleLabel.text = "\(title)"
achievementTitleLabel.zPosition = -9
addChild(achievementTitleLabel)
achievementLabel = SKLabelNode(fontNamed: "Avenir-Black")
achievementLabel.fontColor = UIColor.black;
achievementLabel.fontSize = 13 //self.frame.maxY/30
achievementLabel.position = CGPoint (x: 0, y: -55)
achievementLabel.text = ("\(amount) / \(neededAmount)")
achievementLabel.zPosition = -9
addChild(achievementLabel)
if notification == true {
notifyCircle = SKShapeNode(circleOfRadius: 10)
notifyCircle.fillColor = .red
notifyCircle.position = CGPoint(x: 30 , y: 35)
notifyCircle.zPosition = 1000
notifyCircle.name = "Indicator"
addChild(notifyCircle)
}
}
}
EDIT 1
As you can see from the image below, there are a number of different achievement nodes each with their individual names and unlock criteria, when an achievement becomes unlocked it makes an indicator and changes the graphic, which is seen for the two X nodes here with the coloured red circle in the top right corners of each of them which is the "indicator" (if you look at the subclass at the bottom of it the creation of the indicator is there)
Now as you can seen the big red button in the bottom right hand corner of the picture is the achievement menu button which also has an indicator which is controlled by the bool variable (menuAchieveNotificationOn) what i'm trying to achieve is once the achievements with the indicators have each been pressed they are removed from the node.
what i'm trying to do is search each of the nodes to see if the indicator still exists if not I want to turn the variable (menuAchieveNotificationOn) to false.
You should be able to use this:
if let indicatorNode = childNode(withName: "//Indicator") as! SKShapeNode? {
menuAchieveNotificationOn = false
} else {
menuAchieveNotificationOn = true
}
EDIT: to run some code for EVERY "indicator" node in the scene. If any are found, achievementsFound is set to true:
achievementsFound = false
enumerateChildNodes(withName: "//Indicator") { indicatorNode, _ in
// Do something with indicatorNode
achievementsFound = true
}
Although this seems too simple, so I might have misunderstood your aim.
I have a class ChessSquare with its own attributes and methods. I have another class ChessBoard, which has an attribute of dictionary type.
class ChessBoard {
var squares: [ Int : ChessSquare ] = [:]
.....
One of the methods of the ChessBoard class builds the chessSquare object and assigns it to chessBoard object as below.
self.squares[squareName] = chessSquare
Before Swift3 this code was working fine. After upgrading to Swift3 the assignment stopped working.
Using breakpoints I see that chessSquare objects are built as expected. squareName variable has the expected value. The "self", which is the chessBoard object has proper initial values. But the dictionary assignment gets skipped without any error in the above mentioned code.
Is this anything related to Swift3. I browsed and could not get specific solution to my case.
Adding more code for clarity.
class ChessSquare {
let squareColor: UIColor
let squareShade: Int
let squareName: Int
let squareSize: CGSize
let minXY: CGPoint
let maxXY: CGPoint
let squareOrigin: CGPoint
var hasPiece = false
weak var chessPiece: ChessPiece?
let squareSprite: SKSpriteNode
let squareType: SquareType
//more data members
init(squareColor: UIColor, squareShade: Int, squareName: Int, squareSize: CGSize, minXY: CGPoint, maxXY: CGPoint, squareOrigin: CGPoint, squareSprite: SKSpriteNode, squareType: SquareType) {
self.squareColor = squareColor
self.squareShade = squareShade
self.squareName = squareName
self.squareSize = squareSize
self.minXY = minXY
self.maxXY = maxXY
self.squareOrigin = squareOrigin
self.squareSprite = squareSprite
self.squareType = squareType
}
//more methods
}
class ChessBoard {
var squares: [ Int : ChessSquare ] = [:]
var byteBoard: [UInt8] = [UInt8](repeating: UInt8(0), count: 66)
var byteBoardHashVal: Int = 0
// [ byteBoardHashVal : ((byteBoard, evalnVal), repCnt) ]
var byteBoards: [Int : (([UInt8],Double?), Int)] = [:]
var TT: [Int : Double] = [:]
var TTReuseCount = 0
var whitePawnlessFileByte: UInt8 = 255
var blackPawnlessFileByte: UInt8 = 255
var maxXY: CGPoint = CGPoint()
var minXY: CGPoint = CGPoint()
var oldTouchedSquare: ChessSquare?
init() {
squares = [:]
byteBoard = [UInt8](repeating: UInt8(0), count: 66)
byteBoardHashVal = 0
byteBoards = [:]
TT = [:]
TTReuseCount = 0
whitePawnlessFileByte = 255
blackPawnlessFileByte = 255
}
func drawFlippedChessBoard(_ view: SKView, scene: GameScene) {
....
....
for row in 0..<8 {
for col in 0..<8 {
let squareName = row * 8 + col
....
let chessSquare = ChessSquare(squareColor: currentColor, squareShade: squareShade, squareName: squareName, squareSize: squareSize, minXY: minXY, maxXY: maxXY, squareOrigin: squareOrigin, squareSprite: squareSprite, squareType: squareType)
....
self.squares[squareName] = chessSquare
....
}
....
}
....
}
//more methods
}
I have a difficulty in understanding if squareName is a string or a integer. It sounds like a string, I would suggest to name it chessSquareIndex or squareIndex if in case it is Int and you want to index chess square.
Based on your comment above I did a small test to verify that it works correctly if squareName is Int and here is the result,
class ChessSquare { }
class ChessBoard {
var squares: [ Int : ChessSquare ] = [:]
func getChessSquare(at squareIndex: Int) {
let chessSquare = ChessSquare()
return squares[squareIndex] = chessSquare
}
}
And this seems to work fine with Swift 3. Also notice that it is not required to call self, unless you are inside escaping closures.
I'm trying to implement a simple scoring system into my game using this tutorial as a reference:
http://www.raywenderlich.com/87232/make-game-like-mega-jump-sprite-kit-swift-part-2
The problem is, if I try to implement as is, it crashes in the GameScene.swift on this line:
let another = whichNode as! GameObjectNode
Here are the main parts of the code where the player collects the coins. I can also invite you to my repo if you'd like to take a closer and better look. I know it can be hard from looking at the code I pasted up here.
GameObjectNode.swift:
enum CoinType: Int {
case Normal = 0
case Special
}
struct CollisionCategoryBitmask {
static let Player: UInt32 = 0x00
static let Coin: UInt32 = 0x01
static let Platform: UInt32 = 0x02
}
class GameObjectNode: SKNode {
func collisionWithPlayer(player: SKNode) -> Bool {
return false
}
func checkNodeRemoval(playerY: CGFloat) {
if playerY > self.position.y + 300.0 {
self.removeFromParent()
}
}
}
class CoinNode: GameObjectNode {
let coinSound = SKAction.playSoundFileNamed("StarPing.wav", waitForCompletion: false)
var coinType: CoinType!
override func collisionWithPlayer(player: SKNode) -> Bool {
// Boost the player up
player.physicsBody?.velocity = CGVector(dx: player.physicsBody!.velocity.dx, dy: 400.0)
// Play sound
runAction(coinSound, completion: {
// Remove this Star
self.removeFromParent()
})
// Award score
GameState.sharedInstance.score += (coinType == .Normal ? 20 : 100)
// Award stars
GameState.sharedInstance.coins += (coinType == .Normal ? 1 : 5)
// The HUD needs updating to show the new stars and score
return true
}
}
GameState.swift
class GameState {
var score: Int
var highScore: Int
var coins: Int
init() {
// Init
score = 0
highScore = 0
coins = 0
// Load game state
let defaults = NSUserDefaults.standardUserDefaults()
highScore = defaults.integerForKey("highScore")
coins = defaults.integerForKey("coins")
}
func saveState() {
// Update highScore if the current score is greater
highScore = max(score, highScore)
// Store in user defaults
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setInteger(highScore, forKey: "highScore")
defaults.setInteger(coins, forKey: "coins")
NSUserDefaults.standardUserDefaults().synchronize()
}
class var sharedInstance: GameState {
struct Singleton {
static let instance = GameState()
}
return Singleton.instance
}
}
And the GameScene.swift:
import SpriteKit
import CoreMotion
import GameplayKit
struct PhysicsCategory {
static let None: UInt32 = 0
static let Player: UInt32 = 0b1 // 1
static let PlatformNormal: UInt32 = 0b10 // 2
static let PlatformBreakable: UInt32 = 0b100 // 4
static let CoinNormal: UInt32 = 0b1000 // 8
static let CoinSpecial: UInt32 = 0b10000 // 16
static let Edges: UInt32 = 0b100000 // 32
}
class GameScene: SKScene, SKPhysicsContactDelegate {
// Other Properties
...
var player: SKSpriteNode!
// HUD
var hudNode: SKNode!
var lblScore: SKLabelNode!
var lblCoins: SKLabelNode!
override func didMoveToView(view: SKView) {
....
// HUD
hudNode = SKNode()
hudNode.zPosition = 1000
cameraNode.addChild(hudNode)
// Coins
let coin = SKSpriteNode(imageNamed: "powerup05_1")
coin.position = convertPoint(CGPoint(x: 300, y: self.size.height-100), toNode: cameraNode)
coin.zPosition = 1000
hudNode.addChild(coin)
lblCoins = SKLabelNode(fontNamed: "ChalkboardSE-Bold")
lblCoins.fontSize = 70
lblCoins.fontColor = SKColor.whiteColor()
lblCoins.position = convertPoint(CGPoint(x: 375, y: self.size.height-100), toNode: cameraNode)
lblCoins.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.Left
lblCoins.zPosition = 1000
lblCoins.text = String(format: "X %d", GameState.sharedInstance.coins)
hudNode.addChild(lblCoins)
// Score
// 4
lblScore = SKLabelNode(fontNamed: "ChalkboardSE-Bold")
lblScore.fontSize = 70
lblScore.fontColor = SKColor.whiteColor()
lblScore.position = convertPoint(CGPoint(x: self.size.width-325, y: self.size.height-100), toNode: cameraNode)
lblScore.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.Right
lblScore.zPosition = 1000
lblScore.text = "0"
hudNode.addChild(lblScore)
}
func setupNodes() {
...
player = fgNode.childNodeWithName("Player") as! SKSpriteNode
}
func createStarAtPosition(position: CGPoint, ofType type: CoinType) -> CoinNode {
// 1
let node = CoinNode()
let thePosition = CGPoint(x: position.x * scaleFactor, y: position.y)
node.position = thePosition
node.name = "NODE_COIN"
// 2
node.coinType = type
var sprite: SKSpriteNode
if type == .Special {
sprite = SKSpriteNode(imageNamed: "CoinSpecial")
} else {
sprite = SKSpriteNode(imageNamed: "Coin")
}
node.addChild(sprite)
// 3
node.physicsBody = SKPhysicsBody(circleOfRadius: sprite.size.width / 2)
// 4
node.physicsBody?.dynamic = false
node.physicsBody?.categoryBitMask = CollisionCategoryBitmask.Coin
node.physicsBody?.collisionBitMask = 0
node.physicsBody?.contactTestBitMask = 0
return node
}
func didBeginContact(contact: SKPhysicsContact) {
let other = contact.bodyA.categoryBitMask == PhysicsCategory.Player ? contact.bodyB : contact.bodyA
var updateHUD = false
let whichNode = (contact.bodyA.node != player) ? contact.bodyA.node : contact.bodyB.node
// Code crashes here
let another = whichNode as! GameObjectNode
updateHUD = another.collisionWithPlayer(player)
if updateHUD {
lblCoins.text = String(format: "X %d", GameState.sharedInstance.coins)
lblScore.text = String(format: "%d", GameState.sharedInstance.score)
}
switch other.categoryBitMask {
case PhysicsCategory.CoinNormal:
if let coin = other.node as? SKSpriteNode {
emitParticles("CollectNormal", sprite: coin)
jumpPlayer()
runAction(soundCoin)
}
case PhysicsCategory.CoinSpecial:
if let coin = other.node as? SKSpriteNode {
emitParticles("CollectSpecial", sprite: coin)
boostPlayer()
runAction(soundBoost)
}
case PhysicsCategory.PlatformNormal:
if let platform = other.node as? SKSpriteNode {
if player.physicsBody!.velocity.dy < 0 {
platformAction(platform, breakable: false)
jumpPlayer()
runAction(soundJump)
}
}
case PhysicsCategory.PlatformBreakable:
if let platform = other.node as? SKSpriteNode {
if player.physicsBody!.velocity.dy < 0 {
platformAction(platform, breakable: true)
jumpPlayer()
runAction(soundBrick)
}
}
default:
break;
}
}
I dont understand the code you use in didBeganContact, but you can define the contact bodies in this way:
enum Ctg:UInt32
{
case Coin = 1
case Hero = 2
case Villain = 4
case Car = 8
}
var hero = SKSpriteNode()
hero.physicsBody = SKPhysicsBody(rectangleOfSize: hero.size)
hero.physicsBody?.categoryBitMask = Ctg.Hero.rawValue
hero.physicsBody?.contactTestBitMask = Ctg.Coin.rawValue | Ctg.Villian.rawValue
var coin = SKSpriteNode()
coin.physicsBody = SKPhysicsBody(rectangleOfSize: coin.size)
coin.physicsBody?.categoryBitMask = Ctg.Coin.rawValue
coin.physicsBody?.contactTestBitMask = Ctg.Hero.rawValue
func didBeginContact(contact: SKPhysicsContact)
{
var first = SKNode()
var sec = SKNode()
// this way you ensure that the first body is the most valuable Ctg (enum)
if contact.bodyA.node?.physicsBody?.categoryBitMask > contact.bodyB.node?.physicsBody?.categoryBitMask
{
first = contact.bodyA.node!
sec = contact.bodyB.node!
}
else
{
first = contact.bodyB.node!
sec = contact.bodyA.node!
}
// this part be sure that the category of first it is of most value that sec
if first.physicsBody!.categoryBitMask == Ctg.Hero.rawValue && sec.physicsBody!.categoryBitMask == Ctg.Coin.rawValue
{
hero.coins++
scene.labelCoins.text = String(coins)
}
if first.physicsBody!.categoryBitMask == Ctg.Villain.rawValue && sec.physicsBody!.categoryBitMask == Ctg.Hero.rawValue
{
gameOver = true
hero.removeFromParent()
lostGame()
}
}
I have a class that creates variables for multiple SpriteNods which looks like this:
class Enemy {
var speed:Float = 0.0
var guy:SKSpriteNode
var currentFrame = 0
var randomFrame = 0
var moving = false
var rotationSpeed:CGFloat = 1.0
var angle = 0.0
var range = 1.2
var yPos = CGFloat()
var rotationDirection:Int = 0
var preLocation:CGFloat = 0
var health:Int = 0
init(speed:Float, guy:SKSpriteNode, rotationSpeed:CGFloat, rotationDirection:Int, preLocation:CGFloat, health:Int) {
self.speed = speed
self.guy = guy
self.rotationSpeed = rotationSpeed
self.rotationDirection = rotationDirection
self.preLocation = preLocation
self.health = health
}
A SpriteNode is then applied to the Enemy like this:
func addEnemy(#named: String, speed:Float, yPos: CGFloat, rotationSpeed:CGFloat, rotationDirection:Int, preLocation:CGFloat, health:Int) {
var enemyNode = SKSpriteNode(imageNamed: named)
enemyNode.physicsBody = SKPhysicsBody(texture: enemyNode.texture, alphaThreshold: 0, size: enemyNode.size)
enemyNode.physicsBody!.affectedByGravity = false
enemyNode.physicsBody!.categoryBitMask = ColliderType.Enemy.rawValue
enemyNode.physicsBody!.contactTestBitMask = ColliderType.Hero.rawValue | ColliderType.Enemy.rawValue
enemyNode.physicsBody!.collisionBitMask = ColliderType.Hero.rawValue | ColliderType.Enemy.rawValue
enemyNode.physicsBody?.allowsRotation = false
var enemy = Enemy(speed: speed, guy: enemyNode, rotationSpeed: rotationSpeed, rotationDirection: rotationDirection, preLocation: preLocation, health: health)
enemys.append(enemy)
enemy.guy.name = named
resetEnemy(enemyNode, yPos: yPos)
enemy.yPos = enemyNode.position.y
addChild(enemyNode)
}
Now i have the problem that in the didBeginContact function i only get the SpriteNodes back so i can't get to the enemy specific variables, like health.
So i want to know how i can reference the enemy class to the spritenode, so i can use it in the didBeginContact function.
I hope someone can help me. Thanks.
I just ran into this exact problem. You need to cast the node as the object you are looking for. In didBeginContact it would be something like this:
if let myEnemy = contact.bodyA.node as? Enemy {
myEnemy.speed = 99
}