У меня есть следующий код из другого вопроса о SO, чтобы отслеживать изменение значения счетчика.
package com.my.functions
{
import flash.events.Event;
import flash.events.EventDispatcher;
public class counterWithListener extends EventDispatcher
{
public static const VALUE_CHANGED:String = 'counter_changed';
private var _counter:Number = 0;
public function counterWithListener() { }
public function set counter(value:Number):void
{
_counter = value;
this.dispatchEvent(new Event(counterWithListener.VALUE_CHANGED));
}
}
}
Что я хотел бы сделать, так это передать значение счетчика до того, как я его изменил, а также новое значение слушателю, чтобы я мог решить, действительно ли новое значение.
Ответ 1 · рейтинг 2 · Joel Hooks
Вы захотите создать собственное событие:
package
{
import flash.events.Event;
public class CounterEvent extends Event
{
public static const VALUE_CHANGED:String = 'valueChanged';
public var before:int;
public var after:int;
public function CounterEvent(type:String, before:int, after:int)
{
this.after = after;
this.before = before;
//bubbles and cancellable set to false by default
//this is just my preference
super(type, false, false);
}
override public function clone() : Event
{
return new CounterEvent(this.type, this.before, this.after);
}
}
}
Это изменит ваш приведенный выше код на:
package com.my.functions
{
import CounterEvent;
import flash.events.EventDispatcher;
public class counterWithListener extends EventDispatcher
{
private var _counter:Number = 0;
public function counterWithListener() { }
public function set counter(value:Number):void
{
this.dispatchEvent(new CounterEvent(CounterEvent.VALUE_CHANGED, _counter, value));
_counter = value;
}
}
}