0
votes

I'm trying to build a simple Flash game where the user drags a sombrero onto a cactus. I've got it so that when you drag the sombrero anywhere but the cactus, it snaps back to it's original position. I had it so when you drag it onto the cactus, it stays there.

What I want is when the user drags the sombrero onto the cactus, it takes you to a screen that says "YAY! Play again?" I put a gotoAndPlay() inside my if statement:

if(dropTarget.parent.name == "cactus")
                {
                    //scaleX = scaleY = 0.2;
                    //alpha = 0.2;
                    //y = stage.stageHeight - height - -100;

                    //buttonMode = false;
                    //removeEventListener(MouseEvent.MOUSE_DOWN, down);


                    gotoAndPlay("playAgain");


                    trace("dropped on cactus");


                    }

                    else
                    {
                        returnToOriginalPosition();


                        }

I labeled my second frame as "playAgain." I get an error saying: ArgumentError: Error #2109: Frame label playAgain not found in scene playAgain. at flash.display::MovieClip/gotoAndPlay() at net.dndgtal.Cactus_Game::sombrero/stageUp()

I have Googled and checked and double checked all the suggestions, but cannot get it to work. I don't have a scene "playAgain," only "scene1." I've tried specifying both the scene and the frame- that doesn't work either. And I tried just putting in gotoAndPlay(2), for frame two, but that just does nothing.

Am I missing something? Any help would be appreciated. Here is all of my code if that helps:

package net.dndgtal.Cactus_Game
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.geom.Point;

public class sombrero extends MovieClip
{

    protected var OriginalPosition:Point;

    public function sombrero ()
    {
        OriginalPosition = new Point(x, y);

        buttonMode = true;
        addEventListener ( MouseEvent.MOUSE_DOWN, down );

        //trace("sombrero constructor");
    }

    protected function down (event:MouseEvent):void
    {
        parent.addChild(this);
        startDrag();
        stage.addEventListener(MouseEvent.MOUSE_UP, stageUp);

        //trace("DOWN");
        }

        protected function stageUp(event:MouseEvent):void
        {
            stage.removeEventListener(MouseEvent.MOUSE_UP, stageUp);
            stopDrag();

            if (dropTarget) 
            {


                if(dropTarget.parent.name == "cactus")
                {
                    //scaleX = scaleY = 0.2;
                    //alpha = 0.2;
                    //y = stage.stageHeight - height - -100;

                    //buttonMode = false;
                    //removeEventListener(MouseEvent.MOUSE_DOWN, down);


                    gotoAndPlay("playAgain");


                    trace("dropped on cactus");


                    }

                    else
                    {
                        returnToOriginalPosition();


                        }

                } 

                else
                {
                       returnToOriginalPosition();
                }

            }

    protected function returnToOriginalPosition(): void
    {
        x = OriginalPosition.x;
        y = OriginalPosition.y;
        }

    }



}

Thanks! Let me know if you have any questions.

1
It's hard to say, but I guess your script is in the wrong place in the display tree. I guess you want to navigate on the main timeline, but you are targeting the MCs timeline. Trace out the full tree path to see where you are ...dogsgod
Did you find a solution? If one of the answers below led to your solution, please accept that answer (the checkmark), if not, you can provide your own answer.BadFeelingAboutThis

1 Answers

0
votes

Your issue is likely one of scope. When you use gotoAndPlay("playAgain"), it will take the current timeline (scope), which is the sombrero class, and attempt to find a frame label or scene called "playAgain" on it's timeline.

Presumably, you want the main timeline to gotoAndPlay the frame label "playAgain", not the sombrero.

To access the main timeline, you can use MovieClip(root).gotoAndPlay("playAgain").

If it's not the main timeline, but the parent of sombrero, you can access it with MovieClip(parent).gotoAndPlay("playAgain")

If the relationship between the sombrero and whichever timeline your trying to advance is more complicated than that, then you could pass in a reference to it in your sombrero constructor:

private var targetTimeline:MovieClip;

public function sombrero (targetTimeline_:MovieClip)
{
    targetTimeline = targetTimeline_;
    //...rest of constructor code

And then:

if(dropTarget.parent.name == "cactus"){
    targetTimeline.gotoAndPlay("playAgain");

And when you create the sombrero, pass in the target timeline:

var hat:sombrero = new sombrero(target);