ActionScript 3.0 AddChild Function Not Working -
i have tested out code , addchild won't work. no errors outputted.
package{ import flash.display.movieclip; import flash.events.event; import flash.events.mouseevent; import flash.display.*; import flash.events.*; import flash.geom.rectangle; import flash.media.sound; import flash.text.*; public class game extends flash.display.movieclip{ public static const state_init:int = 10; public static const state_play:int = 20; public static const state_end_game:int = 30; public var gamestate:int = 0; public var score:int = 0; public var chances:int = 5; public var bg:movieclip; public var enemies:array; public var player:movieclip; public var level:number = 0; public var scorelabel:textfield = new textfield public var levellabel:textfield = new textfield public var chanceslabel:textfield = new textfield public var scoretext:textfield = new textfield public var leveltext:textfield = new textfield public var chancestext:textfield = new textfield public const scoreboard_y:number = 380 public function game(){ addeventlistener(event.enter_frame, gameloop); bg = new backimage(); addchild(bg); scorelabel.text = "score:"; levellabel.text = "level:"; chanceslabel.text = "misses:"; scoretext.text = "0"; leveltext.text = "1"; chancestext.text = "5"; scorelabel.y = scoreboard_y; levellabel.y = scoreboard_y; chanceslabel.y = scoreboard_y; scoretext.y = scoreboard_y; leveltext.y = scoreboard_y; chancestext.y = scoreboard_y; scorelabel.x = 5; scoretext.x = 50; chanceslabel.x = 105; chancestext.x = 155; levellabel.x = 205; leveltext.x = 260 addchild(scorelabel); addchild(levellabel); addchild(chanceslabel); addchild(scoretext); addchild(leveltext); addchild(chancestext); gamestate = state_init; } public function gameloop(e:event):void{ switch(gamestate){ case state_init: initgame(); break; case state_play: playgame(); break; case state_end_game: endgame(); break; } } public function initgame():void{ score = 0; chances = 5; player = new playerimage(); enemies = new array(); level = 1; leveltext.text = level.tostring(); addchild(player); player.startdrag(true,new rectangle(0,0,550,400)) gamestate = state_play } public function playgame():void{ player.rotation += 15; makeenemies(); moveenemies(); testcollisions(); testforend(); } public function makeenemies():void{ var chance:number = math.floor(math.random()*100); var tempenemy:movieclip; if (chance < 2 + level) { tempenemy = new enemyimage() tempenemy.speed = 3 + level; tempenemy.gotoandstop(math.floor(math.random()*5)+1); tempenemy.y = 435; tempenemy.x = math.floor(math.random()*515) addchild(tempenemy); enemies.push(tempenemy); } } public function moveenemies():void{ var tempenemy:movieclip; (var i:int = enemies.length -1;i >= 0;i--){ tempenemy = enemies[i]; tempenemy.y -= tempenemy.speed; if (tempenemy.y < -35){ chances -= 1; chancestext.text = chances.tostring(); enemies.splice(i,1); removechild(tempenemy); } } } public function testcollisions():void { var sound:sound = new pop(); var tempenemy:movieclip; (var i:int = enemies.length -1;i >= 0;i--){ tempenemy = enemies[i]; if(tempenemy.hittestobject(player)){ score++; scoretext.text = score.tostring(); sound.play(); enemies.splice(i,1); removechild(tempenemy); } } } public function testforend():void{ if(chances == 5){ gamestate = state_end_game; } else if(score > level*20) { level++; leveltext.text = level.tostring(); } } public function endgame():void{ for(var i:int = 0; i< enemies.length; i++) { removechild(enemies[i]); } enemies = []; player.stopdrag() } } }
i have tried adding this. , stage. in front of addchild , still doesn't work.
this inside file called game.as
when create game
instance need add child stage:
// rename variable 'game' // 1st character of variable name conventionally lower-case // var game:game = new game(); var script:game = new game(); this.addchild(script);
Comments
Post a Comment