2012-02-13 41 views
0

暈,我正在開發一款迷你flash遊戲。如何從特定的5點隨機創建對象?

遊戲玩法: 玩家控制角色並左右移動以收集錢幣。 有5點,金錢和炸彈釋放,並從上到下移動。當角色使用的錢,然後增加貨幣碰撞1。別人打炸彈年齡的增長一旦年齡是99,比賽已經結束,錢是最後得分

這裏是我的代碼:

package Class 
{ 
    import flash.events.Event; 
    import flash.events.KeyboardEvent; 
    import flash.events.MouseEvent; 
    import flash.media.Sound; 
    import flash.media.SoundChannel; 
    import flash.text.TextField; 
    import flash.ui.Keyboard; 
    import flash.display.MovieClip; 

public class Game extends MovieClip 
{ 
    private var bgi:BGI; 
    private var character:Character; 
    private var money:Money; 
    private var bomb:Bomb; 
    private var moneysource : MoneySource; 
    private var moneytext:TextField; 
    private var agetext:TextField; 
    private var characterspeed:int; 
    private var objectspeed:int; 
    private var age:int; 
    private var moneyscore:int; 
    private var moneyinscreen:int; 
    private var moneyVector:Vector.<MovieClip > = new Vector.<MovieClip >; 
    private var noofmoney:int; 
    private var noofbomb:int; 
    private var bombVector:Vector.<MovieClip > = new Vector.<MovieClip >; 

    public function Game() 
    { 
     bgi = null; 
     character = null; 
     money = null; 
     bomb = null; 
     moneysource = null; 
     Initialize(); 
    } 

    private function Initialize() 
    { 

     age = 0; 
     moneyscore = 0; 
     noofmoney = 15; 
     noofbomb = 10; 

     bgi = new BGI ; 
     bgi.x = 0; 
     bgi.y = 0; 

     character = new Character ; 
     character.x = 335; 
     character.y = 400; 

     bomb = new Bomb ; 
     bomb.x = 40; 
     bomb.y = 0; 



     moneytext = new TextField ; 
     moneytext.x = 450; 
     moneytext.y = 0; 
     moneytext.defaultTextFormat = Config.TxtFormat; 
     moneytext.text = "Money : 0"; 
     moneytext.width = 200; 

     agetext = new TextField ; 
     agetext.x = 700; 
     agetext.y = 0; 
     agetext.defaultTextFormat = Config.TxtFormat; 
     agetext.text = "Age : 0"; 
     agetext.width = 100; 

     Config.CurrentStage.addChild(bgi); 
     Config.CurrentStage.addChild(character); 
     Config.CurrentStage.addChild(agetext); 
     Config.CurrentStage.addChild(moneytext); 

     moneysource = new MoneySource; 
     moneysource.x = 61; 
     moneysource.y = 50; 
     Config.CurrentStage.addChild(moneysource); 

     moneysource = new MoneySource; 
     moneysource.x = 211; 
     moneysource.y = 50; 
     Config.CurrentStage.addChild(moneysource); 

     moneysource = new MoneySource; 
     moneysource.x = 371; 
     moneysource.y = 50; 
     Config.CurrentStage.addChild(moneysource); 

     moneysource = new MoneySource; 
     moneysource.x = 531; 
     moneysource.y = 50; 
     Config.CurrentStage.addChild(moneysource); 

     moneysource = new MoneySource; 
     moneysource.x = 691; 
     moneysource.y = 50; 
     Config.CurrentStage.addChild(moneysource); 

     for (var i:int = 0; i < noofmoney; i++) 
     { 
      money = new Money ; 
      money.vel = RandomRange(6,7); 
      money.x = RandomRange(0,750); 
      money.y = RandomRange(0,100); 
      Config.CurrentStage.addChild(money); 
      moneyVector.push(money); 
     } 

     for (var j:int = 0; j < noofbomb; j++) 
     { 
      bomb = new Bomb ; 
      bomb.vel = RandomRange(6,7); 
      bomb.x = RandomRange(0,750); 
      bomb.y = RandomRange(0,100); 
      Config.CurrentStage.addChild(bomb); 
      bombVector.push(bomb); 
     }*/ 

     Config.CurrentStage.addEventListener(Event.ENTER_FRAME,Update); 
     Config.CurrentStage.addEventListener(KeyboardEvent.KEY_DOWN,Control); 
    } 

    private function Update(evt:Event) 
    { 
     for (var i:int = 0; i < moneyVector.length; i++) 
     { 
      if (moneyVector[i].hitTestObject(character)) 
      { 
       Config.CurrentStage.removeChild(moneyVector[i]); 
       moneyVector.splice(i, 1); 
       moneyscore += 400; 
       moneytext.text = "Money : " + moneyscore.toString(); 
       money = new Money ; 
       //money.vel = RandomRange(6,7); 
       money.x = RandomRange(0,750); 
       money.y = RandomRange(0,100); 
       Config.CurrentStage.addChild(money); 
       moneyVector.push(money); 

      } 
      if (moneyVector[i].y > Config.ScreenHeight) 
      { 
       moneyVector[i].x = RandomRange(0,750); 
       moneyVector[i].y = - moneyVector[i].height; 
      } 
     } 

     for (var j:int = 0; j < bombVector.length; j++) 
     { 
      if (bombVector[j].hitTestObject(character)) 
      { 

       Config.CurrentStage.removeChild(bombVector[j]); 
       bombVector.splice(j, 1); 
       age++; 
       agetext.text = "Age : " + age.toString(); 
       bomb = new Bomb ; 
       bomb.vel = RandomRange(6,7); 
       bomb.x = RandomRange(0,750); 
       bomb.y = RandomRange(0,100); 
       Config.CurrentStage.addChild(bomb); 
       bombVector.push(money); 

      } 
      if (bombVector[j].y > Config.ScreenHeight) 
      { 
       bombVector[j].x = RandomRange(0,750); 
       bombVector[j].y = - bombVector[j].height; 
      } 
     } 
    } 

    private function Control(evt:KeyboardEvent) 
    { 
     characterspeed = 15; 
     if (evt.keyCode == Keyboard.LEFT) 
     { 
      character.x -= characterspeed; 
      if (character.x <= 0) 
      { 
       character.x = 0; 
      } 
     } 
     if (evt.keyCode == Keyboard.RIGHT) 
     { 
      character.x += characterspeed; 
      if (character.x >= 670) 
      { 
       character.x = 670; 
      } 
     } 
    } 

    private function RandomRange(min:Number,max:Number):Number 
    { 
     return Math.random() * max - min + min; 
    } 

} 

} 

這是我的遊戲: 目前,炸彈和金錢隨機出現X,100在屏幕上有15個錢和10個炸彈。 我想要的是,如何修改它成爲金錢和炸彈從特定點出現,之後,撞到牆上的錢和炸彈就會反彈回來。 任何人都可以提供我一些代碼?由於

一個例子類似的遊戲玩法與我的比賽是 http://www.youtube.com/watch?v=zzQ8PP5-TVE

回答

0

了一條捷徑得到的錢逼真模擬和炸彈下跌可能使用Box2D的實現。

這裏是一個鏈接到一個演示完整的源代碼,球隨機添加在一排釘之上。這與您鏈接的視頻風格相同。

http://wonderfl.net/c/aeyl

在這個例子中,你可以更換與該選擇一個隨機起點的功能派生球的代碼。下面是編寫這樣的功能的一種方法:

function chooseSpawnPoint():Point{ 
    var points:Array = new Array(); 
    points[0] = new Point(0,50); 
    points[1] = new Point(0,100); 
    points[2] = new Point(0,150); 
    points[3] = new Point(0,200); 
    points[4] = new Point(0,250); 
    return Math.floor(Math.random()*points.length); 
} 

*您可以改善初始化函數調用外的「點」陣列功能的性能。如果你這樣做,它可能會出現這樣的:

var points:Array = new Array(); 
points[0] = new Point(0,50); 
points[1] = new Point(0,100); 
points[2] = new Point(0,150); 
points[3] = new Point(0,200); 
points[4] = new Point(0,250); 
// Add as many points as you want. 
// the function will choose one at random based on 
// the number of points you add to the points array. 

function chooseSpawnPoint():Point{ 
    return Math.floor(Math.random()*points.length); 
} 
+0

感謝您的幫助,介意給我你的聯繫? (MSN/Skype)我需要親引導我在閃光燈。 – 2012-02-14 21:33:11

+0

謝謝,但找到actionscript優點的最佳去處是Stackoverflow。有數字的力量。在這裏提出你的問題,其他具有相同問題的人可能會找到它們。 – 2012-02-15 00:22:22

+0

渲染對象的部分如何?第一次90秒,我需要渲染10個錢物和0炸彈 之後,我需要渲染15個錢物和8個炸彈 一旦發生碰撞,特定的物體會消失,併產生新的物體。 我沒有試過,它可以渲染和重生,但是當測試時,像果醬和卡住的遊戲 我認爲是re​​movechild()和addchild()出現問題 – 2012-02-15 15:02:15