0

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.

5
  • That switch deathArray does not look right to me (actually the whole switch statement looks funny). It looks like you want to provide an index into the deathArray to run the corresponding action, but I don't see any reference to an index value. Commented Mar 12, 2015 at 20:49
  • Well, I thought that it is the best way to loop thru 4 functions. I have no index for that functions. Obviously I was wrong. But I still don't know how to do it! Ive no index for that functions. Commented Mar 12, 2015 at 20:55
  • How do you determine how your hero died (i.e. type of death)? Commented Mar 12, 2015 at 20:58
  • didBeginContact = death in my case if I understood you. Hero dies if he touches one of four enemies. Commented Mar 12, 2015 at 21:02
  • I updated the code, maybe it will help more. That's how I determine death of my hero Commented Mar 13, 2015 at 18:36

2 Answers 2

2

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, ... ]
Sign up to request clarification or add additional context in comments.

1 Comment

Oh, thanks for that! It removed my first problem. But what about patterns?
1

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]()
}   

5 Comments

Full disclosure I don't have access to a compiler right now so I may have a syntax error, I have not used much Swift before.
So I tried this. The only problem I have now is in the deathArray[deathIndex]() It says that it's an 'Array' and it's not convertible to 'Array<T>'.
For some reason, as I understood, it doesn't like accessing functions by Integers. Maybe I'm wrong...
I've added the function type to the array
Thanks! But it didn't worked. Now it's showing me an error "() is not a subtype of 'PlayScene'". That's my name for that scene.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.