Skip to main content
Known Participant
April 30, 2014
Question

Switch statements executing at the same time

  • April 30, 2014
  • 2 replies
  • 569 views

The code checks for a collision and then switches between the six frame depending of the actual frame, if the actual frame is "noDamage" it goes to the next frame, ect.

public function onTick(timerEvent:TimerEvent):void

                    {

                              gameClock.addToValue(25);

                              if(Math.random()<0.01)

                              {

                                        var randomY:Number = Math.floor(Math.random() * 300) + 50;

                                        var newEnemy:Enemy = new Enemy(400,randomY);

                                        army.push(newEnemy);

                                        addChild(newEnemy);

                                        gameScore.addToValue(10);

                              }

                              blade.x = mouseX;

                              blade.y = mouseY;

                              var gadjetHasBeenHit:Boolean = false;

 

            for each(var enemy:Enemy in army)

                              {

                                        if(PixelPerfectCollisionDetection.isColliding(jail,enemy,this,true))

                                  {

                                                  var desiredFrame:String = "noDamage";

                                                  var desiredFrame1:String = "damage_1";

                                                  var desiredFrame2:String = "damage_2";

                                                  var desiredFrame3:String = "damage_3";

                                                  var desiredFrame4:String = "damage_4";

                                                  var desiredFrame5:String = "damage_5";

 

                                                  switch(jail.currentLabel)

                                                  {

                                                            case "noDamage":

                                                                jail.gotoAndStop(desiredFrame1);

                                                                break;

                                                            case "damage_1":

                                                                jail.gotoAndStop(desiredFrame2);

                                                                break;

                                                            case "damage_2":

                                                                jail.gotoAndStop(desiredFrame3);

                                                                break;

                                                            case "damage_3":

                                                                jail.gotoAndStop(desiredFrame4);

                                                                break;

                                                            case "damage_4":

                                                                jail.gotoAndStop(desiredFrame5);

                                                                break;

                                                     }

                                           trace("Enemy collided with the jail");

                                  }break;

}

This topic has been closed for replies.

2 replies

kglad
Community Expert
Community Expert
April 30, 2014

that break statement (on the line after your trace) needs to be in the if-statement brackets so you can exit that for-loop.

Known Participant
May 1, 2014

It didnt work Kglad

Amy Blankenship
Legend
May 1, 2014

Try something more like:

var hasCollided:Boolean;

for each(var enemy:Enemy in army)  {

     if(PixelPerfectCollisionDetection.isColliding(jail,enemy,this,true)) {

          hasCollided = true;

          break;

     }

}

if (hasCollided) {

     var label = getNextLabel(jail);

     if (label!='') {

          jail.goToAndStop(label);

     } else {

          //you were at the last label

     }

}

....

protected function getNextLabel(mc:MovieClip):String {

     for (var i:int; i<mc.currentFrameLabels.length; i++) {

          var frameLabel:FrameLabel = mc.currentFrameLabels;

          if (frameLabel = mc.currentFrameLabel && i < mc.currentFrameLabels.length - 1) {

               return FrameLabel(mc.currentLabels[i+1]).name;

          }

     }

     return '';

}

This way you can add as may frame labels as you want without having to change your logic. Note that it might be a good idea to move the logic for finding and going to the next label into Jail, so that you could then just do

jail.nextLabel();

Ned Murphy
Legend
April 30, 2014

What problem are you having?  Have you used the trace() function to try to solve whatever it is?

Known Participant
April 30, 2014

the switch statement is conducting stright to the last frame, if you collide with another object and if the actual frame label is "noDamage" the you gotoAndPlay "desiredFrame" which is the next frame, if the actual frame object's label is "desiredFrame" then you play "desiredFrame2", ect, but it doesnt work like that, it is playing all the frames at once.