2015-12-21 75 views
0

我正在製作一個學校項目的遊戲,您可以跳過對象並收集其他人。我在時間線上完成了這一切,所以.swf文件是唯一需要打開的文件。如何在Action Script 3中生成多個隨機產生的滾動對象?

這是當前的代碼;最後一部分,「產卵的東西」是我迷失的地方。

import flash.events.Event; 
import flash.events.KeyboardEvent; 

stop(); 



//intro screen 
play_btn.addEventListener(MouseEvent.CLICK, clickAway); 
function clickAway(event:MouseEvent):void 
{ 
    introScreen.visible = false; 
    play_btn.visible = false; 
} 

//jump 
var grav:Number = 8; 
var jumping:Boolean = false; 
var jumpPow:Number = 0; 

stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown); 
stage.addEventListener(Event.ENTER_FRAME, update); 

function onKeyDown(evt:KeyboardEvent):void 
{ 
    if(evt.keyCode == Keyboard.UP) 
    { 
     if(jumping != true) 
     { 
      jumpPow = -50; 
      jumping = true; 
     } 

    } 
} 


function update(evt:Event):void 
{ 
    if(jumping) 
    { 
     okami_mc.y += jumpPow; 
     jumpPow += grav; 

     if(okami_mc.y >=300) 
     { 
      jumping = false; 
      okami_mc.y =300; 
     } 

    } 
} 


//make the background move 
stage.addEventListener(Event.ENTER_FRAME, repeat); 
function repeat(thing:Event):void 
{ 
    treebg_mc.x -= 13; 

    if (treebg_mc.x < -100) 
    { 
     treebg_mc.x = 1000; 
    } 

    sun_mc.x -= 5; 
    if (sun_mc.x < -100) 
    { 
     sun_mc.x = 700; 
    } 
} 

//spawning things 
bdemon_mc.addEventListener(Event.ENTER_FRAME, bscrollMove); 
function bscrollMove(event:Event):void 
{ 
    if (bdemon_mc.x>=-100){ 
     bdemon_mc.x -=10; 
    }else{ 
     bdemon_mc.x=600; 
    } 
} 

回答

0

您可以在舞臺上創建假設網格並隨機化網格上的位置。

var randomPoint:RandomPlus = new RandomPlus(0, 4);//number of array elements 
var positionXArr:Array  = [0, 50, 100, 150, 200]; 
var positionYArr:Array  = [200, 300, 400, 500, 600]; 
var obstaclesArr:Array  = new Array;//push the object that blocks 

private function RandomPositioning():void 
{ 
    if(obstaclesArr != null) 
    { 
     obstaclesArr[0].x = posArray[randomPoint.getNum()];//it is just a random method that ranges between 0 and length of the array. 
    } 
} 

,這是由ADI feiwel隨機類

package 
{ 
    public class RandomPlus 
    { 
     private var _st:Number; 
     private var _en:Number; 
     private var _len:Number; 
     private var _pos:Number; 
     private var _numPos:Number; 
     private var _myNums:Array; 
     private var _randNums:Array; 

     public function RandomPlus(en:Number, st:Number = 0) 
     { 
      _st = st; 
      _en = en; 

      // just in case if params order mixup: 
      if(en < st){ 
       _st = en; 
       _en = st; 
      } 

      _len = _en - _st + 1; 
      shuffle(); 
     } 

     public function getNum():Number 
     { 
      // if passed last item: 
      if(_numPos == _len)shuffle(); 

      var myResult:Number = _randNums[_numPos]; 
      _numPos++; 
      return myResult; 
     } 

     private function shuffle():void 
     { 
      _numPos = 0; 
      _randNums = []; 
      _myNums = []; 

      // Creating Numbers Array: 
      var i:Number; 
      for(i = 0; i<_len; i++){ _myNums[i] = _st + i; } 

      // Creating shuffled Numbers Array: 
      i = 0; 
      while(_myNums.length > 0){ 
       _pos = Math.round(Math.random()*(_myNums.length-1)); 
       _randNums[i] = _myNums[_pos]; 
       i++; 
       _myNums.splice(_pos,1); 
      } 
     } 

     public function get len():Number 
     { 
      return _len; 
     } 

    } 

} 
+0

不知道這是什麼意思 但肯定 – sky

+1

爲什麼你會接受一個答案,如果你不知道這是什麼意思? – BotMaster

+0

@sky請修改您的問題(也許重寫)。因爲你的問題和答案有問題。我正在考慮和BotMaster一樣的評論。澄清問題,不要急於接受正確答案。 – coner