View Full Version : [AS3] enemy spawn and main menu
douwexxl
09-27-2009, 11:21 PM
hi readers.
i building a shooting game all in actionscript (nothing on stage)
( weapon in first person view and a crosshair that is controlled by the mouse.
the barrel of the weapon follows the mouse. i got a reload system with bullet display etc. so thats all good,
but i want my game to have some levels, i want a max of 10 enemys in level one, when u killed all the enemys i want a GRATTS screen and a time and score.
at the moment the enemy keep on coming i dont know how to make like 10 enemys spawn and if u dont kill them within the timelimit u are dead.
all my enemys are inside a array they are pushed on stage and there is a timer to controll them a little. so for a next level i can make them spawn faster.
==========
next question.
==========
i made a nice movieclip for the main menu with option and startbuttons,
but i cant figure out how to get it working everything i try lets the game crash. i got the movieclip on stage but when i click the startgame button it crashes.
can anyone please help with my problems??
thanks in advance.
UberMouse
09-28-2009, 03:04 AM
Post some code and I'll help I don't know what you're doing currently. But most likely you need to wrap the spawning code with an if statement like
if(spawn)
{
spawnthem;
}
douwexxl
09-28-2009, 04:29 AM
yes im trying to do it with for loops but then the enemy will spawn in sets of 10 and they still keep on coming. i need to get 10 enemy on stage one by one and after the last enemy gets on stage the push function stops.
what code u need i will post it thank u verry much.
UberMouse
09-28-2009, 04:38 AM
Spawning code probably but depending on the size of the game post it all (Or at least all the stuff that is used by the spawning code).
douwexxl
09-28-2009, 02:35 PM
well i just started building the game so i its not that big yet. i cut all the code i try`d using to get the enemy spawn limited to 10.
this is the raw code i used for the crosshair / spawnTimer and enemysound effect in the document class, to get the them on stage.
package {
import flash.utils.Timer;
import flash.ui.Mouse;
import flash.media.Sound;
import flash.events.Event;
import flash.display.Stage;
import flash.display.MovieClip;
import flash.events.TimerEvent;
public class Engineshooter extends MovieClip {
//array`s\\..
public static var enemylist:Array=new Array() ;
//==================================
//spawn timer\\
private var enemy:Timer=new Timer(5000);
//===============================
private var _crosshair:Crosshair;
//enemy sound\\
private var EE:Sound = new EnemyEngine ();
public function Engineshooter() {
Mouse.hide();
_crosshair=new Crosshair(stage);
_crosshair.x=stage.stageWidth/2;
_crosshair.y=stage.stageHeight/2;
stage.addChild(_crosshair);
enemy.addEventListener(TimerEvent.TIMER,onstart);
enemy.start();
function onstart(e:Event):void {
var _enemy:Enemy=new Enemy(stage);
_enemy.addEventListener(Event.REMOVED_FROM_STAGE,r emoveenemy,false,0,true);
_enemy.addEventListener("killed",enemykilled,false,0,true);
enemylist.push(_enemy);
//EE.play();
stage.addChild(_enemy);
}
}
private function enemykilled(e:Event) {
trace("enemy killed");
//scoreHUD.updateKills(1);
//scoreHUD.updateScore(e.currentTarget.points);
}
private function removeenemy(e:Event) {
enemylist.splice(enemylist.indexOf(e.currentTarget ),1);
}
}
}
================================================== ===============
this works fine the crosshair is controlled by the mouse and i can shoot to kill the enemy.
the enemyclass dispatches the "killed" event that all works the way it should
but as u can see there is no spawnlimit in there. i try`d using a for loop and if/else statements but then the enemy spawns in pairs and keep on spawing.
i just want to get 10 enemys in the first level of the game with a display howmany enemy are left in the level to kill, then up to next level where 15 enemy will spawn etc, etc.
oh and the scorehud u see in there isnt finished yet so i commented it out for now, i got the graphics and im writing the class for it today.
i hope u can do something with this to help me out. thanks.
UberMouse
09-28-2009, 10:07 PM
while(enemySpawned <= 10 && gamenotfinished)//Some variable controlling if the game has ended
{
var _enemy:Enemy=new Enemy(stage);
_enemy.addEventListener(Event.REMOVED_FROM_STAGE,r emoveenemy,false,0,true);
_enemy.addEventListener("killed",enemykilled,false ,0,true);
enemylist.push(_enemy);
//EE.play();
stage.addChild(_enemy);
enemySpawned++;
}
That should make sure there are no more then 10 enemies and they will not spawn if the game has ended.
douwexxl
09-28-2009, 10:26 PM
oke thank u verry much but where do i put the while code cause it dont seem to work could u please write it were i should place the code??
thank u.
UberMouse
09-28-2009, 10:30 PM
package {
import flash.utils.Timer;
import flash.ui.Mouse;
import flash.media.Sound;
import flash.events.Event;
import flash.display.Stage;
import flash.display.MovieClip;
import flash.events.TimerEvent;
public class Engineshooter extends MovieClip {
//array`s\\..
public static var enemylist:Array=new Array() ;
//==================================
//spawn timer\\
private var enemy:Timer=new Timer(5000);
//===============================
private var _crosshair:Crosshair;
var enemysSpawned:int = 0;
var gameFinished:Boolen = false;
//enemy sound\\
private var EE:Sound = new EnemyEngine ();
public function Engineshooter() {
Mouse.hide();
_crosshair=new Crosshair(stage);
_crosshair.x=stage.stageWidth/2;
_crosshair.y=stage.stageHeight/2;
stage.addChild(_crosshair);
enemy.addEventListener(TimerEvent.TIMER,onstart);
enemy.start();
function onstart(e:Event):void {
while(enemySpawned <= 10 && gameFinished != true)//Some variable controlling if the game has ended
{
var _enemy:Enemy=new Enemy(stage);
_enemy.addEventListener(Event.REMOVED_FROM_STAGE,r emoveenemy,false,0,true);
_enemy.addEventListener("killed",enemykilled,false ,0,true);
enemylist.push(_enemy);
//EE.play();
stage.addChild(_enemy);
enemySpawned++;
}
}
}
private function enemykilled(e:Event) {
trace("enemy killed");
//scoreHUD.updateKills(1);
//scoreHUD.updateScore(e.currentTarget.points);
}
private function removeenemy(e:Event) {
enemylist.splice(enemylist.indexOf(e.currentTarget ),1);
}
}
}
================================================== ===============
That is all the code with mine added in. When you want the round/game to end and the enemies to stop spawning set gameFinished = true; and then back to false when you want them to start spawning again.
douwexxl
09-28-2009, 11:24 PM
oke thank u verry much for ur help, im building the game main menu screen now,
so now my document class is the menuclass and i added all the button clips to the stage.
the buttons are movieclips with buttons inside them and they are added with AS in the menuclass, so when i load the game u get the menuscreen with options play game etc and when u press the options button a new movieclip is loaded with the options in it.
so the options screen listens to the options button now im building all buttons for the optionsscreen and when u press a button in the options screen u get a new screen.
the way im doing it it takes allot of different movieclip and AS files who listen to eachother, do u think there is a better way of doing this or is this the (correct) way.
UberMouse
09-28-2009, 11:27 PM
I'm not much help there since I didn't do the menu's in my game. But I do know that the way it is done is that there's a buttonHandler.as and a screenHandler.as which is a function that you call that creates the buttons/screens dynamically using the functions.
douwexxl
09-29-2009, 12:15 AM
oke well, the way im building works fine ill try and find some stuff to read about this in the next few days.
i build an options screen movieclip and on the timelines of that clip, im build the buttons and the next or pref screen.
so when i click on a button in the optionsscreen u gotoAndStop to the next framelabel.
on a actions layer in the optionsscreen clip is the code for the mouseclick and what to do if u click the button.
i know there must be a better way but for now its the easyest way to do it for me.
i know some timeline action scripting but i wanted to take it a step further and build a game with only AS this will be my first game that doesnt have any graphics on the main stage until u load the game.
i hope i get my game to work on the startgame button. the rest of the buttons is easy just some movieclips to call and or delete from stage.
douwexxl
09-29-2009, 12:17 AM
oh by the way what do u know about sound and music on off functions?? i build a button in the optionsscreen to adjust the music and sound FX in the game
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.