0

Alright, so I have a button when clicked goes to frame 15. If xpsafety is 0 I want a child to be created and xpsafety to be set to 1. So if the button is clicked again it wont display the child and do the calculation

Fame 10:

var xpsafety:int = 0;
var xpclip:MovieClip = new xp();

RememberButton.addEventListener(MouseEvent.CLICK, GoToRemember); 


function GoToRemember(evt:MouseEvent):void { 
gotoAndPlay(15, "Scene 1");
if (xpsafety == 0) {
    addChild(xpclip);
    xpclip.x=41;
    xpclip.y=835;
    calculateExp(8);
    xpsafety = xpsafety +1;
}


}

Frame 15:

stop();


    BackButton.addEventListener(MouseEvent.CLICK, GoBackSafety); 

    function GoBackSafety(evt:MouseEvent):void { 
        gotoAndPlay(10, "Scene 1");
        removeChild(xpclip);
        }

The error I get is this: ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.

I don't understand what it means, any tips? Please and thank you.

Other issue but it's probably related, despite xpsafety being not equal to 0 the function still goes off ( I know because calculateExp(8) happens.

Butterflycode
  • 759
  • 2
  • 10
  • 23

4 Answers4

2

It's been a while since I did any frame scripting, as it soon gets cumbersome and hard to manage. See here.

On the xpsaftey issue, when you go back to frame 10 the whole script is run again and the variable xpsaftey is being redefined with the default value of 0. You would need to place the declaration var xpsafety:int on an earlier frame in order to prevent this.

Your second example with xpsaftey inside the for loop is just confusing things further as you are redefining it again within the scope of the if statement. I suggest having a general read about variable scope.

Community
  • 1
  • 1
Tom Makin
  • 3,203
  • 23
  • 23
0

Try to do something like :

xpclip.parent.removeChild(xpclip);
blue112
  • 52,634
  • 3
  • 45
  • 54
-1

You are trying to access your xpclip variable from inside a listener, try this.

var mc:MovieClip = (root as MovieClip).xpchild;
(root as MovieClip).removeChild(mc);

If this still doesn't work, check if mc isn't null.

Dave Hart
  • 347
  • 1
  • 9
  • I have another flash project where I used the same variable inside a listener and it worked...I don't quite understand what I need to do with the two lines of code you put up. – Butterflycode Oct 26 '12 at 15:19
-2

All I needed to do was put my variable inside the if statement on frame 15.....as below:

stop();

BackButton.addEventListener(MouseEvent.CLICK, GoBackSafety); 

function GoBackSafety(evt:MouseEvent):void { 
    gotoAndPlay(10, "Scene 1");

    }

var xpclip:MovieClip = new xp();
if (xpsafety==0) {
    var xpsafety:int = 0;
    addChild(xpclip);
    xpclip.x=41;
    xpclip.y=835;
    calculateExp(8);
    xpsafety = xpsafety +1;
    trace(xpsafety);
    } 
Butterflycode
  • 759
  • 2
  • 10
  • 23