Actionscript 3 Flash-在特定设定点随机向场景添加对象

Actionscript 3 Flash-在特定设定点随机向场景添加对象,actionscript-3,flash,Actionscript 3,Flash,我正在使用ActionScript3.0创建一个简单的Flash游戏,在场景中产生障碍物时遇到了一个问题。我的目标是在x轴上有大约10个点(保持在相同的y轴上),当在我的场景中生成障碍物时,它将随机拾取其中的2-4个点并在其上生成障碍物 我有随机繁殖的障碍,但无法从列表中找出如何使它们在随机设定点繁殖。如果有人能帮忙,我将不胜感激。谢谢 编辑: 到目前为止,我掌握的代码是: var a:Array = new Array(); for (var count=0; count< 5; cou

我正在使用ActionScript3.0创建一个简单的Flash游戏,在场景中产生障碍物时遇到了一个问题。我的目标是在x轴上有大约10个点(保持在相同的y轴上),当在我的场景中生成障碍物时,它将随机拾取其中的2-4个点并在其上生成障碍物

我有随机繁殖的障碍,但无法从列表中找出如何使它们在随机设定点繁殖。如果有人能帮忙,我将不胜感激。谢谢

编辑:

到目前为止,我掌握的代码是:

var a:Array = new Array();
for (var count=0; count< 5; count++) {
        a[count] = new asteroidOne();
        a[count].x = 100 * count + (Math.floor(Math.random() * 200));
        a[count].y = 100;
        addChild(a[count]);
}

// Asteroid obstacle spawning 2.0

player.addEventListener(Event.ENTER_FRAME, obstacleMove);
function obstacleMove(evt:Event):void {
        for (var i=0; i< 5; i++) {
                a[i].y += 5;
                if (a[i].y == 480) {
                        a[i].y = 0;    
                }
                if (player.hitTestObject(a[i])) {
                        trace("HIT");
                }
        }
}
var a:Array=new Array();
对于(变量计数=0;计数<5;计数++){
a[计数]=新的一个();
a[count].x=100*count+(Math.floor(Math.random()*200));
a[count].y=100;
addChild(a[计数]);
}
//小行星障碍繁殖2.0
player.addEventListener(Event.ENTER_FRAME,obstacleMove);
函数obstacleMove(evt:事件):无效{
对于(变量i=0;i<5;i++){
a[i].y+=5;
如果(a[i].y==480){
a[i].y=0;
}
if(player.hitTestObject(a[i])){
跟踪(“命中”);
}
}
}

假设阵列中有繁殖点,可以执行以下操作:

var spawnPoints:Array = [100,200,250,300,450,500,600,800]; //your list of spawn x locations
spawnPoints.sort(randomizeArray); //lets randomize the spwanPoints

function randomizeArray(a:*, b:*):int {
    return ( Math.random() < .5 ) ? 1 : -1;
}

var a:Vector.<asteroidOne> = new Vector.<asteroidOne>(); //the array for your astroids - changed to vector for possible performance and code hint improvement (basically the same as Array but every object has to be of the specified type)

for (var count:int=0; count < 5; count++) {
        a.push(new asteroidOne());
        a[count].x = spawnPoints.pop(); //pop removes the last element from the array and returns it
        a[count].y = 100;
        addChild(a[count]);
}
var-spawnPoints:Array=[100200250300450500600800]//您的繁殖x位置列表
spawnPoints.sort(randomizarray)//让我们将SPWAN点随机化
函数随机化数组(a:*,b:*):int{
返回值(Math.random()<.5)?1:-1;
}
变量a:向量新向量()//你的星象阵列-为了可能的性能和代码提示改进,改为vector(基本上与array相同,但每个对象都必须是指定的类型)
对于(变量计数:int=0;计数<5;计数++){
a、 推(新的一个());
a[count].x=spawnPoints.pop();//pop从数组中删除最后一个元素并返回它
a[count].y=100;
addChild(a[计数]);
}

编辑 为了回应您的评论,这里有一个不错的例子:

import flash.events.Event;
import flash.events.TimerEvent;
import flash.utils.Timer;

var spawnTimer:Timer = new Timer(10000); //timer will tick every 10 seconds
spawnTimer.addEventListener(TimerEvent.TIMER, spawn, false, 0, true); //let's run the spawn function every timer tick
spawnTimer.start();

var spawnPoints:Array = [100,200,250,300,450,500,600,800]; //your list of spawn x locations
var spawnAmount:int = 5; //how many asteroids to have on the screen at once (you could increase this over time to make it more difficult)
var asteroids:Vector.<asteroidOne> = new Vector.<asteroidOne>(); //the array for your asteroids - changed to vector for possible performance and code hint improvement (basically the same as Array but every object has to be of the specified type)

spawn(); //lets call it right away (otherwise it will won't be called until the first timer tick in 10 seconds)

//calling this will spawn as many new asteroids as are needed to reach the given amount
function spawn(e:Event = null):void {
    if(asteroids.length >= spawnAmount) return; //let's not bother running any of the code below if no new asteroids are needed
    spawnPoints.sort(randomizeArray); //lets randomize the spwanPoints
    var spawnIndex:int = 0;

    var a:asteroidOne; //var to hold the asteroid every loop
    while (asteroids.length < spawnAmount) {
        a = new asteroidOne();
        a.x = spawnPoints[spawnIndex];
        spawnIndex++; //incriment the spawn index
        if (spawnIndex >= spawnPoints.length) spawnIndex = 0; //if the index is out of range of the amount of items in the array, go back to the start

        a.y = 100;
        asteroids.push(a); //add it to the array/vector
        addChild(a); //add it to the display 
    }
}

player.addEventListener(Event.ENTER_FRAME, obstacleMove);
function obstacleMove(evt:Event):void {

    //this is the same as a backwards for loop - for(var i:int=asteroids.length-1;i >= 0; i--)
    var i:int = asteroids.length;
    while(i--){  //since we are potentially removing items from the array/vector, we need to iterate backwards - otherwise when you remove an item, the indices will have shifted and you'll eventually get an out of range error
        asteroids[i].y += 5;
        if (asteroids[i].y > stage.stageHeight || asteroids[i].x > stage.stageWidth || asteroids[i].x < -asteroids[i].width || asteroids[i].y < -asteroids[i].height) {
            //object is out of the bounds of the stage, let's remove it

            removeChild(asteroids[i]); //remove it from the display
            asteroids.splice(i, 1); //remove it from the array/vector

            continue; //move on to the next iteration in the for loop
        }

        if (player.hitTestObject(asteroids[i])) {
            trace("HIT");
        }
    }
}

function randomizeArray(a:*, b:*):int {
    return ( Math.random() < .5 ) ? 1 : -1;
}
导入flash.events.Event;
导入flash.events.TimerEvent;
导入flash.utils.Timer;
变量spawnTimer:计时器=新计时器(10000)//计时器将每10秒滴答一次
spawnTimer.addEventListener(TimerEvent.TIMER,spawn,false,0,true)//让我们每个计时器都运行spawn函数
spawnTimer.start();
变量点:数组=[100200250300450500600800]//您的繁殖x位置列表
风险值金额:int=5//一次屏幕上有多少小行星(你可以随着时间的推移增加,使其更难显示)
var小行星:矢量新向量()//小行星阵列-更改为vector以提高性能和代码提示(基本上与阵列相同,但每个对象都必须为指定类型)
繁殖()//让我们立即调用它(否则,直到10秒后第一个计时器滴答响时才会调用它)
//调用此命令将产生达到给定数量所需的尽可能多的新小行星
函数spawn(e:Event=null):void{
if(asteroids.length>=spawnmount)return;//如果不需要新的asteroids,我们就不用麻烦运行下面的任何代码了
spawnPoints.sort(randomizarray);//让我们将spwanPoints随机化
var-index:int=0;
var a:asteroidOne;//var用于保存小行星的每个循环
while(小行星长度<数量){
a=新的一个();
a、 x=繁殖点[繁殖索引];
spawnIndex++;//说明生成索引
如果(spawnIndex>=spawnimpoints.length)spawnIndex=0;//如果索引超出数组中的项数范围,请返回开始
a、 y=100;
asternos.push(a);//将其添加到数组/向量中
addChild(a);//将其添加到显示器
}
}
player.addEventListener(Event.ENTER_FRAME,obstacleMove);
函数obstacleMove(evt:事件):无效{
//这与向后for循环for相同(var i:int=asteroids.length-1;i>=0;i--)
var i:int=小行星长度;
虽然(i--){//因为我们可能会从数组/向量中删除项,所以我们需要向后迭代-否则,当您删除项时,索引将发生偏移,最终会出现超出范围的错误
小行星[i].y+=5;
if(小行星[i].y>stage.stageHeight | | |小行星[i].x>stage.stageWidth | | | |小行星[i].x<-小行星[i].宽度| |小行星[i].y<-小行星[i].高度){
//对象超出舞台的范围,让我们将其移除
removeChild(小行星[i]);//将其从显示中删除
splice(i,1);//将其从数组/向量中删除
continue;//转到for循环中的下一个迭代
}
if(player.hitTestObject(小行星[i])){
跟踪(“命中”);
}
}
}
函数随机化数组(a:*,b:*):int{
返回值(Math.random()<.5)?1:-1;
}

你能告诉我你的代码哪里有问题吗?我写的代码没有问题,因为我不知道具体怎么做。我已经编辑了我的第一篇文章,其中包含了到目前为止我所拥有的代码。我不明白你想要实现什么?有一个循环可以创建5个小行星并将它们填充到一个数组中,然后将它们的x设置为随机值。然后每帧将它们向下移动5个像素。你还想做什么?您是否试图使起始星象x不完全随机,而是从可接受的x起始点列表中选择一个值?如果是,只需创建一个包含所有可接受起始点的数组/向量,将其随机化,并在您的星象创建循环中每次迭代都弹出该数组。如果还需要的话,我明天会发布一个例子。那我该怎么做呢?我对这个还是有点陌生,所以我不太确定如何从中随机挑选。一个例子是incr