name in if statement?
- tcc85811
-
tcc85811
- Member since: Oct. 12, 2008
- Offline.
-
- Forum Stats
- Member
- Level 03
- Blank Slate
I can have up to 4 enemies on the screen at once, and each one enters the screen from their own corner.
I'm trying to place them in their respective corners by putting the following code in the onClipEvent(load) area:
if(name == E1){
_y = 150;
_x = Stage.width + 150;
}
if(name == E2){
_y = 250;
_x = Stage.width + 150;
}
if(name == E3){
_y = 150;
_x = -150;
}
if(name == E4){
_y = 250;
_x = -150;
}
When I do this, all 4 enemies get placed in the E4 position, even though they all have the correct names.
This problem gets fixed by placing 'else' between the if statements, but WHY? There should be no need for an 'else' anywhere in this code. Why do they all follow the last if statement?
- Denvish
-
Denvish
- Member since: Apr. 25, 2003
- Offline.
-
- Send Private Message
- Browse All Posts (15,977)
- Block
-
- Forum Stats
- Member
- Level 46
- Blank Slate
At 3/16/09 11:31 AM, tcc85811 wrote: I can have up to 4 enemies on the screen at once, and each one enters the screen from their own corner.
I'm trying to place them in their respective corners by putting the following code in the onClipEvent(load) area:
if(name == E1){
_y = 150;
_x = Stage.width + 150;
}
if(name == E2){
_y = 250;
_x = Stage.width + 150;
}
if(name == E3){
_y = 150;
_x = -150;
}
if(name == E4){
_y = 250;
_x = -150;
}
When I do this, all 4 enemies get placed in the E4 position, even though they all have the correct names.
This problem gets fixed by placing 'else' between the if statements, but WHY? There should be no need for an 'else' anywhere in this code. Why do they all follow the last if statement?
If you're trying to grab the instance's _name then you need to use
if(_name=="E1"){
If 'name' is your own variable, then you're looking for trouble, because both 'name' and _name' are reserved by Flash. Use 'nam' or something instead.
In terms of the else, it's actually more efficient in this case to useif else rather thanif in this case, because if the first condition is true, it won't have to check the others
- GodlyKira
-
GodlyKira
- Member since: Dec. 21, 2008
- Offline.
-
- Forum Stats
- Member
- Level 05
- Blank Slate
At 3/16/09 11:38 AM, Denvish wrote: If you're trying to grab the instance's _name then you need to use
if(_name=="E1"){
If 'name' is your own variable, then you're looking for trouble, because both 'name' and _name' are reserved by Flash. Use 'nam' or something instead.
In terms of the else, it's actually more efficient in this case to useif else rather thanif in this case, because if the first condition is true, it won't have to check the others
Just because somethings reserved by flash doesnt mean itll cause problems, unless treated as a method... as long as name is defined, to be honest in that code name is probably a parameter, cause thats how ide do it...


