actionscript 3 - AS3 Game spawn mechanics -


i working on game friend, , need some code... in game want make balls appear on screen, in specific order, have created lists decide spawn balls, , color. example:

var a:array = [["green", "red", "blue", "purple"],                ["orange", "red", "yellow", "black"],                ["green", "green", "blue", "pink"]] 

the inter dimensional arrays because of order want them spawn in(starting bottom). have figured out how spawn balls when spawn spawn on top of each other. can please me this? thanks! (i making in flash btw)

code have:

    import flash.events.event;  stage.addeventlistener(event.enter_frame, main);   // variabels   var row1:number = 92.25; var row2:number = 243.30; var row3:number = 394.30; var row4:number = 547.35;  var pattern:array = pattern1; /* var array:array = pattern.reverse(); var rows:number = pattern1.length; */  //trace(pattern.reverse());  //frame counter var frame:number = 0;  function main(e:event):void {     make(pattern.reverse(), pattern.length);     trace(pattern); }  function make(array:array, rows):void {     //count frames     frame += 1;     //trace(frame);       if (frame % 30 == 0) {         //go thrugh rows         (var r = 0; r<rows; r+=1) {             //trace(array[r]);              //go thrugh lists             (var = 0; <= 3; i+= 1) {                 spawnball(array[r][i], i);             }          }     }     /*     // go through each row      (var r = 0; r <= rows; r += 1) {      }     // go through list , spawn;     (var = 0; <= 3; i+=1) {         spawnball(array[i], i);     }     */ }  // spawn ball function spawnball(color:string, pos:number):void {      if (color == "###") {          trace("not color");      } else {          switch (color) {             case "lil":                 var purple_ball:purpleball = new purpleball;                 addchild(purple_ball);                   purple_ball.x = setx(pos);                 purple_ball.y = 0;             break;              case "gul":                 var yellow_ball:yellowball = new yellowball;                 addchild(yellow_ball);                       yellow_ball.x = setx(pos);                 yellow_ball.y = 0;             break;              case "grø":                 var green_ball:greenball = new greenball;                 addchild(green_ball);                 green_ball.x = setx(pos);                 green_ball.y = 0;             break;              case "rød":                 var red_ball:redball = new redball;                 addchild(red_ball);                 red_ball.x = setx(pos);                 red_ball.y = 0;             break;              case "blå":                 var blue_ball:blueball = new blueball;                 addchild(blue_ball);                 blue_ball.x = setx(pos);                 blue_ball.y = 0;             break;              case "ros":                 var pink_ball:pinkball = new pinkball;                 addchild(pink_ball);                 pink_ball.x = setx(pos);                 pink_ball.y = 0;             break;              case "ora":                 var orange_ball:orangeball = new orangeball;                 addchild(orange_ball);                 orange_ball.x = setx(pos);                 orange_ball.y = 0;             break;         }            this.addeventlistener(event.enter_frame, moveball)     }      function setx(pos):number {         var xpos:number;          switch (pos) {             case 0:                 xpos = row1;             break;              case 1:                 xpos = row2;             break;              case 2:                 xpos = row3;             break;              case 3:                 xpos = row4;             break;         }          return xpos;     }  }   //move balls function moveball(e:event) {     e.currenttarget.y += 5; } 

the balls on top of each other

it easier fix if refactoring first. setx() clunky, example. there's lot can go wrong there. if nothing else, can abbreviated:

//                     0      1       2       3 var xpositions:array = [92.25, 243.30, 394.30, 547.35];  function setx(pos:number):number {   return xpositions[pos]; } 

spawnball can likewise simplified.

var ballcolors:object = {     "lil": purpleball,     "gul": yellowball,     "grø": greenball,     "rød": redball,     "blå": blueball,     "ros": pinkball,     "ora": orangeball };  function spawnball(color:string, pos:number):void {     var ball = ballcolors[color];     if (ball == undefined) {         trace("not color: " + color);         return     }     var ball = new ball();     addchild(ball);     ball.x = setx(pos);     ball.y = 0;      this.addeventlistener(event.enter_frame, moveball); }  //move balls function moveball(e:event):void {     e.currenttarget.y += 5; } 

now spawnball more manageable, can see problem more clearly. you're attaching moveball event handler stage, , having move stage instead of balls. since want move balls simultaneously, having 1 event handler attached stage makes sense, need keep set of spawned balls in array can update of them.

let's move addeventlistener() call out initialization logic , add global array store list of balls.

spawnball becomes:

function spawnball(color:string, pos:number):void {     var ball = ballcolors[color];     if (ball == undefined) {         trace("not color: " + color);         return     }     var ball = new ball();     addchild(ball);     ball.x = setx(pos);     ball.y = 0;      spawnedballs.push(ball); } 

moveball becomes:

//move balls function moveball(e:event):void {     each (var ball in spawnedballs) {         ball.y -= 5;     } } 

and global variable declared:

var spawnedballs:array = []; 

finally, add main():

this.addeventlistener(event.enter_frame, moveball); 

that should work.


Comments

Popular posts from this blog

Java 3D LWJGL collision -

methods - python can't use function in submodule -

c# - ErrorThe type or namespace name 'AxWMPLib' could not be found (are you missing a using directive or an assembly reference?) -