actionscript 3 - Adding object within another -
i have main stage 550x400. header area stats bar. have element underneath named gamestage 550x350.
i creating circles on 1 second interval , trying randomly place them within gamestage. not appear working. seems they're being added 550x350 element, starts @ top of main stage -- not within gamestage.
also if addchild(circle) creates 25 radius circle. gamestage.addchild(circle), circle gets skewed slightly.
what doing wrong?
private function createcircle():void { var stagesafex:number = math.random()*gamestage.width; var stagesafey:number = math.random()*gamestage.height; var circle:sprite = new sprite(); circle.graphics.clear(); circle.graphics.beginfill(math.random()*0xffffff, 1); circle.graphics.drawcircle(0, 0, circleradius); circle.graphics.endfill(); circle.x = stagesafex; circle.y = stagesafey; circle.name = string(circlecount); gamestage.addchild(circle); }
okay i'm using flash develop, you'll have forgive me program doesn't have fla files, classes , uses main class start program (more reminiscent of java if you've ever programmed in that). code i'll show more or less same of how want it.
first recommend make randomnumber function, used in making code if want use here's 1 use (i put in main class, can put wherever want):
public static function randomnumber(minvalue:number, maxvalue:number):uint { return math.floor(math.random() * (1 + maxvalue - minvalue)) + minvalue; }
this inclusive, meaning if put randomnumber(1, 10)
give number between 1 10, including 1 , 10. it's more or less common sense, figured might mention clarify.
now on addcircle function:
public static function addcircle(gamestage:sprite, circleradius:uint):void { //initializing new circle instance var newcircle:sprite = new sprite(); //basically same code had (you don't need set alpha value 1, it's default value 1 regardless) newcircle.graphics.beginfill(math.random() * 0xffffff); newcircle.graphics.drawcircle(0, 0, circleradius); newcircle.graphics.endfill(); //since circle's origin center, want outer edges bound gamestage's edges var safestagex:number = main.randomnumber(newcircle.width / 2, gamestage.width - newcircle.width / 2); var safestagey:number = main.randomnumber(newcircle.height / 2, gamestage.height - newcircle.height / 2); //adding circle gamestage's display field gamestage.addchild(newcircle); //only set circle's x , y after add gamestage's display list, otherwise might not set newcircle.x = safestagex; newcircle.y = safestagey; }
now following give code made creation of gamestage. have it, i'll provide mine in case want use instead:
//initializing gamestage instance var gamestage:sprite = new sprite(); //adding gamestage stage's display field this.stage.addchild(gamestage); //setting gamestage's width , height (using "gamestage.width = 550" , "gamestage.height = 350" not work) //use color of main game's background don't see fill (unless want to) //either or add background picture, need 1 or other in order set gamestage's dimensions gamestage.graphics.beginfill(0x000000); gamestage.graphics.drawrect(0, 0, 550, 350); gamestage.graphics.endfill(); //this puts gamestage on bottom of screen (since it's 50 pixels shorter in y direction) gamestage.y = 50;
lastly give actual loop create circles (this function present in same class/fla gamestage on, because addcircle function needs take in gamestage instance:
//now let's populate gamestage (var i:uint = 0; < [number of circles want]; i++) { main.addcircle(gamestage, [radius of circle]); }
and you're done! i'll include entire main class, can see how functions work together.
package { import flash.display.shape; import flash.display.sprite; import flash.events.event; public class main extends sprite { public function main() { if (stage) init(); else addeventlistener(event.added_to_stage, init); } private function init(e:event = null):void { removeeventlistener(event.added_to_stage, init); var gamestage:sprite = new sprite(); this.stage.addchild(gamestage); gamestage.graphics.beginfill(0x000000); gamestage.graphics.drawrect(0, 0, 550, 350); gamestage.graphics.endfill(); gamestage.y = 50; (var i:uint = 0; < 150; i++) { main.addcircle(gamestage, main.randomnumber(15, 25)); } } public static function addcircle(gamestage:sprite, circleradius:uint):void { var newcircle:sprite = new sprite(); newcircle.graphics.beginfill(math.random() * 0xffffff); newcircle.graphics.drawcircle(0, 0, circleradius); newcircle.graphics.endfill(); var safestagex:number = main.randomnumber(newcircle.width / 2, gamestage.width - newcircle.width / 2); var safestagey:number = main.randomnumber(newcircle.height / 2, gamestage.height - newcircle.height / 2); gamestage.addchild(newcircle); newcircle.x = safestagex; newcircle.y = safestagey; } public static function randomnumber(minvalue:number, maxvalue:number):uint { return math.floor(math.random() * (1 + maxvalue - minvalue)) + minvalue; } } }
Comments
Post a Comment