dimanche 29 mars 2015

Action Script 3.0. How to add random room's template and items with random positions inside

In my flash game are 9 possible rooms. In each room are 3 fixed positions where items can be added. There also are 3 items on every room, but items should be added always in different positions.


For example If here are 3 positions(locations): A(x=1;y=1) B(x=2;y=2) C(x=3;y=3) and Item1 Item2 Item3, sometimes Item1 should be added to A(x=1;y=1), sometimes to B(x=2;y=2) and etc. should be random chance in which positions items will be added.




In this image is shown only 3 rooms, but total I have 9. As you see all rooms have different position for items, but It's fixed on every room and items (blue, pink, yellow) should have ability to change positions everytime It's added to stage.


random rooms


What will be best performance to do It?


It is declared in following:



var room1:Room1 = new Room1();
var item1:Item1 = new Item1();
var item2:Item2 = new Item2();
var item3:Item3 = new Item3();

var room2:Room2 = new Room2();
var item4:Item4 = new Item4();
var item5:Item5 = new Item5();
var item6:Item6 = new Item6();

....


So in Room1 should be added Item1 Item2 and Item3, in Room2 should be added Item4 Item5 and Item6 and etc.


First of all I need to choose randomly 1 of 9 rooms. Should It be something like this?


addChild(Room+randNumber)


Later I need to specify if for every room?



if(Room+randNumber == Room1) {
addChild(Item1);
addChild(Item2);
addChild(Item3);
}


But in this case how to set 3 static x, y position for every room and set It for every item randomly?





Aucun commentaire:

Enregistrer un commentaire