質問

So I'm having a few noob errors to my actionscript and I need some help in resolving it as I keep. The code implements a Timer change will change text for a given duration. It received the duration and the RichText item that need to be highlighted/changed and changes it's color for a given time. That's the basic structure of it.

package
{
    import flash.events.Event;
    import flash.events.TimerEvent;
import flash.utils.Timer;

import spark.components.RichText;

    public class TextChanger
    {


        public function changeColorForDuration(Duration:int, Texter:RichText){

            var highlightTextForDuration:Timer = new Timer(1000, Duration);

    highlightTextForDuration.addEventListener(TimerEvent.TIMER_COMPLETE, textDehighlight(Texter));

            textHighlight(Texter);
            highlightTextForDuration.start();   

        }

        private function textHighlight(specificText:RichText):void{
            var textField:RichText = specificText;
            textField.setStyle("color", "#ED1D24");
        }
        private function textDehighlight(textToChange:RichText):void{
            var textField:RichText = textToChange;
            textField.setStyle("color", "#00000");
        }
      }
    }

Any assistance you can offer will be greatly appreciated.

Thanks.

役に立ちましたか?

解決

When you call addEventListener, you need to pass in a function, not a function call:

highlightTextForDuration.addEventListener(TimerEvent.TIMER_COMPLETE, textDehighlight);

And your listener function needs to look more like this:

private function textDehighlight(e:TimerEvent):void{
    var textField:RichText = textToChange;
    textField.setStyle("color", "#00000");
}

Of course, that will require that you put a class variable for textToChange. If that doesn't work, you can use an anonymous listener function:

highlightTextForDuration.addEventListener(TimerEvent.TIMER_COMPLETE, function(e:TimerEvent):void {
  var textField:RichText = Texter;
  textField.setStyle("color", "#00000");
});
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top