0

I'm trying to make simple collision detection using the switch statement. The statement takes the y position of a certain object and then it detects if the object's x is the same as another object. The problem is that I have if statements inside of the cases it goes through the whole if statement, even if the condition isn't met. The object is supposed to be sent back to the starting point after it gets hit by an enemy (enemies are moving through the screen) and moves in increments of 19 so the case is met. The problem is I can't seem to move it up because it keeps getting sent back to the starting spot whenever I meet the case (y = 221). When I remove deadObject() from the switch statement, it works fine and only prints "dead" when it's running into an enemy. How can I go about to fixing this?

I've been looking around on the internet about this but I can't find anything AS3 related to if statements in switch statements. Thanks for any help

public function enterFrame(me:Event)
    {
        position = objectA.y
        switch(position)
        {
            case 221:
                if (objectA.x == enemy1.x || objectA.x == enemy1.x + 10)
                    trace("dead1");
                    deadObject()
                if (objectA.x == enemy2.x || objectA.x == enemy2.x + 10)
                    trace("dead1");
                    deadObject()
                if (objectA.x == enemy3.x || objectA.x == enemy3.x + 10)
                    trace("dead1");
                    deadObject()
                break;
          }
     }

public function deadObject()
    {
        objectA.x = 133;
        objectA = 240;
    }
pamphlet
  • 2,054
  • 1
  • 17
  • 27
Bob
  • 15
  • 3

1 Answers1

2

I believe this is a simple syntax problem. Your interpreter is smart enough to know that the trace statement belongs to the if statement but now you throw in another function and it gets confused.

My advice would be to always follow best practice and put brackets where they belong

Please also see the following: Is it bad practice to use an if-statement without brackets?

    public function enterFrame(me:Event)
    {
        position = objectA.y;
        switch(position)
        {
            case 221:
                if (objectA.x == enemy1.x || objectA.x == enemy1.x + 10)
                {
                    trace("dead1");
                    deadObject();
                }
                if (objectA.x == enemy2.x || objectA.x == enemy2.x + 10)
                {
                    trace("dead1");
                    deadObject();
                }
                if (objectA.x == enemy3.x || objectA.x == enemy3.x + 10)
                {
                    trace("dead1");
                    deadObject();
                }
                break;
        }
   }
Community
  • 1
  • 1
James LeClair
  • 1,234
  • 10
  • 12
  • Thanks, and sorry I'm still pretty new to this stuff, I've been learning it on and off for a while but I've never really dived into it until recently so my syntax isn't too good – Bob Jun 17 '13 at 18:33
  • There's no need to apologize. We've all had trouble with syntax. If you don't struggle with at least something each day, you aren't programming hard enough. Keep it up and good luck with your game. – James LeClair Jun 17 '13 at 18:50