我仍在努力as3。我的敌人不会从右向左移动。另一方没问题。有人不知道我做错了吗?跟踪goleft是工作。
package {
import flash.display.MovieClip;
import flash.events.Event;
public class mcEnemy extends MovieClip {
public var sDirection:String;
private var nSpeed:Number;
public function mcEnemy()
{
addEventListener(Event.ADDED_TO_STAGE, onAdd);
}
private function onAdd (e:Event): void
{
removeEventListener(Event.ADDED_TO_STAGE, onAdd);
init();
}
//radom enemy's worden gekozen
private function init ():void
{
// 3 frames
var nEnemies:Number = 3;
// pick random number between 1 and number of enemies
var nRandom:Number = randomNumber (1, nEnemies);
// Setup our playhead of this enemy clip to a random number
// Stop op frame 1,2 of 3
this.gotoAndStop(nRandom);
// Setup our enemys start position
setupStartPosition();
}
private function setupStartPosition (): void
{
// pick a random speed for the enemy
nSpeed = randomNumber (5,10);
// Pick random number for left or right, tussen 1 en 2, start position
var nLeftOrRight:Number = randomNumber (1,2);
// if our nLeftOrRight == 1 , enemy is on the left
if (nLeftOrRight == 1)
{
// start enemy on the left side
this.x = - (this.width/2);
sDirection = "R";
//trace ("right");
} else
{
// start enemy on the right side
this.x = stage.stageWidth + (this.width/2);
sDirection = "L";
//trace("left");
}
// set a random hoogte for our enemy
// set a 2 varibele for min and max hoogte
var nMinAltitude: Number = stage.stageHeight/2;
var nMaxAltitude: Number = 720 - (this.height/2);
// Setup our enemies altitude to a random point between our min and max altitudes
this.y = randomNumber (nMinAltitude, nMaxAltitude);
// move our enemy
startMoving ();
}
private function startMoving (): void
{
addEventListener(Event.ENTER_FRAME, enemyLoop)
}
private function enemyLoop (e:Event ): void
{
// test in what direction our enemy is moving
// if our enemy is moving right
if (sDirection == "R")
{
// move our enemy right
this.x += nSpeed;
//trace ("goright");
} else
{
// move our enemy left
this.x -= nSpeed;
//trace ("goleft");
}
}
// geeft random nummer tussen 0 en 1 en stuurt het terug
function randomNumber (low:Number=0, high:Number=1) : Number
{
return Math.floor (Math.random() * (1+high-low)) + low;
}
public function destroyEnemy (): void
{
// remove enemys from the stage
if (parent) {
parent.removeChild(this);
}
// remove any eventlisteners from enemy
removeEventListener(Event.ENTER_FRAME, enemyLoop);
}
}
}
我希望有人可以帮助我。抱歉我的英语不好。
答案 0 :(得分:0)
当它向左移动时会发生什么?是不是总是向右移动,或者它有时是正确的,有时根本不移动?
答案 1 :(得分:0)
据我所知和所想,是" var nLeftOrRight:Number = randomNumber(1,2);"创建一个介于1和2之间的随机数,带有分贝(例如1.2929291),然后你将它变成一个int,这使得1真的很高的可能性,以及2真的很低的几率...,如果这是case不要使randomNumber(1,2),但randomNumber(1,3),然后抓住可能的' 3你需要制作一个if for 3,然后把它变成e 1或2 ......
关于随机数生成问题,我可能会错,但是如果我提供错误的信息,请原谅我