0

我有一个简单的城市动画,一辆车从屏幕上穿过,一个球向左滚动。动画由 ENTER_FRAME 事件运行,但我想使用计时器,但我无法弄清楚。这是我的代码:

package {
import flash.display.*;
import flash.events.*;
import flash.utils.*;


public class cityApp extends MovieClip {
    function cityApp() {
        //TASK 1 : INITIALIZE THE MOVIECLIPS ON STAGE
        Car.x = 10;
        Ball.x = 500;
        Sunset.alpha = 0;

        //TASK 2: ADD A LISTENER TO LOOP THROUGH THE FRAME
        var atimer:Timer= new Timer(500, 30);
        atimer.addEventListener(TimerEvent.TIMER, CityMoves);
    }

    function CityMoves(event:TimerEvent):void {
        //TASK 1:  
        // A. DRIVE THE CAR ACROSS THE STAGE BY INCREMENTING ITS 
        // X POSITION BY 2
        Car.x += 2;
        //B. ROTATE THE WHEELS OF THE CAR
        Car.Wheel1.rotation+=3;
        Car.Wheel2.rotation+=3;

        //TASK 2: MOVE THE BALL IN THE OPPOSITE POSITION OF THE CAR
        //ROTATE THE BALL SO THAT IT APPEARS TO BE ROLLING
        Ball.x -= 2;
        Ball.rotation-= 5;

        //TASK 3:  THE EVENING SUNSET GRADUALLY APPEARS AS ITS ALPHA INCREASES.
        Sunset.alpha += .007;
    }
}

} 任何帮助深表感谢。

4

2 回答 2

1

应该启动计时器。

atimer.start();
于 2013-10-22T14:33:34.120 回答
0

首先使计时器成为成员变量(从技术上讲,只需确保您保留它的实时引用)。然后设置好后启动它:

package {
import flash.display.*;
import flash.events.*;
import flash.utils.*;


public class cityApp extends MovieClip {
    private var atimer:Timer; // change 1/3

    function cityApp() {
        //TASK 1 : INITIALIZE THE MOVIECLIPS ON STAGE
        Car.x = 10;
        Ball.x = 500;
        Sunset.alpha = 0;

        //TASK 2: ADD A LISTENER TO LOOP THROUGH THE FRAME
        atimer = new Timer(500, 30); // change 2/3
        atimer.addEventListener(TimerEvent.TIMER, CityMoves);
        atimer.start(); // change 3/3
    }

    function CityMoves(event:TimerEvent):void {
        //TASK 1:  
        // A. DRIVE THE CAR ACROSS THE STAGE BY INCREMENTING ITS 
        // X POSITION BY 2
        Car.x += 2;
        //B. ROTATE THE WHEELS OF THE CAR
        Car.Wheel1.rotation+=3;
        Car.Wheel2.rotation+=3;

        //TASK 2: MOVE THE BALL IN THE OPPOSITE POSITION OF THE CAR
        //ROTATE THE BALL SO THAT IT APPEARS TO BE ROLLING
        Ball.x -= 2;
        Ball.rotation-= 5;

        //TASK 3:  THE EVENING SUNSET GRADUALLY APPEARS AS ITS ALPHA INCREASES.
        Sunset.alpha += .007;
    }
}
于 2013-10-22T14:38:37.047 回答