-
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
-
Senior Member
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!
-
5+5=55
Originally Posted by Jephz
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).
-
Senior Member
Originally Posted by Schfifty Five
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.
-
Untitled-1.fla
Originally Posted by Schfifty Five
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.
-
5+5=55
Originally Posted by strille
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
-
Thank you Pazil, I will try that. Meanwhile, if anyone else has any bright ideas, please tell me!
-
5+5=55
Originally Posted by Jephz
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|