随机移动 flash 中的一个字符
Move a character in flash randomly
我有一个角色导入到我的舞台,但我想要一个名为 "Random" 的按钮,当您点击它时,每次单击它时,角色会向左或向右上下移动。这是我尝试过的方法,但角色只会沿对角线移动。
//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 之间),但这里肯定有问题。
在您的代码中,XMove 始终等于 YMove,并且当您调用 RandomNum(e:ComponentEvent) 时,RandomNumber 的值不会改变function.so角色只会沿对角线移动。
试试这个。
function getRandomNumber():Number{
//return a random number in range. Math.random() * (max - min + 1)) + min;
return Math.floor(Math.random() * (5 + 5 + 1)) -5;
}
function RandomNum(e:ComponentEvent) {
XMove = 0;
YMove = 0;
var direction:int = Math.floor(Math.random()*2);//horizontal or Vertical
//either up down left or right
switch(direction){
case 0:
XMove = getRandomNumber();
break;
case 1:
YMove = getRandomNumber();
break;
}
tmrMove.start()
}
我有一个角色导入到我的舞台,但我想要一个名为 "Random" 的按钮,当您点击它时,每次单击它时,角色会向左或向右上下移动。这是我尝试过的方法,但角色只会沿对角线移动。
//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 之间),但这里肯定有问题。
在您的代码中,XMove 始终等于 YMove,并且当您调用 RandomNum(e:ComponentEvent) 时,RandomNumber 的值不会改变function.so角色只会沿对角线移动。
试试这个。
function getRandomNumber():Number{
//return a random number in range. Math.random() * (max - min + 1)) + min;
return Math.floor(Math.random() * (5 + 5 + 1)) -5;
}
function RandomNum(e:ComponentEvent) {
XMove = 0;
YMove = 0;
var direction:int = Math.floor(Math.random()*2);//horizontal or Vertical
//either up down left or right
switch(direction){
case 0:
XMove = getRandomNumber();
break;
case 1:
YMove = getRandomNumber();
break;
}
tmrMove.start()
}