0
我有一個導入到我的舞臺的角色,但我想要一個名爲「隨機」的按鈕,當它被點擊時,每次點擊它時左右移動角色。這是我的嘗試,但角色會對角移動。隨機將一個角色移動到閃存中
//import the code to use the components
import fl.events.ComponentEvent;
import flash.utils.Timer;
import flash.events.TimerEvent;
//stay on this frame
stop();
//declare variables
var RandomNumber:Number = Math.floor(Math.random() * 5) -5;
var XMove:Number;
var YMove:Number;
var tmrMove:Timer = new Timer(25);
btnRandom.addEventListener(ComponentEvent.BUTTON_DOWN, RandomNum);
//listen for timer to tick
tmrMove.addEventListener(TimerEvent.TIMER, onTick);
function RandomNum(e:ComponentEvent) {
XMove = RandomNumber;
YMove = RandomNumber;
tmrMove.start()
}
//function for timer
function onTick (e:TimerEvent) {
//move the ninja
picNinja.x = picNinja.x + XMove;
picNinja.y = picNinja.y + YMove;
//check if ninja goes off stage
if (picNinja.x > stage.stageWidth) {
picNinja.x = 0;
}
if (picNinja.x < 0) {
picNinja.x = stage.stageWidth;
}
if(picNinja.y > stage.stageHeight) {
picNinja.y = 0;
}
if(picNinja.y < 0) {
picNinja.y = stage.stageHeight;
}
//function to stop the timer
function stopApp (e:ComponentEvent) {
tmrMove.stop();
}
我以爲分配一個隨機值可以工作(介於5和-5之間),但在這裏肯定是錯誤的。