Skip to main content
Participating Frequently
March 31, 2014
Question

gotoAndStop when if statement is true problem

  • March 31, 2014
  • 2 replies
  • 1479 views

Hi guys,

Ive asked a question yesterday with this code but it was a different one. Im stuck now again the whole day and i cannot find any related answered question here about this.

I need to get flash to jump to a certain frame/label when a statement is true. In other words in my program ive made a simple drag and drop game and what i need is that when all are correct my right_mc smiley comes up and when wrong my wrong_mc smiley comes up. But i need flash to gotoAndStop at the last frame of the program if the answers are correct, and if the answers are not correct (else) it should stay on the current site so that the player can try again.

Ive tried many different codes but just jumps directly to the last frame without showing the game so what am i doing wrong? Im thinking i have the right arguments but im kinda confused now after all. Would be very much appriciated forany help. I googled, read in the book etc but can only find regular gotoAndStop actions without any if statements so i cant even find any examples 😕😕

At the very end of the page is the code bit i mean.

right_mc.visible=false;

wrong_mc.visible=false;

var orig1X:Number=item1_mc.x;

var orig1Y:Number=item1_mc.y;

var orig2X:Number=item2_mc.x;

var orig2Y:Number=item2_mc.y;

var orig3X:Number=item3_mc.x;

var orig3Y:Number=item3_mc.y;

var orig4X:Number=item4_mc.x;

var orig4Y:Number=item4_mc.y;

var orig5X:Number=item5_mc.x;

var orig5Y:Number=item5_mc.y;

item1_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragTheObject);

item1_mc.addEventListener(MouseEvent.MOUSE_UP, item1Release);

item2_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragTheObject); 

item2_mc.addEventListener(MouseEvent.MOUSE_UP, item2Release); 

item3_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragTheObject);  

item3_mc.addEventListener(MouseEvent.MOUSE_UP, item3Release);

item4_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragTheObject);  

item4_mc.addEventListener(MouseEvent.MOUSE_UP, item4Release);

item5_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragTheObject);  

item5_mc.addEventListener(MouseEvent.MOUSE_UP, item5Release);

//skapar en liten hand när man placerar musen över objekten

item1_mc.buttonMode=true;  

item2_mc.buttonMode=true;  

item3_mc.buttonMode=true;

item4_mc.buttonMode=true;  

item5_mc.buttonMode=true;  

function dragTheObject(event:MouseEvent):void {

    var item:MovieClip=MovieClip(event.target);

    item.startDrag();

    var topPos:uint=this.numChildren-1;

    this.setChildIndex(item, topPos);  

};

function item1Release(event:MouseEvent):void {

    var item:MovieClip=MovieClip(event.target);

    item.stopDrag();     

    if (avc_mc.hitTestPoint(item.x,item.y)) {

        item.x=avc_mc.x;

        item.y=avc_mc.y;

    } else {

       item.x=orig1X;

       item.y=orig1Y;

    }

};  

function item2Release(event:MouseEvent):void {

    var item:MovieClip=MovieClip(event.target);

    item.stopDrag();

    if (miljo_mc.hitTestPoint(item.x,item.y)) {

        item.x=miljo_mc.x;

        item.y=miljo_mc.y;

    } else {

        item.x=orig2X;

        item.y=orig2Y;

    }

};  

function item3Release(event:MouseEvent):void {

    var item:MovieClip=MovieClip(event.target);

    item.stopDrag(); 

    if (trashbag_mc.hitTestPoint(item.x,item.y)) {

        item.x=trashbag_mc.x;

        item.y=trashbag_mc.y;

    } else {

        item.x=orig3X;

        item.y=orig3Y;

    }

};

function item4Release(event:MouseEvent):void {

    var item:MovieClip=MovieClip(event.target);

    item.stopDrag(); 

    if (avs_mc.hitTestPoint(item.x,item.y)) {

        item.x=avs_mc.x;

        item.y=avs_mc.y;

    } else {

        item.x=orig4X;

        item.y=orig4Y;

    }

};  function item5Release(event:MouseEvent):void {

    var item:MovieClip=MovieClip(event.target);

    item.stopDrag(); 

    if (apotek_mc.hitTestPoint(item.x,item.y)) {

        item.x=apotek_mc.x;

        item.y=apotek_mc.y;

    } else {

        item.x=orig5X;

        item.y=orig5Y;

    }

answer_btn.addEventListener(MouseEvent.CLICK, checkAnswers);  

reset_btn.addEventListener(MouseEvent.CLICK, reset);

  };  function checkAnswers(event:MouseEvent):void {

    if (avc_mc.hitTestPoint(item1_mc.x,item1_mc.y) &&

            miljo_mc.hitTestPoint(item2_mc.x,item2_mc.y) &&

          trashbag_mc.hitTestPoint(item3_mc.x,item3_mc.y) &&

          avs_mc.hitTestPoint(item4_mc.x,item4_mc.y) &&

          apotek_mc.hitTestPoint(item5_mc.x,item5_mc.y)) {

        wrong_mc.visible = false;

        right_mc.visible = true;

    } else {

           wrong_mc.visible = true;

        right_mc.visible = false;

    }

};function reset(event:MouseEvent):void {

    item1_mc.x=orig1X;

    item1_mc.y=orig1Y;   

    item2_mc.x=orig2X;

    item2_mc.y=orig2Y;

    item3_mc.x=orig3X;

    item3_mc.y=orig3Y;

          item4_mc.x=orig4X;

    item4_mc.y=orig4Y;

          item5_mc.x=orig5X;

    item5_mc.y=orig5Y;

     

    right_mc.visible=false;

    wrong_mc.visible=false;

}

if (right_mc.visible == true);

gotoAndStop ("theend");

else

{

gotoAndStop ("Game");

}

}

This topic has been closed for replies.

2 replies

Inspiring
March 31, 2014

1. You have some syntax problems.

2. I added function moveToProperFrame() which is called from different places- I am not sure if this is what you want though. Try this script:

right_mc.visible = false;

wrong_mc.visible = false;

var orig1X:Number = item1_mc.x;

var orig1Y:Number = item1_mc.y;

var orig2X:Number = item2_mc.x;

var orig2Y:Number = item2_mc.y;

var orig3X:Number = item3_mc.x;

var orig3Y:Number = item3_mc.y;

var orig4X:Number = item4_mc.x;

var orig4Y:Number = item4_mc.y;

var orig5X:Number = item5_mc.x;

var orig5Y:Number = item5_mc.y;

item1_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragTheObject);

item1_mc.addEventListener(MouseEvent.MOUSE_UP, item1Release);

item2_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragTheObject);

item2_mc.addEventListener(MouseEvent.MOUSE_UP, item2Release);

item3_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragTheObject);

item3_mc.addEventListener(MouseEvent.MOUSE_UP, item3Release);

item4_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragTheObject);

item4_mc.addEventListener(MouseEvent.MOUSE_UP, item4Release);

item5_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragTheObject);

item5_mc.addEventListener(MouseEvent.MOUSE_UP, item5Release);

//skapar en liten hand när man placerar musen över objekten

item1_mc.buttonMode = true;

item2_mc.buttonMode = true;

item3_mc.buttonMode = true;

item4_mc.buttonMode = true;

item5_mc.buttonMode = true;

function dragTheObject(event:MouseEvent):void

{

          var item:MovieClip = MovieClip(event.target);

          item.startDrag();

          addChild(item);

}

function item1Release(event:MouseEvent):void

{

          var item:MovieClip = MovieClip(event.target);

          item.stopDrag();

          if (avc_mc.hitTestPoint(item.x, item.y))

          {

                    item.x = avc_mc.x;

                    item.y = avc_mc.y;

 

          }

          else

          {

                    item.x = orig1X;

                    item.y = orig1Y;

          }

}

function item2Release(event:MouseEvent):void

{

          var item:MovieClip = MovieClip(event.target);

          item.stopDrag();

          if (miljo_mc.hitTestPoint(item.x, item.y))

          {

                    item.x = miljo_mc.x;

                    item.y = miljo_mc.y;

          }

          else

          {

                    item.x = orig2X;

                    item.y = orig2Y;

          }

}

function item3Release(event:MouseEvent):void

{

          var item:MovieClip = MovieClip(event.target);

          item.stopDrag();

          if (trashbag_mc.hitTestPoint(item.x, item.y))

          {

                    item.x = trashbag_mc.x;

                    item.y = trashbag_mc.y;

          }

          else

          {

                    item.x = orig3X;

                    item.y = orig3Y;

          }

}

function item4Release(event:MouseEvent):void

{

          var item:MovieClip = MovieClip(event.target);

          item.stopDrag();

          if (avs_mc.hitTestPoint(item.x, item.y))

          {

                    item.x = avs_mc.x;

                    item.y = avs_mc.y;

          }

          else

          {

                    item.x = orig4X;

                    item.y = orig4Y;

          }

}

function item5Release(event:MouseEvent):void

{

          var item:MovieClip = MovieClip(event.target);

          item.stopDrag();

          if (apotek_mc.hitTestPoint(item.x, item.y))

          {

                    item.x = apotek_mc.x;

                    item.y = apotek_mc.y;

 

          }

          else

          {

                    item.x = orig5X;

                    item.y = orig5Y;

          }

 

          answer_btn.addEventListener(MouseEvent.CLICK, checkAnswers);

          reset_btn.addEventListener(MouseEvent.CLICK, reset);

}

function checkAnswers(event:MouseEvent):void

{

          if (avc_mc.hitTestPoint(item1_mc.x, item1_mc.y) && miljo_mc.hitTestPoint(item2_mc.x, item2_mc.y) && trashbag_mc.hitTestPoint(item3_mc.x, item3_mc.y) && avs_mc.hitTestPoint(item4_mc.x, item4_mc.y) && apotek_mc.hitTestPoint(item5_mc.x, item5_mc.y))

          {

                    wrong_mc.visible = false;

                    right_mc.visible = true;

          }

          else

          {

                    wrong_mc.visible = true;

                    right_mc.visible = false;

          }

          moveToProperFrame();

}

function reset(event:MouseEvent):void

{

          item1_mc.x = orig1X;

          item1_mc.y = orig1Y;

          item2_mc.x = orig2X;

          item2_mc.y = orig2Y;

          item3_mc.x = orig3X;

          item3_mc.y = orig3Y;

          item4_mc.x = orig4X;

          item4_mc.y = orig4Y;

          item5_mc.x = orig5X;

          item5_mc.y = orig5Y;

          right_mc.visible = false;

          wrong_mc.visible = false;

          moveToProperFrame();

}

function moveToProperFrame():void

{

          if (right_mc.visible == true)

                    gotoAndStop("theend");

          else

          {

                    gotoAndStop("Game");

          }

}

Pati83Author
Participating Frequently
April 7, 2014

Hey thanks for reply and sorry for late reply but my internet went down.Your code helped though so i thank you very much for that. I dont know if im allowed to ask other questions here regarding my code but do you happen to know how i can remove the children (my objects) after they are dragged because they appear on the next page when i leave the  game. Im looking around for tutorials and explanations but there are no good explanations and im going out of my mind.

If im not allowed to  ask that here i apologize and thank you for your previous help!!


Cheers

kglad
Community Expert
Community Expert
April 7, 2014

use removeChild to remove objects that you no longer want in the display list.

kglad
Community Expert
Community Expert
March 31, 2014

this is problematic:

if (right_mc.visible == true);

gotoAndStop ("theend");

else

{

gotoAndStop ("Game");

}

}

-------------

first, that needs to execute when you want to check and that's probably not when that frame loads (as you currently defined it).

second, the code after the line with reset_btn.addEvent...  needs to be fixed so there are no improperly matched brackets (as exemplified in the problematic code).