Actionscript 3 垂直射手AS3中的随机敌人

Actionscript 3 垂直射手AS3中的随机敌人,actionscript-3,flash,actionscript,Actionscript 3,Flash,Actionscript,我正在做一个垂直射击游戏。我需要找到一种方法,使随机对象(敌人)从顶部下降。目前我有1个敌人从顶部随机掉落,正在工作。这是我正在使用的代码 主代码 // Full Screen Command fscommand("fullscreen","true"); stop(); //center player on screen mcMain.x = 880,45; mcMain.y = 940,40; //these booleans will check which keys are down

我正在做一个垂直射击游戏。我需要找到一种方法,使随机对象(敌人)从顶部下降。目前我有1个敌人从顶部随机掉落,正在工作。这是我正在使用的代码

主代码

// Full Screen Command
fscommand("fullscreen","true");
stop();

//center player on screen
mcMain.x = 880,45;
mcMain.y = 940,40;

//these booleans will check which keys are down
var leftDown:Boolean = false;
var upDown:Boolean = false;
var rightDown:Boolean = false;
var downDown:Boolean = false;
//how fast the character will be able to go
var mainSpeed:int = 25;

//how much time before allowed to shoot again
var cTime:int = 0;
//the time it has to reach in order to be allowed to shoot (in frames)
var cLimit:int = 12;
//whether or not the user is allowed to shoot
var shootAllow:Boolean = true;

//how much time before another enemy is made
var enemyTime:int = 0;
//how much time needed to make an enemy
//it should be more than the shooting rate
//or else killing all of the enemies would
//be impossible :O
var enemyLimit:int = 16;

//the player's score
var score:int = 0; 

//this movieclip will hold all of the bullets
var bulletContainer:MovieClip = new MovieClip();
addChild(bulletContainer);

//whether or not the game is over
var gameOver:Boolean = false;

//adding a listener to mcMain that will move the character
mcMain.addEventListener(Event.ENTER_FRAME, moveChar);
function moveChar(event:Event):void{
//checking if the key booleans are true then moving
//the character based on the keys
if(leftDown){
    mcMain.x -= mainSpeed;
}
if(upDown){
    mcMain.y -= mainSpeed;
}
if(rightDown){
    mcMain.x += mainSpeed;
}
if(downDown){
    mcMain.y += mainSpeed;
}
//keeping the main character within bounds
if(mcMain.x <= 0){
    mcMain.x += mainSpeed;
}
if(mcMain.y <= 0){
    mcMain.y += mainSpeed;
}
if(mcMain.x >= stage.stageWidth - mcMain.width){
    mcMain.x -= mainSpeed;
}
if(mcMain.y >= stage.stageHeight - mcMain.height){
    mcMain.y -= mainSpeed;
}

    //Incrementing the cTime

//checking if cTime has reached the limit yet
if(cTime < cLimit){
    cTime ++;
} else {
    //if it has, then allow the user to shoot
    shootAllow = true;
    //and reset cTime
    cTime = 0;
}
//adding enemies to stage
if(enemyTime < enemyLimit){
    //if time hasn't reached the limit, then just increment
    enemyTime ++;
} else {
//defining a variable which will hold the new enemy
var newEnemy = new Enemy();
//making the enemy offstage when it is created
newEnemy.y = -1 * newEnemy.height;
//making the enemy's x coordinates random
//the "int" function will act the same as Math.floor but a bit faster
newEnemy.x = int(Math.random()*(stage.stageWidth - newEnemy.width));
//then add the enemy to stage
addChild(newEnemy);
//and reset the enemyTime
enemyTime = 0;
}
//updating the score text
txtScore.text = 'SCORE: '+score;
}
//this listener will listen for down keystrokes
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
function checkKeysDown(event:KeyboardEvent):void{
//making the booleans true based on the keycode
//WASD Keys or arrow keys
if(event.keyCode == 37 || event.keyCode == 65){
    leftDown = true;
}
if(event.keyCode == 39 || event.keyCode == 68){
    rightDown = true;
}

//checking if the space bar is pressed and shooting is allowed
if(event.keyCode == 32 && shootAllow){
//making it so the user can't shoot for a bit
shootAllow = false;
//declaring a variable to be a new Bullet
var newBullet:Bullet = new Bullet();
//changing the bullet's coordinates
newBullet.x = mcMain.x + mcMain.width/2 - newBullet.width/2;
newBullet.y = mcMain.y;
//then we add the bullet to stage
bulletContainer.addChild(newBullet);
mcMain.gotoAndPlay(1);
}
}
//this listener will listen for keys being released
stage.addEventListener(KeyboardEvent.KEY_UP, checkKeysUp);
function checkKeysUp(event:KeyboardEvent):void{
//making the booleans false based on the keycode
if(event.keyCode == 37 || event.keyCode == 65){
    leftDown = false;
}
if(event.keyCode == 39 || event.keyCode == 68){
    rightDown = false;
}

}
//全屏命令
fscommand(“全屏”、“真”);
停止();
//在屏幕上居中播放
mcMain.x=880,45;
mcMain.y=940,40;
//这些布尔值将检查哪些键已按下
var leftDown:Boolean=false;
var upDown:Boolean=false;
var rightDown:Boolean=false;
变量向下:布尔值=false;
//角色能跑多快
var主速度:int=25;
//多少时间后才允许再次拍摄
变量cTime:int=0;
//为了允许拍摄,它必须达到的时间(以帧为单位)
var-cLimit:int=12;
//是否允许用户进行拍摄
var shootAllow:Boolean=true;
//还有多少时间会有另一个敌人
var-enemyTime:int=0;
//制造敌人需要多少时间
//这应该比射门率高
//否则杀死所有的敌人都会
//不可能:哦
var enemyLimit:int=16;
//球员的得分
变量得分:int=0;
//这张电影胶片可以容纳所有的子弹
var bulletContainer:MovieClip=新的MovieClip();
addChild(bulletContainer);
//不管比赛是否结束
var gameOver:Boolean=false;
//向mcMain添加将移动角色的侦听器
mcMain.addEventListener(Event.ENTER_FRAME,moveChar);
函数moveChar(事件:事件):void{
//检查键布尔值是否为真,然后移动
//基于关键点的字符
如果(左下){
mcMain.x-=主速度;
}
如果(向上向下){
mcMain.y-=主速度;
}
如果(右下){
mcMain.x+=主速度;
}
如果(向下){
mcMain.y+=主速度;
}
//保持主角在一定范围内
如果(mcMain.x=stage.stageHeight-mcMain.height){
mcMain.y-=主速度;
}
//增加cTime
//检查cTime是否已达到限制
if(cTime
敌级

package{
//we have to import certain display objects and events
import flash.display.MovieClip;
import flash.events.*;
//this just means that Enemy will act like a MovieClip
public class Enemy extends MovieClip{
    //VARIABLES
    //this will act as the root of the document
    //so we can easily reference it within the class
    private var _root:Object;
    //how quickly the enemy will move
    private var speed:int = 5;
    //this function will run every time the Bullet is added
    //to the stage
    public function Enemy(){
        //adding events to this class
        //functions that will run only when the MC is added
        addEventListener(Event.ADDED, beginClass);
        //functions that will run on enter frame
        addEventListener(Event.ENTER_FRAME, eFrame);
    }
    private function beginClass(event:Event):void{
        _root = MovieClip(root);
    }
    private function eFrame(event:Event):void{
        //moving the bullet up screen
        y += speed;
        //making the bullet be removed if it goes off stage
        if(this.y > stage.stageHeight){
            removeEventListener(Event.ENTER_FRAME, eFrame);
            _root.removeChild(this);
        }
        //checking if it is touching any bullets
        //we will have to run a for loop because there will be multiple bullets
        for(var i:int = 0;i<_root.bulletContainer.numChildren;i++){
            //numChildren is just the amount of movieclips within 
            //the bulletContainer.

            //we define a variable that will be the bullet that we are currently
            //hit testing.
            var bulletTarget:MovieClip = _root.bulletContainer.getChildAt(i);

            //now we hit test
            if(hitTestObject(bulletTarget)){
                //remove this from the stage if it touches a bullet
                removeEventListener(Event.ENTER_FRAME, eFrame);
                _root.removeChild(this);
                //also remove the bullet and its listeners
                _root.bulletContainer.removeChild(bulletTarget);
                bulletTarget.removeListeners();
                //up the score
                _root.score += 5;
            }
        }

        //hit testing with the user
        if(hitTestObject(_root.deadLine)){
            //losing the game
            _root.gameOver = true;
            _root.gotoAndStop('lose');
        }

        //checking if game is over
        if(_root.gameOver){
            removeEventListener(Event.ENTER_FRAME, eFrame);
            this.parent.removeChild(this);
        }

    }
}
}
包{
//我们必须导入某些显示对象和事件
导入flash.display.MovieClip;
导入flash.events.*;
//这只意味着敌人会像电影演员一样表演
公众阶级的敌人延伸到电影界{
//变数
//这将作为文档的根
//因此,我们可以很容易地在类中引用它
私有变量根:对象;
//敌人移动得有多快
专用var速度:int=5;
//每次添加项目符号时,此函数都将运行
//上台
公共职能(){
//将事件添加到此类
//仅在添加MC时运行的函数
addEventListener(Event.ADDED,beginClass);
//将在enter frame上运行的函数
addEventListener(Event.ENTER_FRAME,eFrame);
}
私有函数beginClass(事件:事件):无效{
_根=MovieClip(根);
}
私有函数eFrame(事件:事件):无效{
//将子弹向上移动屏幕
y+=速度;
//让子弹离开舞台时被移除
if(this.y>stage.stageHeight){
removeEventListener(Event.ENTER_FRAME,eFrame);
_根。removeChild(本);
}
//检查它是否碰到子弹
//我们将不得不运行for循环,因为将有多个项目符号

对于(var i:int=0;i我曾经在flash中制作游戏,并使用此条件不断在游戏中添加敌人

if (Math.floor(Math.random() * 90) == 5){

在这种情况下,您可以使用您的代码随机添加1个敌人

我认为您应该在主代码中连接一个计时器类

找一个全局计数器

   int count=0;
   int timeElapsedToNewEnemy = int((Math.random()*10+1)*10);
   //get u a number from 10 to 100
拿你的计时器

   var time:Timer=new Timer(100,0);// each 1/10 seconds 
   time.start();
   time.addEventListener(TimerEvent.TIMER, countTime);
获取计时器的ur函数

   function countTime(e:TimerEvent):void
   {//increment ur timer
    count++;
if  (cont == timeElapsedToNewEnemy)
     {
    cont=0;//restar ur counter..
            //createUrNewEnemyHere  
            //get a new timeElapsedToEnemy
            timeElapsedToNewEnemy = int((Math.random()*10+1)*10);
     }
    }
现在你可以很容易地调整它来创建随机的敌人每随机x时间


让我知道它是否适用于u

谢谢大家,但我找到了一个解决方案

var newEnemy = new (getDefinitionByName("Enemy"+Math.floor(Math.random()*4)));

然后创建了不同的敌人电影唇作为敌人

你需要更多的敌人,主要是在图形方面,然后,当你初始化另一个敌人时,它应该