MultyInit for NativeSignal

59 views
Skip to first unread message

Leezarius

unread,
Oct 24, 2011, 9:04:14 AM10/24/11
to as3-signals
Hi EveryBody!
I see signals and find strange situation:

...
public var clicked:NativeSignal;

public function ButtonSound(_view:MovieClip)
{
view = _view;
init();
}

private function init():void
{

clicked = new NativeSignal(view, MouseEvent.CLICK, MouseEvent);
clicked.add(changeState);
clicked = new NativeSignal(view, MouseEvent.ROLL_OVER, MouseEvent);
clicked.add(overHandler);
clicked = new NativeSignal(view, MouseEvent.ROLL_OUT, MouseEvent);
clicked.add(outHandler);
...

And its work!
My button duspatch all 3 state.
trace(clicked.numListeners)// output:"1"
This means the signal keeps copies of all assigned without the
possibility of their removal?

Simon Richardson

unread,
Oct 24, 2011, 11:03:51 AM10/24/11
to as3-s...@googlegroups.com
The NativeSignal wraps addEventListener, so the changeState, overHandler, outHandler get passed to the native method. So when the view clicked all three methods would be called (this is expected). Ideally the gc would collect this when the view is removed and the class (which the native signals are in) is gc'd, but when this might happen is a guess.

My solution would be to use different variables for the the NativeSignals (nativeClicked, nativeRollOver, nativeRollOut, etc) and this would prevent the leak, but if this situation does occur then you could just use the view.removeEventListener(MouseEvent.CLICK, changeState), but then you have to rely on gc for the original signals being cleared.

But from what I'm seeing, I think you're trying to do the following:

public const signals : Vector.<NativeSignal> = new Vector.<NativeSignal>(3, true);

signals[0] = new NativeSignal(view, MouseEvent.CLICK, MouseEvent);
signals[0].add(changeState);
signals[1] = new NativeSignal(view, MouseEvent.ROLL_OVER, MouseEvent);
signals[1].add(overHandler);
signals[2] = new NativeSignal(view, MouseEvent.ROLL_OUT, MouseEvent);
signals[2].add(outHandler);

If this is the aim, I would suggest making a immutable class that has 3 getters for each NativeSignal and having a method on it to determine how many signals are used in total. (get numListeners())

(other options exist as well, this is just how I would do it.)

Cheers
si

Robert Penner

unread,
Oct 24, 2011, 11:17:35 AM10/24/11
to as3-s...@googlegroups.com
If you're using several NativeSignals, have a look at the new base classes, e.g. SignalMovieClip:


// view extends SignalMovieClip
view.signals.click.add(onClick);
view.signals.rollOver.add(onRollOver);
view.signals.rollOut.add(onRollOut);

Robert

Leezarius

unread,
Oct 24, 2011, 11:47:11 AM10/24/11
to as3-signals
Simon Thanks!
I used three different signal

public var clicked:NativeSignal;
public var overed:NativeSignal;
public var outed:NativeSignal;

But I was wrong when copying and did not change signal names when
initializing.
I understand that the signal is a wrapper, but I think it's a bad
situation.
In the following case, for example, I have only one array
var customArray:Array;
customArray=[1,1,1];
customArray=[2,2,2];
customArray=[3,3,3];
And this array is [3,3,3]

In the case of a signal I can add any number of events and a problem
with garbage collector or uncontrolled behavior of the object.
Thanks.

Leezarius

unread,
Oct 24, 2011, 11:49:25 AM10/24/11
to as3-signals
Hi Robert!
Many respects for your code!
I try SignalMovieClip, thank you.

On Oct 24, 7:17 pm, Robert Penner <rob...@robertpenner.com> wrote:
> If you're using several NativeSignals, have a look at the new base classes,
> e.g. SignalMovieClip:
>
> https://github.com/robertpenner/as3-signals/blob/master/src/org/osfla...
>
> // view extends SignalMovieClip
> view.signals.click.add(onClick);
> view.signals.rollOver.add(onRollOver);
> view.signals.rollOut.add(onRollOut);
>
> Robert
>

Stray

unread,
Oct 24, 2011, 11:54:41 AM10/24/11
to as3-s...@googlegroups.com
True, but what you're comparing (with the signals) is more like:

var customArray:Array;

customArray = [view]
customArray[0].addEventListener(MouseEvent.CLICK...);
customArray = [view]
customArray[0].addEventListener(MouseEvent.MOUSE_DOWN...);
customArray = [view]
customArray[0].addEventListener(MouseEvent.MOUSE_UP...);

Which would behave in just the same way as the Signals do...

Stray

Robert Penner

unread,
Oct 24, 2011, 11:58:28 AM10/24/11
to as3-s...@googlegroups.com
That's the thing with listeners: the event source has a reference to them. It can't tell when you've nulled or replaced one of the references to the listening object. You need to clean up afterwards and this applies beyond Signals. At least removeAll() makes it easy.

Robert

Leezarius

unread,
Oct 24, 2011, 12:38:03 PM10/24/11
to as3-signals
True if we remember(know) that the signal is not an independent
entity.
Thanks.

Leezarius

unread,
Oct 24, 2011, 12:38:23 PM10/24/11
to as3-signals
Thank Roberts.

Stray

unread,
Oct 24, 2011, 12:40:55 PM10/24/11
to as3-s...@googlegroups.com
:) And it's a good thing to call out to people learning Signals - so nicely picked up!

Ulaş Binici

unread,
Nov 11, 2013, 5:36:38 PM11/11/13
to as3-s...@googlegroups.com
Or You can use NativeSignalSet

clicked = new NativeSignalSet(view);
clicked.getNativeSignal(MouseEvent.CLICK,MouseEvent).add(changeState);
clicked.getNativeSignal(MouseEvent.ROLL_OVER,MouseEvent).add(overHandler);
clicked.getNativeSignal(MouseEvent.ROLL_OUT,MouseEvent).add(outHandler);
trace(clicked.numListeners);
Reply all
Reply to author
Forward
0 new messages