ActionScript3 Code Reduction/Simplification for my game
hi wondering if me simplify code have written game making. it's basic game, want see if can me make code less lengthy, less lines better
any @ great
package
{
import flash.display.movieclip;
import flash.events.mouseevent;
import flash.events.event
public class galacticgame extends movieclip
{
//declare variables
var background:background;
var startpage:startpage;
//i prefer separating variables locate each section pertains to. not appreciate cluster
var repairr:repairr;
var repairm:repairm;
var repairl:repairl;
var demonguy:demonguy;
var demonguy2:demonguy2;
var demonguy3:demonguy3;
var demonguy4:demonguy4;
var l:int;
var hitcount:int;//this int because check how many times spacetown hit , if spacetown destroyed game stop
var gameover:gameover;
var uwin:uwin;
var displayscore:displayscore;
var destroy:int = 0;
var planet:int = 1;
public function galacticgame()
{
//here declare of our variables using , setting our objects movieclips , etc in game making. must defined in order work
background = new background();
startpage = new startpage();
uwin = new uwin();
repairr = new repairr();
repairm = new repairm();
repairl = new repairl();
gameover = new gameover();
displayscore = new displayscore();
hitcount = 0;
repairm.gotoandstop(1);
repairl.gotoandstop(1);
repairr.gotoandstop(1);
addchild(background);
addchild(startpage);
//add event listeners
startpage.startbutton.addeventlistener(mouseevent.click, startbuttonclick);
}
function startbuttonclick(event:mouseevent):void
{
removechild(startpage);
addchild(repairr);
addchild(repairm);
addchild(repairl);
addchild(displayscore);
trace("thank playing game. i");
//this allow demonguys being spawned randomly drop down , generated in different areas, allowing more fun
function randomrange(minnum:number, maxnum:number):number
{
return (math.floor(math.random() * (maxnum - minnum + 1)) + minnum);
}
//spawn demonguy1
function spawndemonguy()
{
demonguy = new demonguy();
addchild(demonguy);
demonguy.x=randomrange(20,460)
demonguy.y=randomrange(-20, -500)
//colision bulidings
addeventlistener(event.enter_frame,checkforcollision1);
function checkforcollision1(event:event)
{
if(demonguy.dg.hittestobject(repairm))
{
removechild(demonguy);
if(repairm.currentframe==2)
{
repairm.gotoandstop(3);
//this check see deaths, if dead dead simple
hitcount++
dead()
}
if(repairm.currentframe==1)
{
repairm.gotoandstop(2);
//incing dead cnt
hitcount++
}
spawndemonguy()
}
if(demonguy.dg.hittestobject(repairr))
{
removechild(demonguy);
if(repairr.currentframe==2)
{
repairr.gotoandstop(3);
//increments dead counter , checks see if dead
hitcount++
dead()
}
if(repairr.currentframe==1)
{
repairr.gotoandstop(2);
//increments dead counter
hitcount++
}
spawndemonguy()
}
if(demonguy.dg.hittestobject(repairl))
{
removechild(demonguy);
if(repairl.currentframe==2)
{
repairl.gotoandstop(3);
//increments dead counter , checks see if dead
hitcount++
dead()
}
if(repairl.currentframe==1)
{
repairl.gotoandstop(2);
//increments dead counter
hitcount++
}
spawndemonguy()
}
}
demonguy.dg.addeventlistener(mouseevent.click, ondemonclick1);
function ondemonclick1(event:mouseevent)
{
removechild(demonguy);
//add destroy , update destroy , planet on stage
destroy++
updatedisplayscore()
//respawn demonguy 1
spawndemonguy()
}
}
//this spawn new demonguy2 add more demons/monsters enemies game.
function spawndemonguy2()
{
demonguy2 = new demonguy2();
addchild(demonguy2);
demonguy2.x=randomrange(20,460)
demonguy2.y=randomrange(-20, -500)
//colision bulidings
addeventlistener(event.enter_frame,checkforcollision1);
function checkforcollision1(event:event)
{
if(demonguy2.dg2.hittestobject(repairm))
{
removechild(demonguy2);
if(repairm.currentframe==2)
{
repairm.gotoandstop(3);
//increments counter checks if youre destroyed, if space town done
hitcount++
dead()
}
if(repairm.currentframe==1)
{
repairm.gotoandstop(2);
//""
hitcount++
}
spawndemonguy2()
}
if(demonguy2.dg2.hittestobject(repairr))
{
removechild(demonguy2);
if(repairr.currentframe==2)
{
repairr.gotoandstop(3);
//""
hitcount++
dead()
}
if(repairr.currentframe==1)
{
repairr.gotoandstop(2);
//inc dc""
hitcount++
}
spawndemonguy2()
}
if(demonguy2.dg2.hittestobject(repairl))
{
removechild(demonguy2);
if(repairl.currentframe==2)
{
repairl.gotoandstop(3);
//""
hitcount++
dead()
}
if(repairl.currentframe==1)
{
repairl.gotoandstop(2);
//""
hitcount++
}
spawndemonguy2()
}
}
demonguy2.dg2.addeventlistener(mouseevent.click, ondemonclick1);
function ondemonclick1(event:mouseevent)
{
removechild(demonguy2);
//add destroy , update destroy score , planet level.
destroy++
updatedisplayscore()
//respawn demonguy 2
spawndemonguy2()
}
}
//spawn demonguy3 -
function spawndemonguy3()
{
demonguy3 = new demonguy3();
addchild(demonguy3);
demonguy3.x=randomrange(20,460)
demonguy3.y=randomrange(-20, -500)
//coll. spacetown
addeventlistener(event.enter_frame,checkforcollision1);
function checkforcollision1(event:event)
{
if(demonguy3.dg3.hittestobject(repairm))
{
removechild(demonguy3);
if(repairm.currentframe==2)
{
repairm.gotoandstop(3);
//increments dead counter , checks see if dead
hitcount++
dead()
}
if(repairm.currentframe==1)
{
repairm.gotoandstop(2);
//increments dead counter
hitcount++
}
spawndemonguy3()
}
if(demonguy3.dg3.hittestobject(repairr))
{
removechild(demonguy3);
if(repairr.currentframe==2)
{
repairr.gotoandstop(3);
//increments dead counter , checks see if dead
hitcount++
dead()
}
if(repairr.currentframe==1)
{
repairr.gotoandstop(2);
//increments dead counter
hitcount++
}
spawndemonguy3()
}
if(demonguy3.dg3.hittestobject(repairl))
{
removechild(demonguy3);
if(repairl.currentframe==2)
{
repairl.gotoandstop(3);
//
hitcount++
dead()
}
if(repairl.currentframe==1)
{
repairl.gotoandstop(2);
//inc d
hitcount++
}
spawndemonguy3()
}
}
demonguy3.dg3.addeventlistener(mouseevent.click, ondemonclick1);
function ondemonclick1(event:mouseevent)
{
removechild(demonguy3);
//add destroy , update destroy , planet on stage
destroy++
updatedisplayscore()
//respawn demonguy 3
spawndemonguy3()
}
}
//spawn demonguy4
function spawndemonguy4()
{
demonguy4 = new demonguy4();
addchild(demonguy4);
demonguy4.x=randomrange(20,460)
demonguy4.y=randomrange(-20, -500)
//if spacetown hit code find out. hitting spacetown triggered movieclips in game known demons
addeventlistener(event.enter_frame,checkforcollision1);
function checkforcollision1(event:event)
{
if(demonguy4.dg4.hittestobject(repairm))
{
removechild(demonguy4);
if(repairm.currentframe==2)
{
repairm.gotoandstop(3);
//""
hitcount++
dead()
}
if(repairm.currentframe==1)
{
repairm.gotoandstop(2);
//""
hitcount++
}
spawndemonguy4()
}
if(demonguy4.dg4.hittestobject(repairr))
{
removechild(demonguy4);
if(repairr.currentframe==2)
{
repairr.gotoandstop(3);
//""
hitcount++
dead()
}
if(repairr.currentframe==1)
{
repairr.gotoandstop(2);
//d++
hitcount++
}
spawndemonguy4()
}
if(demonguy4.dg4.hittestobject(repairl))
{
removechild(demonguy4);
if(repairl.currentframe==2)
{
repairl.gotoandstop(3);
//if dead go game on otherwise continue..
hitcount++
dead()
}
if(repairl.currentframe==1)
{
repairl.gotoandstop(2);
//++dead
hitcount++
}
spawndemonguy4()
}
}
demonguy4.dg4.addeventlistener(mouseevent.click, ondemonclick1);
function ondemonclick1(event:mouseevent)
{
removechild(demonguy4);
//add planet
destroy++
updatedisplayscore()
//respawn demonguy 4
spawndemonguy4()
}
}
//i believe code stops generating demons
function dead()
{
if (hitcount==6)
{
trace("the game has ended, playing. made louis reynolds it201 professor sequeira");
addchild(gameover);
}
}
function updatedisplayscore()
{//this make sure each time click on demon , destroyed update destroy count once you've reached destroy score demon appear go on next planet etc:
displayscore.playerscore.text=(string(destroy));
displayscore.playerlevel.text=(string(planet));
//this makes sure planet goes next stage
levup()
}
//planets 1-3 function here. code below displays when planet goes next planet when destroy reaches number
function levup()
{
if(destroy==12)
{
planet++
spawndemonguy3()
}
if(destroy==15)
{
planet++
spawndemonguy4()
}
if(destroy==30)
{
//if spacetown takes many hits, (which diminishes of buildings, game cease, , text loser text appear)
hitcount=7;
addchild(uwin);
}
}
//this here spawns first demon in beginning of game, second
spawndemonguy()
spawndemonguy2()
}
}
}
to simplify need repeating patterns in processing , wherever possible turn them generic looping processing. 1 bit see repeat involves checkforcollision functions nesting inside other functions... should not nest named functions within functions. first thing should break out 1 function, , see how can share between spawndemonguy functions.
also, within function can develop looping scheme following repetitive sections 1 shown below, though want name first numeric ending rest make work. need put array work dealing repairm/r/l...
if(demonguy.dg.hittestobject(repairm))
{
removechild(demonguy);
if(repairm.currentframe==2)
{
repairm.gotoandstop(3);
//this check see deaths, if dead dead simple
hitcount++
dead()
}
if(repairm.currentframe==1)
{
repairm.gotoandstop(2);
//incing dead cnt
hitcount++
}
spawndemonguy()
}
More discussions in ActionScript 3
adobe
Comments
Post a Comment