PDA

View Full Version : [AS3 - Flash 9] removing objects



relinquished
March 15th, 2007, 11:40 AM
Okay, I'm making something in AS3, it's kinda random but it's just to start to get used to the new language. I'm really advanced in AS2, just AS3 is new to me.

What I'm having problems with is removing objects. Here's the code for the entire project:

SimpleParticle class:


package
{
import flash.display.MovieClip
import flash.display.DisplayObjectContainer
import flash.display.DisplayObject
public class SimpleParticle extends MovieClip
{
var speed = 3
var dir = Math.random() * 360
var exist = 0
var liveTo = 60
var xSpeed = Math.cos(dir) * speed
var ySpeed = Math.sin(dir) * speed
public function SimpleParticle()
{
trace("Ball created: " + this.name)
this.addEventListener('enterFrame', frameListener)
}
private function frameListener(event):void
{
exist++
this.x += xSpeed
this.y += ySpeed
xSpeed += 0.1
ySpeed -= 0.05
if(exist >= liveTo)
{
this.killParticle()
}
}
public function killParticle()
{
this.removeEventListener('enterFrame', frameListener)
// What do I need to put here to remove the particle?
}
}
}
Creates particles:


particles = 0
addEventListener('enterFrame', createParticle)
function createParticle(event):void
{
for(var i = 1; i <= 5; i++)
{
var atch:SimpleParticle = new SimpleParticle
atch.x = 100
atch.y = 300
addChild(atch)
trace(atch)
particles++
}
particles_txt.text = particles
}
As you can tell, I can't figure out what I need to make it remove itself once it's life has expired. All of the script works perfectly except for that part. Any help is greatly appreciated :). If you need more details feel free to ask

skatehead
March 15th, 2007, 11:53 AM
to remove children, use the removeChild() function, and pass in the object you want to remove, you can also remove children by their index, i.e removeChildAt(i)

cheers

relinquished
March 15th, 2007, 11:57 AM
Thanks :), but I've tried that already and it gave me an error, something like "variable undefined at SimpleParticle at killParticle() :/.

edit:

sorry, the error was "ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at SimpleParticle/killParticle()
at SimpleParticle/SimpleParticle::frameListener()"

Dazzer
March 15th, 2007, 11:59 AM
public function killParticle()
{
this.removeEventListener('enterFrame', frameListener)
// What do I need to put here to remove the particle?
if (this.parent != null)
{
this.parent.removeChild(this);
}
}


try this.

senocular
March 15th, 2007, 12:00 PM
sounds like you're trying to remove something from something that doesnt contain that something

relinquished
March 15th, 2007, 12:03 PM
ActionScript Code:

public function killParticle()
{
this.removeEventListener('enterFrame', frameListener)
// What do I need to put here to remove the particle?
if (this.parent != null)
{
this.parent.removeChild(this);
}
}





try this.

It works perfectly! Thanks a lot :).

One question, just so I understand what that's doing. By saying "this.parent" you're getting the parent of the object, then by ".removeChild(this)", you're removing the child object (named this) from the parent you just found out?

Dazzer
March 15th, 2007, 12:10 PM
I'm telling the parent of the object to remove this object from its display list.

Remember that AS3 doesn't work with names anymore. It is very much an Object Oriented code. By passing it the parameter 'this' I am, in fact, not giving it an object called this, but passing it a REFERENCE to an object, which in this case, is that current object.

relinquished
March 15th, 2007, 12:42 PM
Okay, got it. That's actually really easy and useful :P :). I'm just having a hard time switching over. Like now I can't find out how to do something when you press down a specific key... in AS2 it was "if(Key.isDown("A".charCodeAt()))", I can't get it to work in AS3, the Keyboard Event class is confusing.

Dazzer
March 15th, 2007, 12:45 PM
senocular has a class that emulates the Key.isDown feature

but with the events structure, now you can just wait for the key press to happen. Instead of checking for that key every frame.

relinquished
March 15th, 2007, 12:54 PM
Okay, but what would the code be for that? something like:



this.addEventListener(KeyboardEvent.KEY_DOWN, keyboardListener)
private function keyboardListener(event:KeyboardEvent):void
{
//do stuff here
}
Because that doesn't even work, and whenever I tried to add a specific key in there it gives me an error. Basically I want it to just trace("key pressed") when you press the A key.

Dazzer
March 15th, 2007, 07:51 PM
Keyboard Listener can only be applied to the stage, i believe.
That is the only way to ensure all keypresses are captured.

relinquished
March 15th, 2007, 08:19 PM
Okay, I still can't get it to work though... :/.

bzouchir
June 3rd, 2007, 09:51 PM
hi all,

just to complete this topic, I am tryin to do the same as in removing a child from object and I'm getting the same error. Obviously I'm tryin got remove the child from the wrong place.



private function addHeadlines(nTabs):void {

var txt01:TextField = new TextField();
var txt02:TextField = new TextField();

if (nTabs ==3){
txt01.text = "display something";
txt01.textColor = 0xFFFFFF;
addChild(txt01);
}else if (nTabs ==4){
removeChild(txt01);
txt02.text = "display something else...";
txt02.textColor = 0xFFFFFF;
addChild(txt02);
}
}

I'm not sure anymore where I'm parenting this TextField? :S
When I addChild it adds it to the stage right? and when i removeChild it should remove that same child from that stage? unless i gor it all wrong :h:

What is the easiest way to deal wiht TectFields? should i create a Sprite and store the TextField in it, so I can switch it on and off and manipuklate however I want?



because adding and removing textField doesn't seem to work for me :S


I'm really stuck please help!!

thx

bzouchir
June 3rd, 2007, 10:04 PM
alright it worked when i defined the variables outside the function as such:



var txt01:TextField = new TextField();
var txt02:TextField = new TextField();

private function addHeadlines(nTabs):void {
...
}



can someone please help me understand how this works?
how are childs usually placed?

thx

muhammadzubair
March 9th, 2009, 10:12 AM
For relinquished,
In AS3 you can't associate the key events to blank stage. you must have any DisplayObject on stage which MUST have keyDown event defined.i.e. TextField... here is the example below


import flash.events.KeyboardEvent;
var txt01:TextField = new TextField();
txt01.border=true;
this.addChild(txt01);
txt01.addEventListener(KeyboardEvent.KEY_DOWN, keyboardListener);
function keyboardListener(event:KeyboardEvent):void
{
trace("Key Pressed = "+event.keyCode);
trace("Key Value = "+String.fromCharCode(event.keyCode));
}

muhammadzubair
March 9th, 2009, 10:21 AM
for bzouchir,
You have a scope problem buddy. In AS3, the variable scope is more better then AS2, thats why it enforces OO (Object Oriented) Design as well. A scope is defined is with-in Curly braces ('{' and '}'). And any thing is defined within any scope is NOT available outside of that scope as well. Like in you first example you have


private function addHeadlines(nTabs):void {

var txt01:TextField = new TextField();
var txt02:TextField = new TextField();

if (nTabs ==3){
txt01.text = "display something";
txt01.textColor = 0xFFFFFF;
addChild(txt01);
}else if (nTabs ==4){
removeChild(txt01);
txt02.text = "display something else...";
txt02.textColor = 0xFFFFFF;
addChild(txt02);
}
}

you defined


var txt01:TextField = new TextField();
and
var txt02:TextField = new TextField();

is defined only your function. and after that those are not available at all, after the function calls ends.


var txt01:TextField = new TextField();
var txt02:TextField = new TextField();

private function addHeadlines(nTabs):void {
...
}

AND, Why this piece of code working... because you have scope variables defined outside your function scope and when you call these values these are available every time.

May be I can't make you clear :) , if you have any more questions please ask.

og2t
May 12th, 2009, 08:15 AM
Hey, you can debug your display list to find the depth your child resides at, have a read here: http://play.blog2t.net/finding-the-missing-child/

Antheor
January 27th, 2010, 08:36 AM
Dazzer, thx for your code ! I was messing around with .this..

Though I still have a thing I don't understand as explained in the comments
Code:

if (this.parent != null)
{
trace(this.parent); //sends [object myparentobject]
trace(this);// sends [object myobject]
this.parent.removeChild(this);
}
trace(this.parent);// NOW sends null
trace(this);// STILL sends [object myobject]
}
So what does that mean :
-I removed the child but it is still in memory (what is the code for removing it then ?)
- I removed the parent ?:face:

Thx for any hints

Maqrkk
January 27th, 2010, 09:26 AM
Dazzer, thx for your code ! I was messing around with .this..

Though I still have a thing I don't understand as explained in the comments
Code:

if (this.parent != null)
{
trace(this.parent); //sends [object myparentobject]
trace(this);// sends [object myobject]
this.parent.removeChild(this);
}
trace(this.parent);// NOW sends null
trace(this);// STILL sends [object myobject]
}
So what does that mean :
-I removed the child but it is still in memory (what is the code for removing it then ?)
- I removed the parent ?:face:

Thx for any hints

- You did not remove the child, you removed it from the display list. It is still in memory. An object will only be garbage collected if there's no references left to that object.
- You did not remove the parent, you removed the object from it's parent. It no longer has a parent, hence null.

setVariable
September 8th, 2010, 08:32 PM
struggling with references to items in memory, can someone help with this.

if I have the following basic test


import flash.display.MovieClip;

var mc:MovieClip = new MovieClip()

addChild(mc)
trace(":mc1: "+mc)
removeChild(mc)

trace(":mc2: "+mc)

it traces out

:mc1: [object MovieClip]
:mc2: [object MovieClip]

is the second trace the reference to stop it being removed? how do you get shot of it and still check it has gone? ie

if(mc)
{
removeChild(mc)
}

is the if(mc) reference above in the if statement preventing the item from being removed?

setVariable
September 8th, 2010, 08:52 PM
only way I got it to work is


var mc:MovieClip = new MovieClip()

addChild(mc)
trace(":mc1: "+mc)
removeChild(mc)

trace(":mc2: "+mc)
mc = null
trace(":mc3: "+mc)

is this the only way?