我有一个简单的城市动画,车子正对着屏幕,左边滚着一个球。这个动作是由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;
}
}
} 非常感谢任何帮助。
答案 0 :(得分:1)
应该启动计时器。
atimer.start();
答案 1 :(得分: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;
}
}