MultyInit for NativeSignal

已查看 59 次
跳至第一个未读帖子

Leezarius

未读,
2011年10月24日 09:04:142011/10/24
收件人 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

未读,
2011年10月24日 11:03:512011/10/24
收件人 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

未读,
2011年10月24日 11:17:352011/10/24
收件人 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

未读,
2011年10月24日 11:47:112011/10/24
收件人 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

未读,
2011年10月24日 11:49:252011/10/24
收件人 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

未读,
2011年10月24日 11:54:412011/10/24
收件人 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

未读,
2011年10月24日 11:58:282011/10/24
收件人 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

未读,
2011年10月24日 12:38:032011/10/24
收件人 as3-signals
True if we remember(know) that the signal is not an independent
entity.
Thanks.

Leezarius

未读,
2011年10月24日 12:38:232011/10/24
收件人 as3-signals
Thank Roberts.

Stray

未读,
2011年10月24日 12:40:552011/10/24
收件人 as3-s...@googlegroups.com
:) And it's a good thing to call out to people learning Signals - so nicely picked up!

Ulaş Binici

未读,
2013年11月11日 17:36:382013/11/11
收件人 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);
回复全部
回复作者
转发
0 个新帖子