Actionscript 3.0, adobe flash cs6 - removing a child - actionscript-3

I set a state every time I press a button. Then it goes to a frame and in the action script, whatever the state, it add a child to the stage and change the state again, but when you click the button again the child removes and state is changed back to previous state and the same child gets added again. Here is my code:
// first frame and will never come back to this one
function blueTurnValve(event:MouseEvent) // to go to the frame
{
gotoAndPlay("blue");
STATE = 1;
}
var redscreen:redfilling = new redfilling();
if (STATE == 2)
{
removeChild(redscreen);
STATE = 1;
}
if (STATE == 1)
{
addChildAt(redscreen,2);
STATE = 2;
}
//after the child was added
function blueTurnValve1(event:MouseEvent)
{
gotoAndPlay("blue");
}
but it won't work, I get the error in the output(just before removing the child):
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at p1_fla::MainTimeline/frame2()[p1_fla.MainTimeline::frame2:35]
at flash.display::MovieClip/gotoAndPlay()
at p1_fla::MainTimeline/blueTurnValve1()[p1_fla.MainTimeline::frame30:19]
what is wrong with my code?

You need to use removeChildAt() since at STATE==1.
You are adding it as addChildAt(redscreen,2);
Try, removing it like so,
removeChildAt(2);

Related

ActionScript 3: Adding multiple instances of the same object to the stage and removing each separately

I have a piece of code adding three insances of the same MovieClip to the stage. I also added MouseEvent.CLICK listener. Once any of the MovieClips gets clicked, I want it to be removed from the stage. My problem is, whenever any of the elements gets clicked, only the last one gets removed and when I click again on a different instance, I'm getting:
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
I added these three MovieClips to an array, but I don't know how I can properly identify which instance got clicked and remove only the said instance.
This is the excerpt of code I have:
var myMC: SomeMC;
var myArray: Array = [];
function Loaded(e: Event): void {
for (var i: int = 0; i < 3; i++) {
myMC = new SomeMC();
myMC.addEventListener(MouseEvent.CLICK, Clicked);
myMC.y = 50;
myMC.x = 50 * i;
addChild(myMC);
myArray.push(myMC);
}
}
function imageClicked(e: MouseEvent){
// Only the last instance gets removed.
e.currentTarget.parent.removeChild(myMC);
}
I'd be grateful for any help.
In Loaded function you create 3 instances of the object, but by doing:
myMC = new SomeMC();
you overwrite reference. In first iteration myMC is 1st one, in 2nd 2nd one, etc...
Then in imageClicked you're trying to remove it. 1st time it's working because it's referencing the last object, but after you removed it from stage it won't work anymore.
How about changing e.currentTarget.parent.removeChild(myMC); to e.currentTarget.parent.removeChild(e.currentTarget); ? That should remove clicked one.

AS3 - Cannot access a property or method of a null object reference on gotoAndPlay

I have a scene that contains a movie clip. That movie clip has a button that controls the y position of a symbol in the scene. What I tried to do is move on to the next scene when the symbol reaches certain y values. I used gotoAndPlay when the desired y position was reached, the new scene was switched to but an error appeared on output as indicated by the title. This is the code that appears in the movie clip:
launch_btn.addEventListener(MouseEvent.CLICK, init_launch)
function init_launch(evt:MouseEvent):void
{
MovieClip(root).launch_video.play();
var k = setTimeout(launch, 1);
}
function launch():void
{
trace(MovieClip(root).rkt.y);
if(MovieClip(root).rkt.y != null)
{
//progressively changing the y position
if(MovieClip(root).rkt.y != null)
{
if(MovieClip(root).rkt.y < 600)
{
MovieClip(root).rkt.y -=0.3
}
if(MovieClip(root).rkt.y < 500)
{
...
}
setTimeout(launch, 1);
if(MovieClip(root).rkt.y < -150)
{
MovieClip(root).gotoAndPlay(1, "Scene 3")
}
}
}
Currently, as compile this code the error points to the line "trace(MovieClip(root).rkt.y);".
What I don't get is why rkt suddenly becomes null when I try to go to a different scene. I tried checking if the property is null but that doesn't help.
I tried removing eventListener, and calling functions that resided in the actions layer of the scene itself (the original one) instead of directly going to the scene from the movie clip.
All in vain.
Any ideas?
What I don't get is why rkt suddenly becomes null when I try to go to a different scene. I tried checking if the property is null but that doesn't help.
It's the object that's null, not the property.
I tried removing eventListener
Which won't help much given that the continuing calls to launch are not triggered by an event but setTimeout which will keep firing.
Stop using setTimeout and use a Timer. This allows you to remove the event listeners properly and actually stop it.
MovieClip(root) was actually the property that turned null when the y position was reached. I modified the condition inside launch() to handle that:
function launch():void
{
if(MovieClip(root)!= null)
{
rest of code...
}
}
I really hope this would help others. I only posted a question here after much research online and particularly here. It appears this error plagues a lot of developers.

Error 2025 that I absolutely don't understand

I'm trying to build a point & click game.
I can drag item from my inventory to the scene.
I want to make my object disapear when I'm clicking 2 times.
It's working, but when the object disapear I've got an error 2025.. (I can ignore it and everything is working, but I'd like to correct this error).
My error say :
Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at com.laserdragonuniversity.alpaca::DraggedItem/removeDraggedItem()
[C:\Users\Stéphan\Desktop\12 octobre\La Brousse en folie tactile\com\laserdragonuniversity\alpaca\DraggedItem.as:145]
Here's when it's happening :
(I click on my inventory, take my item, drag it to the scene, click 2 times anywhere, the item diseapear, I'm clicking on the inventory again --> ERROR 2025)
Here's my removeDraggedItem function :
private function removeDraggedItem(e:MouseEvent) {
if(timer.running==true) {
if(e.buttonDown) {
stageRef.removeEventListener(MouseEvent.MOUSE_MOVE, dragItem);
stageRef.removeEventListener(Event.ENTER_FRAME, itemHitTest);
draggedItem.removeEventListener(MouseEvent.MOUSE_DOWN, itemClick);
stageRef.removeChild(draggedItem);
toolbar.useText.text = "";
if (stageRef.contains(this))
stageRef.removeChild(this);
Mouse.show();
Engine.playerControl = true;
}
} else {
if(e.buttonDown) {
timer.start();
}
}
}
What am I doing wrong ?
To avoid this error, I do this:
if( itemToBeRemoved.parent )
{
itemToBeRemoved.parent.removeChild( itemToBeRemoved );
}
I can't tell what the problem is in your code as its not showing me the contents of DraggedItem especially like 145. Perhaps you clicking 2 times causes remove-item events that shouldn't?

Removing Children in AS3

My flash game exists of a timeline with multiple frames (I know I should avoid the timeline)
The point of the game is a point and click adventure. The object that you are able to pick up get spawned and destroyed accordingly as you enter and leave the room. now my problem is when entering frame 14 (accessibel from frame 12) it creates a piece of paper which you are able to pick up if you have another item. Now my problem is when you can't or don't pick up the paper and go back to frame 12 (only exit is to frame 12), you can't click on any other object and you are basicly stuck on frame 12. When leaving and entering other rooms it works properly but for some reason it doesn't for on the paper on frame 14.
My code to remove objects works as following
In my Main.as Documentclass I have a function that called as soon as the game starts which does the following
if (lastframe == 14)
{
trace (prop.numChildren);
while (prop.numChildren )
{
prop.removeChildAt(0);
}
}
The lastframe variable is established when moving from frames
this function is found on the frame itself (each exit function on it's own respective frame)
function exitKantine(event:MouseEvent):void
{
Main.lastframe = 14;
gotoAndStop(12);
}
The function to remove the prop actually removes it but then causes all other clickable objects to be unusable.
Thanks for looking at my question and thanks in advance for your suggestions
I would say instead of removing children, add it once in the beginning, add all the listeners in the beginning, and toggle the visibility instead of trying to addChild and removeChild every time you want to hide it. Use an array so you can have a few happening at the same time.
something like this:
private function init():void
{
assignVars();
addListeners();
stage.addChild // make sure this is in document class or you are passing stage to the class using it
}
for (var i = 0; i < _thingsAry.length; i++)
{
if (_thingsAry[i] == 14)
{
_thingsAry[i].visible = false;
trace("the visibility of _thingsAry[" + i + "] is " + _thingsAry[i].visible
}
}

Parameter child must be non-null error in AS3

I have a code to pause the game, and it runs the pause function as shown:
public function onKeyPress(keyboardEvent:KeyboardEvent) :void
{
//Check for pause
if(keyboardEvent.keyCode == Keyboard.P)
{
//If the timer is still running
if(gameTimer.running)
{
gameTimer.stop();
Mouse.show();
pauseText = new PauseText();
pauseText.x = 150;
pauseText.y = 100;
addChild(pauseText);
//If the player is using the mouse, resume by clicking on the player
if(mouseControl)
{
player.addEventListener(MouseEvent.CLICK, resumeGame);
pauseText.pauseInformation.text = "click on yourself";
}
else
{
pauseText.pauseInformation.text = "press 'p'";
}
}
else
{
//Only allow the player to resume with P IF he is using the keyboard
//This prevents cheating with the mouse.
if(!mouseControl)
{
gameTimer.start();
removeChild(pauseText);
pauseText = null;
}
}
}
}
The game runs perfectly fine. On my first playthrough, the pause functions work. However, if later I die and restart the game, then pause it, I get the following message:
TypeError: Error #2007: Parameter child must be non-null.
at flash.display::DisplayObjectContainer/removeChild()
at Game/onKeyPress()
The game still runs fine though. However, everytime I pause, or unpause, this error appears. If I die again, restart, then pause, TWO of these errors appears. From what I can gather it seems as if it attempts to remove the pauseText…but I’ve been removing it just fine on the first playthrough, I’ve used removeChild() then set as null for other parts of my code and it works fine. Additionally, if I add a trace(“a”); statement right after the function header, I get the error before the “a” appears on the output panel.
What’s wrong?
Additional notes:
If I don’t use the pause function at all for my first playthough, there is no error when I call it up on my second playthrough.
put removeChild into 'if' ,this will solve error :
if(pauseText.parent){
pauseText.parent.removeChild(pauseText);
}
but You should anyway check what is the source of problem , maybe 'gameTimer.running' is false on beggining ?
You probably instantiate another Game object (the one that contains the whole game) while not removing the previous game's event listener. That would explain such behavior, since you have more than one KeyboardEvent.KEY_DOWN listener active, and note that when you're stopping the game, you most likely stop the timer in it, so the "else" clause of your "if (gameTimer.running)" statement is executed, but the timer was effectively stop without pauseText to be generated. So, you miss a
removeEventListener(KeyboardEvent.KEY_DOWN,onKeyPress);
in your game destruction code.
if (!mouseControl) {
gameTimer.start();
if (pauseText && contains(pauseText)) {
removeChild(pauseText);
pauseText = null;
}
}