Skip to main content
Participant
October 17, 2018
Answered

Else If Statement is working MOSTLY as intended aside from 1 small error that I can't figure out

  • October 17, 2018
  • 1 reply
  • 620 views

So i'm rather new to this, and I'm not even sure if people still use AS 2.0 (lol), but i'll give this a shot.

I'm making a puzzle game in which the player's character moves on rails (you don't control it directly) but you guide their movement by setting up special directional arrows, portals, and other tools to help the character reach the end of the level.

I'm going crazy trying to figure out the problem with this code, when I test the game I get no errors and every aspect of the code works besides one little bug with the highlighted portion down below. It's really confusing to me, because it's copy and pasted from another line of code that IS working as intended.

The code is an array loop that checks if the player "guy" is hitting something that would change it's movement:

So it first checks if the guy is moving, and if that's true then I run through the loop, checking if there is a hittest and which Frame the object you hit is on. Frame 7 for example is a Right Arrow which if hit will cause the guy to move to the right. Frame 8 is a Down Arrow, causing the guy to move down, 9 is left, and 10 is up. You'll also notice that I tell the object you hit to gotoAndStop on the next frame. This is because I want this specific Arrow object to change direction whenever you hit it. This works as intended for all of the code besides the highlighted part. Sorry if this is confusing or unnecessary. Here's a picture to sort of give you an idea. The Golden Arrow is the direction you will move when the "guy" hits it. After that the Arrow's direction will rotate 90 degrees counterclockwise so you will move up instead of to the right.

So here's the code:

guy.onEnterFrame = function() {

if ((_root.MovingOn == 1)) {

for(var counter:Number = 1; counter < hbTiles.length; counter++){

if (_root.guy.hitTest(hbTiles[counter]) and (_root.MovingOn == 1) and (allTiles[counter]._currentFrame == 7)) {

_root.guy._x = hbTiles[counter]._x;

_root.guy._y = hbTiles[counter]._y;

_root.MovingUp = 0;

_root.MovingRight = 1;

_root.MovingDown = 0;

_root.MovingLeft = 0;

_root.guy._x += 14;

_root.allTiles[counter].gotoAndStop(8);

} else if (_root.guy.hitTest(hbTiles[counter]) and (_root.MovingOn == 1) and (allTiles[counter]._currentFrame == 8)) {

 

_root.guy._x = hbTiles[counter]._x;

_root.guy._y = hbTiles[counter]._y;

_root.MovingUp = 0;

_root.MovingRight = 0;

_root.MovingDown = 1;

_root.MovingLeft = 0;

_root.guy._y += 14;

_root.allTiles[counter].gotoAndStop(9);

} else if (_root.guy.hitTest(hbTiles[counter]) and (_root.MovingOn == 1) and (allTiles[counter]._currentFrame == 9)) {

_root.guy._x = hbTiles[counter]._x;

_root.guy._y = hbTiles[counter]._y;

_root.MovingUp = 0;

_root.MovingRight = 0;

_root.MovingDown = 0;

_root.MovingLeft = 1;

_root.guy._x -= 14;

_root.allTiles[counter].gotoAndStop(10);

} else if (_root.guy.hitTest(hbTiles[counter]) and (_root.MovingOn == 1) and (allTiles[counter]._currentFrame == 10)) {

 

_root.guy._x = hbTiles[counter]._x;

_root.guy._y = hbTiles[counter]._y;

_root.MovingUp = 1;

_root.MovingRight = 0;

_root.MovingDown = 0;

_root.MovingLeft = 0;

_root.guy._y -= 14;

_root.allTiles[counter].gotoAndStop(7);

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

} else if (_root.guy.hitTest(hbTiles[counter]) and (_root.MovingOn == 1) and (allTiles[counter]._currentFrame == 12)) {

_root.guy._x = hbTiles[counter]._x;

_root.guy._y = hbTiles[counter]._y;

_root.MovingUp = 0;

_root.MovingRight = 1;

_root.MovingDown = 0;

_root.MovingLeft = 0;

_root.guy._x += 14;

_root.allTiles[counter].gotoAndStop(13);

} else if (_root.guy.hitTest(hbTiles[counter]) and (_root.MovingOn == 1) and (allTiles[counter]._currentFrame == 13)) {

     

_root.guy._x = hbTiles[counter]._x;

_root.guy._y = hbTiles[counter]._y;

_root.MovingUp = 1;

_root.MovingRight = 0;

_root.MovingDown = 0;

_root.MovingLeft = 0;

_root.guy._y -= 14;

_root.allTiles[counter].gotoAndStop(14);

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

} else if (_root.guy.hitTest(hbTiles[counter]) and (_root.MovingOn == 1) and (allTiles[counter]._currentFrame == 14)) {

 

_root.guy._x = hbTiles[counter]._x;

_root.guy._y = hbTiles[counter]._y;

_root.MovingUp = 0;

_root.MovingRight = 0;

_root.MovingDown = 0;

_root.MovingLeft = 1;

_root.guy._x -= 14;

_root.allTiles[counter].gotoAndStop(15);

} else if (_root.guy.hitTest(hbTiles[counter]) and (_root.MovingOn == 1) and (allTiles[counter]._currentFrame == 15)) {

_root.guy._x = hbTiles[counter]._x;

_root.guy._y = hbTiles[counter]._y;

_root.MovingUp = 0;

_root.MovingRight = 0;

_root.MovingDown = 1;

_root.MovingLeft = 0;

_root.guy._y += 14;

_root.allTiles[counter].gotoAndStop(12);

}}}}

So all of that code works properly besides the highlighted part. As far as I can tell what is happening is that the Else If Statement is sort of being ignored there. It's running both lines of code, moving the "guy" to the right, then immediately moving him upwards. I was under the impression that only one of theseElse If Statements should ever be go off at a time. Also, I want to note that the reason why I move the "guy" (_root.guy._y += 14;) is because it pushes him off of the object he's hitting preventing it from looping infinitely. I'm really confused because it all works as intended for everything besides that one part.

This topic has been closed for replies.
Correct answer kglad

you can simplify that code and better see what's going on and make it easier to debug and maintain your code:

guy.onEnterFrame = function() {

if ((_root.MovingOn == 1)) {

for(var counter:Number = 1; counter < hbTiles.length; counter++){

if (_root.guy.hitTest(hbTiles[counter]) ){

switch (allTiles[counter]._currentFrame){

case 7:

allTiles7F(counter);

break;

case 8:

allTiles8F(counter);

break

etc

}

}

}

}

}

function allTiles7F(counter){

_root.guy._x = hbTiles[counter]._x;

_root.guy._y = hbTiles[counter]._y;

_root.MovingUp = 0;

_root.MovingRight = 1;

_root.MovingDown = 0;

_root.MovingLeft = 0;

_root.guy._x += 14;

_root.allTiles[counter].gotoAndStop(8);

}

function allTiles8F(counter){

_root.guy._x = hbTiles[counter]._x;

_root.guy._y = hbTiles[counter]._y;

_root.MovingUp = 0;

_root.MovingRight = 0;

_root.MovingDown = 1;

_root.MovingLeft = 0;

_root.guy._y += 14;

_root.allTiles[counter].gotoAndStop(9);

}

etc

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
October 17, 2018

you can simplify that code and better see what's going on and make it easier to debug and maintain your code:

guy.onEnterFrame = function() {

if ((_root.MovingOn == 1)) {

for(var counter:Number = 1; counter < hbTiles.length; counter++){

if (_root.guy.hitTest(hbTiles[counter]) ){

switch (allTiles[counter]._currentFrame){

case 7:

allTiles7F(counter);

break;

case 8:

allTiles8F(counter);

break

etc

}

}

}

}

}

function allTiles7F(counter){

_root.guy._x = hbTiles[counter]._x;

_root.guy._y = hbTiles[counter]._y;

_root.MovingUp = 0;

_root.MovingRight = 1;

_root.MovingDown = 0;

_root.MovingLeft = 0;

_root.guy._x += 14;

_root.allTiles[counter].gotoAndStop(8);

}

function allTiles8F(counter){

_root.guy._x = hbTiles[counter]._x;

_root.guy._y = hbTiles[counter]._y;

_root.MovingUp = 0;

_root.MovingRight = 0;

_root.MovingDown = 1;

_root.MovingLeft = 0;

_root.guy._y += 14;

_root.allTiles[counter].gotoAndStop(9);

}

etc

Participant
October 18, 2018

Thanks, that made things easier. I've solved the problem and it was unrelated to the code.

I'm curious though, how would I go about adding a second condition to let's say case 6.

This causes problems:

case 6:

if ((_root.MovingRight == 1)) {

allTiles6rF(counter);

break;

if ((_root.MovingLeft == 1)) {

allTiles6lF(counter);

break;

if ((_root.MovingUp == 1)) {

allTiles6uF(counter);

break;

if ((_root.MovingDown == 1)) {

allTiles6dF(counter);

break;

That causes all of the cases beyond 6 to stop working. However, the cases before that still work. I'm not too sure why.

kglad
Community Expert
Community Expert
October 18, 2018

case 6:

if ((_root.MovingRight == 1)) {

allTiles6rF(counter);

} else if ((_root.MovingLeft == 1)) {

allTiles6lF(counter);

} else if ((_root.MovingUp == 1)) {

allTiles6uF(counter);

} else if ((_root.MovingDown == 1)) {

allTiles6dF(counter);

}

break;