需要帮助创建岩石以在随机位置生成

Need help creating rocks to spawn on random location

我正在制作一个基本标准级别的 Bullet Hell Shooter 游戏,我正在使用 AS3 并且不想使用任何包。我是一名游戏设计专业的学生,​​我的技能真的很低。我一直在尝试寻找从不同地方生成的落石,但我得到的只是一块岩石中的 10 块岩石,它们在离开屏幕后不会重新生成。

我当前的代码是:

    var background = new Background;

background.x = 200;
background.y = 200;

addChild(background);

var ship = new Ship

ship.x = 125;
ship.y = 350;

addChild(ship);



var rock = new Rock

var result:Number = Math.random() * 100 
var randomX:Number = Math.random() * stage.stageWidth
rock.x = randomX;

addChild(rock);

addEventListener(Event.ENTER_FRAME, fl_EnterFrameHandler_2);

function fl_EnterFrameHandler_2(event:Event):void
{
    rock.y +=5;
}






ship.gotoAndStop("normal");


stage.addEventListener(KeyboardEvent.KEY_DOWN,fl_KeyboardDownHandler);

function fl_KeyboardDownHandler(event:KeyboardEvent):void

{

        if (event.keyCode == Keyboard.RIGHT)

        {

        ship.x +=5;

        }
          if (event.keyCode == Keyboard.LEFT)

        {

        ship.x -=5;

        }
}

stage.addEventListener(Event.ENTER_FRAME,detectCollision);
function detectCollision(event:Event)
{
         if(ship.hitTestObject(rock))
     {
         ship.gotoAndPlay("boom");
         ship.stop();
         stage.removeEventListener(KeyboardEvent.KEY_DOWN,fl_KeyboardDownHandler);

     }
}

你需要这样的东西:

var rocks = new Array();
// Create 10 rocks
for ( var i = 0; i < 10; ++i )
{
   var rock = new Rock();
   rock.x = Math.random() * stage.stageWidth;
   rock.y = 0;
   rocks.push( rock );
}

stage.addEventListener(Event.ENTER_FRAME,stepDown);
function stepDown(event:Event)
{
   // step down each rock
   for ( var i = 0; i < 10; ++i )
   {
      rocks[i].y += 5;
       // Check if rocks fall from stage than set it on new position
      if ( stage.stageHeight < rocks[i].y )
      {
         rocks[i].y = 0;
         rocks[i].x = Math.random() * stage.stageWidth
      }
   }
}

这里可能有几处错误,不过大家可以顺便理解一下