00:00
00:00
Newgrounds Background Image Theme

Masterbaiter60 just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

AS3 if else hierarchy

395 Views | 3 Replies
New Topic Respond to this Topic

AS3 if else hierarchy 2015-10-10 20:42:19


I have an error that I can't seem to work around. I have tried to re-structure this code a few different ways, but I am missing something fundamental.
I have also tried to read up on if else statements, and switches to look for other options.

The player begins in the 'Start' screen. If he moves far enough left, the scene transitions to 'leftScene'. He is moved to the right side of the screen, and the BG changes to make it appear he has moved.

function sceneTransition(e:Event):void{
	if (player.x <= -10){
		if (mainBG.FrameLabel = 'Start'){
			player.x = 650;
			mainBG.gotoAndPlay('leftScene');
		}
		else if (mainBG.FrameLabel = 'rightScene'){
			player.x = 650;
			mainBG.gotoAndPlay('Start');
		}
	}
	else if (player.x >= 670){
		if (mainBG.FrameLabel = 'Start'){
			player.x = 10;
			mainBG.gotoAndPlay('rightScene');
		}
		else if (mainBG.FrameLabel = 'leftScene'){
			player.x = 10;
			mainBG.gotoAndPlay('Start');
		}
	}
}

As is, he can walk to 'leftScene', but when he tried to return to 'Start', it skips ahead to 'rightScene'. Then if he does left again, it skips 'Start' once more, and goes straight to 'leftScene'.
I think I know the problem. But am not sure how to fix it. I think his co-ordinates get checked by all segments of the code simultaneously. So in that exact instance he satisfies the code to move him to 'Start' and to 'rightScene'. Because of this, 'Start' gets skipped every time.

How can I avoid this?
Thank you!


GAH!

BBS Signature

Response to AS3 if else hierarchy 2015-10-10 21:12:16


At 10/10/15 08:42 PM, Noodle wrote: As is, he can walk to 'leftScene', but when he tried to return to 'Start', it skips ahead to 'rightScene'.

That's because you're using the assignment operator in your if-statements, and not the equality operator; a single equals sign is the assignment operator and two equals signs is the equality operator. The reason that behaviour is happening is because when assigning a value, the value is returned after being assigned and that's what ends up being evaluated in the if-statement, which, in this case, is the string "Start" and that evaluates to true because any non-empty string will always evaluate to true.

Response to AS3 if else hierarchy 2015-10-10 22:09:33


At 10/10/15 09:12 PM, Diki wrote:
At 10/10/15 08:42 PM, Noodle wrote: As is, he can walk to 'leftScene', but when he tried to return to 'Start', it skips ahead to 'rightScene'.
That's because you're using the assignment operator in your if-statements, and not the equality operator; a single equals sign is the assignment operator and two equals signs is the equality operator. The reason that behaviour is happening is because when assigning a value, the value is returned after being assigned and that's what ends up being evaluated in the if-statement, which, in this case, is the string "Start" and that evaluates to true because any non-empty string will always evaluate to true.

That didn't quite fix the problem. That makes a lot of sense, but making that change didn't quite fix things. Although it was a step in the right direction!

I found out that mainBG.FrameLabel wasn't the proper operation to use in that scenario. Instead it now reads as

function sceneTransition(e:Event):void{
	if (mainBG.currentLabel == 'Start'){

And now things are peachy-keen :)
Thank you for all of your help lately!


GAH!

BBS Signature

Response to AS3 if else hierarchy 2015-10-10 23:10:43


At 10/10/15 10:09 PM, Noodle wrote: I found out that mainBG.FrameLabel wasn't the proper operation to use in that scenario. Instead it now reads as

function sceneTransition(e:Event):void{
if (mainBG.currentLabel == 'Start'){

Ah, right. I only quickly skimmed your code and missed that—would definitely explain it, though.

At 10/10/15 10:09 PM, Noodle wrote: Thank you for all of your help lately!

You're welcome; happy to help.