Forum Topic: More AS3 Problems...

(580 views • 12 replies)

This topic is 1 page long.

<< < > >>
None

Wurmy

Reply To Post Reply & Quote

Posted at: 9/7/08 08:56 PM

Wurmy LIGHT LEVEL 28

Sign-Up: 06/20/06

Posts: 2,307

Ok another AS3 problem that also has to do with event listeners (specifically the key event listener) and the garbage collector..

Ok, so at this moment, I have the entire game (as in gameplay part of game) in one class (with many subclasses of course..). All the game is displayed through that game class etc etc.., not one entity is outside it.

So whenever the player clicks on "start game" it creates a new entity of game, when you lose, frame plays game entity is deleted (removed from display list and then set to null). But it's not actually removing the entity from memory. One of the movieclips inside it is playing constantly (I know this thanks to trace).

And this is all because of the key listeners. They key listeners are still referencing to the game entity, and therefore the gc isn't cleaning it up. As soon as I remove the key listeners it all works great.. but obviously I need key listeners otherwise you won't be able to move!

The problem is key listeners can only be on stage for some reason, so I have to either put them in the document as file, or on the timeline.. either way I have to reference to the game entity and that doesn't let the garbage collector do it's job..

So I am officially stumped and have no idea where to go now.. halp? :(
PS: I have removed all event listeners within the class and all subclasses, including the ENTER_FRAME and TIMER ones.. there's a bug where even if those are weak referenced they won't remove (flash players fault).. Source (not source code :p)

BBS Signature

None

deadlock32

Reply To Post Reply & Quote

Posted at: 9/7/08 09:14 PM

deadlock32 NEUTRAL LEVEL 18

Sign-Up: 04/28/01

Posts: 2,502

The key event listeners do not have to be on the stage my good man.

depending on child of the stage has the event listener you can do:

stage.focus = someDisplayObject;
someDisplayObject.addEventListener(keyboardEvent.KEY_DOWN, keyDownEvent);

/// now if this display object ALWAYS needs the keyboard listener you can do:
someDisplayObject.addEventListener(focusEvent.OUT, focusOutEvent); // might be FOCUS_OUT

/// or maybe you just need to disable stuff that intercepts keyboard events that you don't care about
/// such as a text field.  If the mouse clicks on a display object, the previously focus displayObject looses focus
anotherDisplayObject.mouseEnabled = false;
/// and if it has children
anotherDisplayObject.mouseChildren = false;

None

Wurmy

Reply To Post Reply & Quote

Posted at: 9/7/08 11:07 PM

Wurmy LIGHT LEVEL 28

Sign-Up: 06/20/06

Posts: 2,307

Got it working, but now I have two problems:
1. Really really weird bug.. I have a random yellow line across my screen (the flash window I mean).. if I click it goes away or moves somewhere.. it's really odd and I've never seen this before .. it only happens when I create the game class and when I give the object focus.
2. It's not removing the game class from memory now, and I don't have any idea why.. I'll investigate some more..

But I would like to know why #1 pops up..

BBS Signature

None

Wurmy

Reply To Post Reply & Quote

Posted at: 9/7/08 11:09 PM

Wurmy LIGHT LEVEL 28

Sign-Up: 06/20/06

Posts: 2,307

Ok, I should have said this in the last post but..
If I give the stage focus to the game class, then the yellow line appears AND the entity isn't removed (completly)..
And if I don't give stage focus to the game class, it all works fine (except I don't have any keyboard input . . . )

BBS Signature

None

deadlock32

Reply To Post Reply & Quote

Posted at: 9/8/08 06:33 PM

deadlock32 NEUTRAL LEVEL 18

Sign-Up: 04/28/01

Posts: 2,502

this really strange. I can only infer that the yellow line is when you hit tab and flash highlights buttons... so maybe flash is viewing your game class as a button. ( if you want to see an example goto homestarrunner.com and hit tab when you are on the main page.)

if you are having issues removing something from memory, make sure if its a movieclip, that it is stopped, and if any sounds are playing that they are also stopped. I noticed that if a movieclip with timeline sound is playing, it doesnt get unloaded.


None

Moonkey

Reply To Post Reply & Quote

Posted at: 9/8/08 06:58 PM

Moonkey DARK LEVEL 07

Sign-Up: 05/11/07

Posts: 919

You know you can remove event listeners right? (.removeEventListener)

If you remove all the event listeners, things will be freed for garbage collection again.

Another simple way is to use weak referenced listeners.

something.addEventListener(Event.BLA, handler, false, 0, true);

The last parameter means it will use a weak reference, which means it won't prevent garbage collection.


None

Wurmy

Reply To Post Reply & Quote

Posted at: 9/8/08 07:02 PM

Wurmy LIGHT LEVEL 28

Sign-Up: 06/20/06

Posts: 2,307

At 9/8/08 06:58 PM, Moonkey wrote: You know you can remove event listeners right? (.removeEventListener)

If you remove all the event listeners, things will be freed for garbage collection again.

Another simple way is to use weak referenced listeners.

something.addEventListener(Event.BLA, handler, false, 0, true);

The last parameter means it will use a weak reference, which means it won't prevent garbage collection.

I already have all of my listeners set to be weak referenced. And I also remove all of my listeners..
Although I think I solved my problem.. I just wish there was a way to check that it really is removed..

BBS Signature

None

Moonkey

Reply To Post Reply & Quote

Posted at: 9/8/08 07:20 PM

Moonkey DARK LEVEL 07

Sign-Up: 05/11/07

Posts: 919

Ugh, sorry.. looks like I skimmed over the important parts of your post.

I guess its impossible to ever really know since gc doesn't happen at set times.

But yeah, a proper delete keyword and a deconstructor would be nice.


None

Wurmy

Reply To Post Reply & Quote

Posted at: 9/8/08 09:18 PM

Wurmy LIGHT LEVEL 28

Sign-Up: 06/20/06

Posts: 2,307

At 9/8/08 07:20 PM, Moonkey wrote: Ugh, sorry.. looks like I skimmed over the important parts of your post.

I guess its impossible to ever really know since gc doesn't happen at set times.

But yeah, a proper delete keyword and a deconstructor would be nice.

DECONSTRUCTORS! Arrg! I forgot completly about those X_X Those should really help.. as soon as I find out how to make them in AS (I'll google it)

Although I wouldn't mind an explination for the delete keyword.. I see it being used in lots of places (on the internet) but whenever I try to use it I get errors.... whenever I type "delete(" the tool tip shows up and it says "delete(XML)".. so apparently it's for XML?

BBS Signature

None

mongoid

Reply To Post Reply & Quote

Posted at: 9/8/08 11:52 PM

mongoid LIGHT LEVEL 09

Sign-Up: 01/03/02

Posts: 245

Too bad there are no deconstructors in AS 3.0 :-(

You just need to create your own. Try this:

Add a method to your class called deconstruct() or removeMe(), etc. Within the function remove all Event Listeners and finally remove it from the stage.

Example:

public function removeMe():void{

// remove listeners
removeEventListener(blah, blah);

// delete it from the stage
parent.removeChild(this);

}

Hope this works.

:-)


None

DougyTheFreshmaker

Reply To Post Reply & Quote

Posted at: 9/9/08 01:36 AM

DougyTheFreshmaker NEUTRAL LEVEL 02

Sign-Up: 07/30/07

Posts: 518

This post from me won't be the most helpful, unfortunately.

It's destructor(s), not deconstructor(s). I've made this mistake, too. AS3 doesn't have them, presumably because "everything is garbage collected anyway!", but I can't be sure. Either way, they're still necessary for various reasons (BitmapData anyone? many others), and I know other managed languages (C#, at least) do have them... so maybe someday, but not now. You can wrap your stuff to fake a destructor if you really want to, but shoehorning features from other languages into the one you happen to be using is a little bit ugly usually--though I have done that before and it was helpful iirc.

No idea what the yellow line is about. For the "it's not removing the game class from memory" problem... I think that may be a little bit too vague.

We should take care not to make the intellect our god; it has, of course, powerful muscles, but no personality.
Freshmaking
Brainscrape

BBS Signature

None

deadlock32

Reply To Post Reply & Quote

Posted at: 9/9/08 12:13 PM

deadlock32 NEUTRAL LEVEL 18

Sign-Up: 04/28/01

Posts: 2,502

At 9/8/08 07:02 PM, Wurmy1029 wrote:
I already have all of my listeners set to be weak referenced. And I also remove all of my listeners..
Although I think I solved my problem.. I just wish there was a way to check that it really is removed..

There is a way for you to know if garbage collection has happened.

first get a flash memory viewer:
http://www.gskinner.com/blog/archives/20 08/04/simple_componen.html

second, where ever you are removing listeners and want to know if garbage collection happened... create a bunch of garbage. Then do a net connection 'hack' to start the garbage collector.

private const GARBAGE_TOTAL:int = 250;
////////////
someObject.removeEventListener(Foo.BAR, foobar);

for(var garbageIndex:int = 0; garbageIndex < GARBAGE_TOTAL; garbageIndex++)
{
   var trashClip:MovieClip = new MovieClip();
   trashClip.graphics.lineStyle();  
   trashClip.graphics.beginFill(0xF0CC12, 1);
   trashClip.graphics.drawRect(0,0,500,500);
   trashClip.graphics.endFill();
}

/// trigger garbage collection
try
{
    var lc:LocalConnection = new LocalConnection();
   lc.connect("FOO");
   lc.connect("BAR");
}
catch(e:Error)
{

}

using the memory viewer, you will see memory increase, then if it decreases that means garbage collection has happened. Adding some garbage helps see if the 'hack' triggered garbage collection.

of couse this doen't mean your event listener has garbage collected... you are just sure that a GC sweep happened. ^_^

also don't keep that code in your program when it is released

None

Ben-Fox

Reply To Post Reply & Quote

Posted at: 9/9/08 05:07 PM

Ben-Fox LIGHT LEVEL 07

Sign-Up: 12/27/03

Posts: 571

I haven't seen it mentioned yet, so you might want to check your code to see if you've defined any circular references.


All times are Eastern Standard Time (GMT -5) | Current Time: 10:57 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!