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

gotoAndStop when if statement is true problem

New Here ,
Mar 31, 2014 Mar 31, 2014

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");

}

}

TOPICS
ActionScript
1.3K
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 ,
Mar 31, 2014 Mar 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). 

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 ,
Mar 31, 2014 Mar 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");

          }

}

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
New Here ,
Apr 07, 2014 Apr 07, 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

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 07, 2014 Apr 07, 2014

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

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
New Here ,
Apr 07, 2014 Apr 07, 2014

yes exactly but how do i apply it to my code ?

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 07, 2014 Apr 07, 2014

when you do you want those objects removed?  that's when you should execute removeChild for each object you want removed.

and, if you want them re-added later, you need use addChild to re-add them.

you should be able to understand your own code to know when you want those lines of code to execute.

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
New Here ,
Apr 08, 2014 Apr 08, 2014

i do understand my own code my dear friend, i´ve made it myself from scratch and im pretty proud since im a beginner, but since i am a beginner i cannot know everything, right

So i did know that remove child needs to be used on those objects, but i do not know exactly how to apply it and i cannot find a very good tutorial online and even worse, nothing in our school litterature, so this is why i ask for help.
But i appreciate any feedback so thanks a lot

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 08, 2014 Apr 08, 2014

then where in your code do you want to re-add those object?  is there function that executes when you want to re-add those objects?

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
New Here ,
Apr 08, 2014 Apr 08, 2014

i dont know what you mean with re-adding. Whenever a person goes back to the game it starts from the beginning, objects are back to their original position so no need to re-add the objects. So the only thing i want is for the objects not to appear anywhere else but the game space. I didnt know that was such an complex thing to do but apparanly it is. Or im dumb

Anyhow, i appriciate all the feedback, ill just have to try to google it

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 08, 2014 Apr 08, 2014
LATEST

ok when do you want to remove those objects?

is it when one (or both) of these if-branches executes:

function moveToProperFrame():void
{
          if (right_mc.visible == true)
                    gotoAndStop("theend");
          else
          {
                    gotoAndStop("Game");
          }
}

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 07, 2014 Apr 07, 2014

I must admit I don't understand your requirements. At what point do you want them to be removed? The statement "after they are dragged" is pretty vague. What other conditions coincide with (or result in) objects being dragged? Do you mean any object that has been dragged should disappear? Or do they disappear under certain circumstances only?

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
New Here ,
Apr 07, 2014 Apr 07, 2014

sorry for my bad english. my game is a part of a website so what i mean is that when you drag around an object in the game and then click on the menue to go to an other page (lets say i dont wanna play anymore), the objects from the drag and drop game still appear on the next page.

And i want them to just stay on the game page, if you understand what i mean. Did i explain it better now ? 🙂

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 07, 2014 Apr 07, 2014

So, what you are saying is that objects must stay within the "realm" of particular game/level, right? In other words, if person returns to the game's "page"/level - objects that are related to this page/level must appear again?

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
New Here ,
Apr 08, 2014 Apr 08, 2014

Yes exactly and they do appear again if you return to that page, just that they duplicate when they return to the game page for some weird reason....

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