Reputation: 668
I know that's likely a stupid question, but I'm trying for several hours already and can't do it. So, I'm making a simple game. I have 4 functions for death of my hero. Each of them is performing a different animation so I want program to know who exactly killed a hero. My logic is to put that functions into the array but Xcode tells me that I have a "missing argument for parameter #1 in call". I ignored that and tried to make a switch statement. But every 'case' has an error that tells me that it's expecting a pattern. Here is my code:
func didBeginContact(contact: SKPhysicsContact) {
dead()
}
var deathArray:Array = [eatenByMouse(), eatenByCat(), eatenByHamster(), eatenByRabbit()]
func dead() {
switch deathArray {
case :
eatenByMouse()
case:
eatenByCat()
case:
eatenByHamster()
case:
eatenByRabbit()
default:
cookie
}
}
It's probably an extremely simple problem but I'm new to this and don't know what to do.
I think it's a good idea to post more related samples of my code. I think, my problem could be in collisions. So I have a ColliderType:
enum ColliderType:UInt32 {
case Cookie = 1
case Rabbit = 2
case Mouse = 3
case Hamster = 4
case Cat = 5
}
Then I'm making a physic bodies for hero and enemies:
self.cookie.physicsBody = SKPhysicsBody(circleOfRadius: CGFloat(self.cookie.size.width / 2))
self.cookie.physicsBody?.affectedByGravity = false
self.cookie.physicsBody?.categoryBitMask = ColliderType.Cookie.rawValue
self.cookie.physicsBody?.collisionBitMask = ColliderType.Mouse.rawValue
self.cookie.physicsBody?.contactTestBitMask = ColliderType.Mouse.rawValue
self.mouse.physicsBody = SKPhysicsBody(rectangleOfSize: self.mouse.size)
self.mouse.physicsBody?.dynamic = false
self.mouse.physicsBody?.categoryBitMask = ColliderType.Mouse.rawValue
self.mouse.physicsBody?.contactTestBitMask = ColliderType.Cookie.rawValue
self.mouse.physicsBody?.collisionBitMask = ColliderType.Cookie.rawValue
Sample of eatenByMouse() function:
func eatenByMouse() {
self.groundSpeed = 0
self.cookie.hidden = true
let animateAction = SKAction.animateWithTextures(self.mouseArray, timePerFrame: 0.1)
self.mouse.runAction(animateAction)
}
I hope it will clear something maybe. It seems to be so easy problem, I'm ashamed that I don't understand it.
Upvotes: 0
Views: 1382
Reputation: 916
I'm assuming you can determine the type of death in didBeginContact
. So you need to determine the index for the type of death here and then pass that to the death
function so it can find the function reference and execute it.
If your death type functions take arguments or return a different result you will need to change the type of the array accordingly.
func didBeginContact(contact: SKPhysicsContact) {
// You will need to determine this index based on the type of contact/death.
var deathIndex:Int = 0 // Index of the type of death (e.g. eatenByMouse in this case)
dead(deathIndex)
}
var deathArray:Array<() -> ()> = [eatenByMouse, eatenByCat, eatenByHamster, eatenByRabbit]
func dead(deathIndex: Int) {
deathArray[deathIndex]()
}
Upvotes: 1
Reputation: 7361
On line 5, you're defining your array of functions:
[ eatenByMouse(), eatenByCat(), ... ]
Those expressions with the ()
syntax actually invoke the function and resolve to it's return value. Try by just using the function names but not invoking them.
[ eatenByMouse, eatenByCat, ... ]
Upvotes: 2