dcsimg
A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: Can this be done in a better way?

  1. #1
    Member
    Join Date
    Apr 2009
    Posts
    43

    Can this be done in a better way?

    Jephz here. I would like to know if there is a better way to code this other then a million if statements.

    For exameple:

    Code:
    if(_root.poisoned == true) {
    //do stuff
    }
    if(_root.stunned == true) {
    //do stuff
    }
    ........
    .........
    I can't use case statements , because it only switches one variable where as I need them all to be manipulated. Like, you can be "poisoned" and "stunned" at the same time.

    Just wondering, IS there a better way of coding this?

    -Jephz

  2. #2
    Senior Member Pazil's Avatar
    Join Date
    Sep 2006
    Location
    Ontario, Canada
    Posts
    913
    Instead of setting _root.poisoned = true, you could simply push a poisoned function into a states array:

    states.push(poisoned);

    If you need parameters, then:

    states.push([poisoned, param1, param2, etc]);

    To execute all states then, you can do:

    for (i=0;i<states.length;i++)
    {
    states[i][0](states[i][1], states[i][2]);
    }

    If all the state functions (such as poisoned and stunned) have the same amount of parameters, this will work. If they each have different ones, then you should make it that they shouldn't. For example, if you want poisons with different damage, and stuns with different period of time, and that also makes you lose a certain amount of health, just create Poison and Stun classes, and do:

    states.push( [poisoned, new Poison( damage, period, type), player]);

    And then the poisoned function would look like:

    private function poisoned( poison:Poison, target:Character)

    Anyways, that's the best way I could think of right now...

    P.
    WIP-ZOMBIES

    I love vegetarians! More meat for the rest of us!

  3. #3
    5+5=55 Schfifty Five's Avatar
    Join Date
    Jun 2006
    Posts
    698
    Quote Originally Posted by Jephz View Post
    Jephz here. I would like to know if there is a better way to code this other then a million if statements.

    For exameple:

    Code:
    if(_root.poisoned == true) {
    //do stuff
    }
    if(_root.stunned == true) {
    //do stuff
    }
    ........
    .........
    I can't use case statements , because it only switches one variable where as I need them all to be manipulated. Like, you can be "poisoned" and "stunned" at the same time.

    Just wondering, IS there a better way of coding this?

    -Jephz

    You can use a switch case to check multiple conditions, although it might seem like a weird way of doing it:

    Code:
    switch(true){
    	case _root.poisoned:
    		// statements
    	case _root.stunned:
    		// statements
    }
    Note that there's no break statements, so it will allow multiple conditions (like poisoned and stunned).

  4. #4
    Member
    Join Date
    Apr 2009
    Posts
    43
    Thank you Pazil, I will try that. Meanwhile, if anyone else has any bright ideas, please tell me!

  5. #5
    5+5=55 Schfifty Five's Avatar
    Join Date
    Jun 2006
    Posts
    698
    Quote Originally Posted by Jephz View Post
    Thank you Pazil, I will try that. Meanwhile, if anyone else has any bright ideas, please tell me!
    Yeah, check what I said (just posting this to bump, since you might not have seen my post, as I said it right before you replied).

    Also, not really related to the topic, but you might want to keep those properties in an object of some sort (i.e. _root.status.poisioned, _root.status.stunned, etc..), since keeping everything as a property of _root can get messy.

    You might already be doing this anyway, and were just using _root as an example.

  6. #6
    Senior Member UnknownGuy's Avatar
    Join Date
    Jul 2003
    Location
    Canada
    Posts
    1,367
    Quote Originally Posted by Schfifty Five View Post
    Code:
    switch(true){
    	case _root.poisoned:
    		// statements
    	case _root.stunned:
    		// statements
    }
    If you are going to use case statements without breaks in them sometimes, make sure to VERY explicitly comment that, as that can be quite confusing for you later or to another programmer(who may think it is a mistake).

    I would generally not recommend using case without a break, because of the lack of clarity.

  7. #7
    Untitled-1.fla strille's Avatar
    Join Date
    Mar 2001
    Location
    Sweden
    Posts
    1,626
    Quote Originally Posted by Schfifty Five View Post
    You can use a switch case to check multiple conditions, although it might seem like a weird way of doing it:

    Code:
    switch(true){
    	case _root.poisoned:
    		// statements
    	case _root.stunned:
    		// statements
    }
    Note that there's no break statements, so it will allow multiple conditions (like poisoned and stunned).
    This will not work. If _root.poisoned equals true, then the stunned statements will be executed even if _root.stunned equals false.

  8. #8
    5+5=55 Schfifty Five's Avatar
    Join Date
    Jun 2006
    Posts
    698
    Quote Originally Posted by strille View Post
    This will not work. If _root.poisoned equals true, then the stunned statements will be executed even if _root.stunned equals false.

    Ahh, you're right, good call.
    I wasn't thinking.

    So yeah, try what Pazil suggested

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center