Search code examples
actionscript-3flashactionscriptflash-cs4

AS3 : displaying multiple (unlimited) achievements for 2 seconds each


I'm working on a very simple (1st project) "game" with various achievements. In fact, that's pretty much all there is to the game.

Let's say we have 3 possible achievements:

  • Game started
  • You played 5 seconds
  • You played 6 seconds.

(I know, I know, I can hear you: Wow, looks like a super fun game concept ! hehe)

I'm trying to build a generic function in charge of displaying the achievement message on the screen, and remove it 2 seconds after it had been displayed. It mean multiple achievement can be displayed at the same time. But each should disapear after their own 2 seconds.

I tried to instanciate a new timer for each achievement whitout success. The problem is that each time need a name, and a callback function, and I have no way to link a timer to an achievement message since I have to use the callback function. I'm sure I'm just doing it wrong since it's my first project, so I'm asking for some help.

Here's the code I have to display an achievement:

var genericTimer:Array = new Array();
function displayAchievement(msg){
    var myTextBox:TextField = new TextField();    
    myTextBox.text = msg;
    myTextBox.x = 50;    
    myTextBox.y = 20; 
    addChild(myTextBox);

    genericTimer[] = new Timer(2000, 1);
    //Make myTextBox (of this instance only) disapear after 2 seconds
}

Solution

  • You can achieve this by the help of anonymous function. Personally I don't like them, but such approach will give you minimum of code:

    function displayAchievement(msg:String):void {
        var myTextBox:TextField = new TextField();
        myTextBox.text = msg;
        myTextBox.x = 50;
        myTextBox.y = 20;
        addChild(myTextBox);
    
        var timer:Timer = new Timer(2000, 1);
        timer.addEventListener(TimerEvent.TIMER_COMPLETE, function (e:TimerEvent):void {
            removeChild(myTextBox);
        });
        timer.start();
    }