Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Switch statements executing at the same time

Community Beginner ,
Apr 30, 2014 Apr 30, 2014

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;

}

TOPICS
ActionScript
491
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 30, 2014 Apr 30, 2014

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 30, 2014 Apr 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 30, 2014 Apr 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 30, 2014 Apr 30, 2014

It didnt work Kglad

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Apr 30, 2014 Apr 30, 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();

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 30, 2014 Apr 30, 2014

it also looks like those goto's are in a timer loop. if so, that could be problematic.

in any case, copy and paste your corrected onTick function.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 30, 2014 Apr 30, 2014
LATEST

Thanks a lot you guys!!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines