Be a Supporter!

name in if statement?

  • 180 Views
  • 2 Replies
New Topic Respond to this Topic
tcc85811
tcc85811
  • Member since: Oct. 12, 2008
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
name in if statement? 2009-03-16 11:31:08 Reply

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.
Forum Stats
Member
Level 46
Blank Slate
Response to name in if statement? 2009-03-16 11:38:53 Reply

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


- - Flash - Music - Images - -

BBS Signature
GodlyKira
GodlyKira
  • Member since: Dec. 21, 2008
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to name in if statement? 2009-03-16 12:30:50 Reply

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...


BBS Signature