-
[RESOLVED] Enemy Spawn
It's me again. I'm making a game like Gnat Attack from Mario Paint except you hit fairies instead of flies
The problem is that I got the code to make flash spawn multiple fairies but it keeps overwriting them because they're at the same depth. So I used _root.getNextHighestDeph and all that did was break the spawn and have one fairy teleport to random places while another spawns on the top left unable to be affected by the hit check triggered by the fly swatter
Code:
setInterval(SpawnFairy, 2000);
function SpawnFairy() {
attachMovie("Fairy", "Fairy", _root.getNextHighestDepth());
Fairy._x = random(1024);
Fairy._y = random(768);
}
This is the code I have down right now. Is there anything I'm doing wrong?
-
PHP Code:
setInterval(SpawnFairy, 2000);
var depth_num=0;
var fairy_container=[]
function SpawnFairy() {
_root.attachMovie("fairy", "fairy"+depth_num, depth_num);
_root["fairy"+depth_num]._x = random(1024);
_root["fairy"+depth_num]._y = random(768);
fairy_container.push(_root["fairy"+depth_num])//add fairy to the next array spot of fairy_container
depth_num++//increase number
//extra commands begin
for(var i=0;i<fairy_container.length;i++){//loop through each fairy
fairy_container[i]._x+=5//move x of each fairy in container
fairy_container[i]._y+=5//move y of each fairy in container
}
//extra commands end
}
-
So, it's working now but the hit test I put in anytime the fly swatter hits a fairy doesn't work.
The hit test is in it's own function that's called when you press the "P" Key which spawns in a hitbox "hit" to collide with the fairy. If the collision is true it calls for the Fairy movie to despawn and attach a death animation in it's place
PHP Code:
function kill() {
if (hit.hitTest(Fairy)) {
_root.score += 3;
Swatter.gotoAndPlay("2");
Fairy.gotoAndPlay("13");
unloadMovie(hit);
unloadMovie(Fairy);
attachMovie("Fairy_death", "Fairy_death", 6);
Fairy_death._x = Fairy._x;
Fairy_death._y = Fairy._y;
} }
should I replace "Fairy" with "_root["Fairy"+depth_num]"?
-
The depth_num was increased after creating the last fairy so it wouldn't be accessed like that because you are calling fairy1 when the one created was called fairy0, a good option is to go through each fairy stored in the container array called fairy_container 1 by 1 like fairy_container[0], fairy_container[1] ... in the for loop & check if one of them is touching the hit movieclip.
PHP Code:
setInterval(SpawnFairy, 2000);
var depth_num=0;
var fairy_container=[]
var fairy_death_container=[]
function SpawnFairy() {
_root.attachMovie("fairy", "fairy"+depth_num, depth_num);
_root["fairy"+depth_num]._x = random(1024);
_root["fairy"+depth_num]._y = random(768);
fairy_container.push(_root["fairy"+depth_num])//add fairy to the next array spot of fairy_container
depth_num++//increase number
//extra commands begin
for(var i=0;i<fairy_container.length;i++){//loop through each fairy
fairy_container[i]._x+=5//move x of each fairy in container
fairy_container[i]._y+=5//move y of each fairy in container
}
//extra commands end
}
function kill() {
// hit._x=_root._xmouse
// hit._y=_root._ymouse
for(var i=0;i<fairy_container.length;i++){//loop through each fairy fairy_container[0] , fairy_container[1] etc...
if (hit.hitTest( fairy_container[i])) {// if the current fairy being checked in the loop is touching hit
_root.score += 3;
Swatter.gotoAndPlay("2");
fairy_container[i].gotoAndPlay("13");//current fairy in the loop that hit, goto frame 13
_root.attachMovie("Fairy_death", "Fairy_death"+depth_num, depth_num);//attach depth fairy
fairy_death_container.push(_root["Fairy_death"+depth_num])//add death fairy to fairy_death_container array to be accessed as fairy_death_container[0] , fairy_death_container[1] etc...
_root["Fairy_death"+depth_num]._x = fairy_container[i]._x;//before removing the old fairy, give the fairy death the same coordinates as the old fairy
_root["Fairy_death"+depth_num]._y = fairy_container[i]._y;//before removing the old fairy, give the fairy death the same coordinates as the old fairy
unloadMovie(hit);//unload the hit object during hit test with a fairy
unloadMovie( fairy_container[i]);//unload current fairy in the array position [i] that touched the hit movieclip
}
}
depth_num++//increase the depth after you are done using anything with _root["Fairy_death"+depth_num] because the next call would be for a different name
}
-
If you are going to have alot of fairys I added fairy_container.splice to save processing power.
PHP Code:
setInterval(SpawnFairy, 2000);
var depth_num=0;
var fairy_container=[]
var fairy_death_container=[]
function SpawnFairy() {
_root.attachMovie("fairy", "fairy"+depth_num, depth_num);
_root["fairy"+depth_num]._x = random(1024);
_root["fairy"+depth_num]._y = random(768);
fairy_container.push(_root["fairy"+depth_num])//add fairy to the next array spot of fairy_container
depth_num++//increase number
//extra commands begin
for(var i=0;i<fairy_container.length;i++){//loop through each fairy
fairy_container[i]._x+=5//move x of each fairy in container
fairy_container[i]._y+=5//move y of each fairy in container
}
//extra commands end
}
function kill() {
// hit._x=_root._xmouse
// hit._y=_root._ymouse
for(var i=0;i<fairy_container.length;i++){//loop through each fairy fairy_container[0] , fairy_container[1] etc...
if (hit.hitTest( fairy_container[i])) {// if the current fairy being checked in the loop is touching hit
_root.score += 3;
Swatter.gotoAndPlay("2");
fairy_container[i].gotoAndPlay("13");//current fairy in the loop that hit, goto frame 13
_root.attachMovie("Fairy_death", "Fairy_death"+depth_num, depth_num);//attach depth fairy
fairy_death_container.push(_root["Fairy_death"+depth_num])//add death fairy to fairy_death_container array to be accessed as fairy_death_container[0] , fairy_death_container[1] etc...
_root["Fairy_death"+depth_num]._x = fairy_container[i]._x;//before removing the old fairy, give the fairy death the same coordinates as the old fairy
_root["Fairy_death"+depth_num]._y = fairy_container[i]._y;//before removing the old fairy, give the fairy death the same coordinates as the old fairy
unloadMovie(hit);//unload the hit object during hit test with a fairy
unloadMovie( fairy_container[i]);//unload current fairy in the array position [i] that touched the hit movieclip
fairy_container.splice(i,1);//remove fairy that was unloaded from its array position
}
}
depth_num++//increase the depth after you are done using anything with _root["Fairy_death"+depth_num] because the next call would be for a different name
}
-
One last question, How do I perform a frame check for _root["Fairy_death"+depth_num]
-
This is the line to put inside of the for() loop thats going through each fairy
PHP Code:
if( _root["Fairy_death"+depth_num]._currentframe==1){//if the fairy frame is at frame one...
trace(true);//trace true in the output panel
}
Hey would you be interested in downloading the AS3 game I just released on Android, I will be waiting for users in the lobby as a way to tend to users while its empty, I put a little ringtone for when someone comes in... xP thats what you will be doing later in multiplayer fairy land for Android hahaha: download
-
Still running into problems
I have trace actions set to on yet nothing is happening when I trigger the death animation on the fairies. I thinking it's something with an event listener or an onEnterFrame() function
I have death(); which will call the function for the death animation. Mostly to make programming easier.
PHP Code:
setInterval(SpawnFairy, 2000);
var depth_num=0;
var fairy_container=[]
var fairy_death_container=[]
function SpawnFairy() {
_root.attachMovie("fairy", "fairy"+depth_num, depth_num);
_root["fairy"+depth_num]._x = random(1024);
_root["fairy"+depth_num]._y = random(768);
fairy_container.push(_root["fairy"+depth_num])//add fairy to the next array spot of fairy_container
depth_num++//increase number
//extra commands begin
for(var i=0;i<fairy_container.length;i++){//loop through each fairy
death(); //call the function for the fairy's death animation
fairy_container[i]._x+=5//move x of each fairy in container
fairy_container[i]._y+=5//move y of each fairy in container
}
//extra commands end
}
function death() {
if( _root["Fairy_death"+depth_num]._currentframe==5){
trace(true);
Key.removeListener(Attack_Fairy);
}
}
-
PHP Code:
setInterval(SpawnFairy, 2000);
var depth_num=0;
var fairy_container=[]
var fairy_death_container=[]
function SpawnFairy() {
_root.attachMovie("fairy", "fairy"+depth_num, depth_num);
_root["fairy"+depth_num]._x = random(1024);
_root["fairy"+depth_num]._y = random(768);
fairy_container.push(_root["fairy"+depth_num])//add fairy to the next array spot of fairy_container
depth_num++//increase number
//extra commands begin
for(var i=0;i<fairy_container.length;i++){//loop through each fairy
death(i); //call the function for the fairy's death animation & send value i to construct the full name of fairy_death at the death function
fairy_container[i]._x+=5//move x of each fairy in container
fairy_container[i]._y+=5//move y of each fairy in container
}
//extra commands end
}
function death(a) {
if( _root["Fairy_death"+a]._currentframe==5){//the loop is for fairy though so I would use: _root["fairy"+a]
trace(true);
Key.removeListener(Attack_Fairy);
}
}
-
Nothing seems to be working. I even changed it to where the kill() function would just have the fairy movieclip jump to there frames containing the death animation instead of spawning in a separate one.
Something definitely broke
-
Ok so when fairy reaches a certain frame it will be removed and a fairy_death will be added to replace its spot, at the top function in "extra commands 2", fairy death can keep moving if the container for fairy_death has any fairy_death movieclips stored in it:
PHP Code:
setInterval(SpawnFairy, 2000);
var depth_num=0;
var fairy_container=[]
var fairy_death_container=[]
function SpawnFairy() {
_root.attachMovie("fairy", "fairy"+depth_num, depth_num);
_root["fairy"+depth_num]._x = random(1024);
_root["fairy"+depth_num]._y = random(768);
fairy_container.push(_root["fairy"+depth_num])//add fairy to the next array spot of fairy_container
depth_num++//increase number
//extra commands begin
for(var i=0;i<fairy_container.length;i++){//loop through each fairy
death(i); //call the function for the fairy's death animation & send value i to construct the full name of fairy_death at the death function
fairy_container[i]._x+=1//move x of each fairy in container
fairy_container[i]._y+=1//move y of each fairy in container
}
//extra commands end
//extra commands begin 2
for(var i=0;i<fairy_death_container.length;i++){//loop through each fairy
fairy_death_container[i]._x+=1//move x of each fairy in container
fairy_death_container[i]._y+=1//move y of each fairy in container
}
//extra commands end 2
}
function death(a) {
if( _root["fairy"+a]._currentframe==3){//the loop is for fairy though so I would use: _root["fairy"+a]
trace(true);//takes a while to get to frame 5 with a slow interval
_root.attachMovie("Fairy_death", "Fairy_death"+depth_num, depth_num);//attach depth fairy
fairy_death_container.push(_root["Fairy_death"+depth_num])//add death fairy to fairy_death_container array to be accessed as fairy_death_container[0] , fairy_death_container[1] etc...
_root["Fairy_death"+depth_num]._x = fairy_container[a]._x;//before removing the old fairy, give the fairy death the same coordinates as the old fairy
_root["Fairy_death"+depth_num]._y = fairy_container[a]._y;//before removing the old fairy, give the fairy death the same coordinates as the old fairy
unloadMovie( fairy_container[a]);//unload current fairy in the array position [a] that touched the hit movieclip
fairy_container.splice(a,1);//remove fairy that was unloaded from its array position
Key.removeListener(Attack_Fairy);
}
depth_num++//increase the depth after you are done using anything with attach
}
-
Crap, It's still not working.
I have an Idea though, I think there's a tutorial on how to make an asteroids game. Maybe I can look into that and see if I can get the spawning working
-
Alright, try that, or maybe explain it more detailed. If the setInterval happens every 2 seconds,
it would take 10 seconds to view a fairy death replace the fairy.
-
Is it okay if I share the .FLA file to you?
-
-
https://noirville.neocities.org/flashkit_share.zip
I tried a different approach to the spawn code. Following a tutorial on how to make asteroids in flash which had instructions on how to make a spawn cap as well.
However, the fairies spawn all at once and every time I try to add the setInterval, it crashes flash.
PHP Code:
function init() {
//Settings for the game
SPEEDER = 6;
//positive speed for the fairy
NEGSPEEDER = -6;
//negative speed for the fairy
LEFT = 0;
//x = 0
TOP = 0;
//y = 0
BOTTOM = Stage.height-50;
//y = 718
RIGHT = Stage.width-50;
//x = 974
MAXFAIRY = 10;
//Spawncap for the fairies
fairy_array = new Array();
//Fairy Array
createEmptyMovieClip("fairy_spawn", 0);
//movieclip where fairies spawn in
fairyinit();
}
function fairyinit() {
//settings for the fairies
FAIRYINDEX = 0;
//Set the current spawn to 0
while (FAIRYINDEX<MAXFAIRY) {
//when the fairy number is below the spawn cap, spawn faries periodically. However, flash crashes when I try to use the interval, perhaps an issue with flash trying to count how many movieclips are on the canvas and running the setInterval code at the same time
//setInterval(SpawnFairy, 1000);
spawnFairy();
}
}
function spawnFairy() {
//Code for when the fairy is spawned. This determines the speed, the coordinates, and runs the function for the movement
fairy_clip = fairy_spawn.attachMovie("Fairy", "Fairy"+FAIRYINDEX, FAIRYINDEX++);
trace(fairy_clip);
fairy_clip.NEGVELX = Math.random()*NEGSPEEDER;
fairy_clip.NEGVELY = Math.random()*NEGSPEEDER;
fairy_clip.VELX = Math.random()*SPEEDER;
fairy_clip.VELY = Math.random()*SPEEDER;
fairy_clip._x = Math.random()*RIGHT;
fairy_clip._y = Math.random()*BOTTOM;
fairy_clip.onEnterFrame = fairymove;
fairy_array.push(fairy_clip);//The fairy enemy
}
function fairymove() {//The movement of the fairy. This is the basics, I'll probably write something different later once I get the spawn and death animations done.
if (this.hitTest(Wall) == true) {
this._x += this.VELX;
this._y += this.VELY;
this._x += this.NEGVELX;
this._y += this.NEGVELY;
}
if (this.hitTest(Wall) == false) {//The goal of the faries is to swat them down before they leave the field. Planning on having this deducting 3 points from the score.
unloadMovie(this);
}
}
-
Oh yes, the crash is because FAIRYINDEX++ wasn't implemented in the function "spawnFairy()" to eventually be able to end the while() loop.
Here is the file in AS3.0 version because I no longer have an IDE that supports AS2.0: Download
That shouldn't be a problem right?
-
Is it easy to convert it into AS2?
-
If you take a minute to download & Pre-Register an account to my AS3.0 game I'll rewrite it in AS2.0, can you let me know if the process went smooth, Thanks. I don't have anyone to test register.
Or else if that is not possible you can just ask me certain parts that you need converted. It shouldn't be that much of an issue to convert. You need to use proper as2 enter frame, remove event listeners, addchild for as2 is attach.
-
I just need the important parts converted, if that's okay with you.
-
ok I just wrote it on a notepad since I cannot test it, this goes on the main timeline, & does all the jobs so maybe you should not have any program written inside movieclips besides "stop();"
Somethings to check if there are still errors:
setinterval is written properly for as2?
movieclip linkage names are spelled correct case for the attachmovieclip?
PHP Code:
//Share this with AS3.0 for help
var score = 0;
var check_interval=null
var check_time=1000//every 1000 milliseconds, check if fairy count is too low to spawn more
var check_enabled=false
var fairy_array = []
var fairy_death_spawn_array=[]
var fairy_death_duration=700//ms
var MAXFAIRY = 10;
var FAIRYINDEX=0
var playerSpeed=35
//Setings for the game
var SPEEDER = 6;
//positive speed for the fairy
var NEGSPEEDER = -6;
//negtive speed for the fairy
var LEFT = 0;
//x = 0
var TOP = 0;
//y = 0
var BOTTOM = stage.stageHeight-50
//y = 718
var RIGHT = stage.stageWidth-50;
//stop();
var current_depth=10;
init();
//spawn player and faries
current_depth++
attachMovie("Swatter", "Swatter", current_depth);
//var swatter_var:Swatter=new Swatter()
//stage.addChild(swatter_var);
//swatter_var.name="Swatter"
//attach the swatter
_root["Swatter"]._x = 300;
//swatter ccordinates
_root["Swatter"]._y = 200;
//swatter ccordinates
//Spawncap for the fairies
function init() {
fairy_array =[]
fairy_death_spawn_array=[]
SPEEDER = 6;
//positive speed for the fairy
NEGSPEEDER = -6;
//negtive speed for the fairy
LEFT = 0;
//x = 0
TOP = 0;
//y = 0
BOTTOM = Stage.height-50
//y = 718
RIGHT =Stage.width-50;
MAXFAIRY = 10;
FAIRYINDEX=0
//Spawncap for the fairies
//Fairy Array
///createEmptyMovieClip("fairy_spawn", 0);
//var fairy_spawn_var:Fairy=new Fairy()
//stage.addChild(fairy_spawn_var);
//fairy_spawn_var.name="fairy_spawn_var"
///movieclip where fairies spawn in
fairyinit();
}
function fairyinit() {
//settings for the fairies
FAIRYINDEX = 0;
//Set the current spawn to 0
while (FAIRYINDEX<MAXFAIRY) {
spawnFairy();
}
FAIRYINDEX=MAXFAIRY
//when the fairy number is below the spawn cap, spawn faries periodically. However, flash crashes when I try to use the interval, perhaps an issue with flash trying to cound how many movieclips are on the canvas and running the setInterval code at the same time
check_enabled=true
//check_interval=setTimeout(lowFairyCountCheck,check_time);
setInterval(lowFairyCountCheck, check_time);
}
function lowFairyCountCheck(){
if(check_enabled){
trace("check " + FAIRYINDEX)
while (FAIRYINDEX<MAXFAIRY) {
spawnFairy();
}
}
}
function spawnFairy() {
//Code for when the fairy is spawned. This determines the speed, the coordinates, and runs the function for the movement
// var fairy_spawn_var:Fairy=new Fairy()//create fairy variable
//stage.addChild(fairy_spawn_var);//add it to stage
//fairy_spawn_var.name="fairy_spawn_var"//give it a name to access as stage.getChildByName("fairy_spawn_var").x etc..
current_depth++;
attachMovie("Fairy", "Fairy"+current_depth, current_depth);
//trace(fairy_clip);
_root["Fairy"+depth_num].NEGVELX = Math.random()*NEGSPEEDER;
_root["Fairy"+depth_num].NEGVELY = Math.random()*NEGSPEEDER;
_root["Fairy"+depth_num].VELX = Math.random()*SPEEDER;
_root["Fairy"+depth_num].VELY = Math.random()*SPEEDER;
_root["Fairy"+depth_num]._x = Math.random()*RIGHT;
_root["Fairy"+depth_num]._y = Math.random()*BOTTOM;
//fairy_spawn_var.onEnterFrame = fairymove;
fairy_array.push( _root["Fairy"+depth_num]);//The fairy enemy
FAIRYINDEX++
}
onEnterFrame=function(){
for(var i=0;i<fairy_array.length;i++){
if (fairy_array[i].hitTest(Wall) == true) {
fairy_array[i]._x += fairy_array[i].VELX;
fairy_array[i]._y += fairy_array[i].VELY;
fairy_array[i]._x += fairy_array[i].NEGVELX;
fairy_array[i]._y += fairy_array[i].NEGVELY;
}else{
unloadMovie(fairy_array[i])//unload fairy that left the wall
fairy_array.splice(i,1)//remove fairy from array space
FAIRYINDEX--;//subtract the index so that more fairies missing can be added later if needed.
}
}
/*
if (this.hitTest(Wall) == false) {//The goal of the faries is to swat them down before they leave the field. Planning on having this deducting 3 points from the score.
unloadMovie(this);
}
*/
}
//Player Controls: WASD for movement, P for swatting
// A is 65
// S is 83
// D is 68
// W is 87
var Attack_Fairy:Object = new Object();
Attack_Fairy.onKeyDown = function() {
if (Key.getCode() == "80") {
Splat();
kill();
}
if (Key.getCode() == "87") {//w
_root["Swatter"]._y-=playerSpeed
}
if (Key.getCode() == "65") {//a
_root["Swatter"]._x-=playerSpeed
}
if (Key.getCode() == "83") {//s
_root["Swatter"]._y+=playerSpeed
}
if (Key.getCode() == "68") {//d
_root["Swatter"]._x+=playerSpeed
}
};
/*
stage.addEventListener(KeyboardEvent.KEY_DOWN,kd)
function kd(e:*){
if(e.keyCode=="80"){
Splat();
kill();
}
if(e.keyCode=="87"){//W
//Key.addListener(Attack_Fairy);
stage.getChildByName("Swatter").y -= playerSpeed;
}
if(e.keyCode=="65"){//A
//Key.addListener(Attack_Fairy);
stage.getChildByName("Swatter").x -= playerSpeed;
}
if(e.keyCode=="83"){//S
//Key.addListener(Attack_Fairy);
stage.getChildByName("Swatter").y += playerSpeed;
}
if(e.keyCode=="68"){//D
//Key.addListener(Attack_Fairy);
stage.getChildByName("Swatter").x+= playerSpeed;
}
}
*/
function kill(){
for(var i=0;i<fairy_array.length;i++){
if (fairy_array[i].hitTest(_root["Swatter"]) == true) {
//var fairy_death_spawn_var:Fairy_death=new Fairy_death()
//stage.addChild(fairy_death_spawn_var);
//fairy_death_spawn_var.name="fairy_death_spawn_var"
current_depth++
attachMovie("Fairy_death", "Fairy_death"+current_depth, current_depth);
fairy_death_spawn_array.push(_root["Fairy_death"+depth_num]);//The fairy enemy
_root["Fairy_death"+depth_num]._x=fairy_array[i]._x
_root["Fairy_death"+depth_num]._y=fairy_array[i]._y
//setTimeout(removeFairyDeath,fairy_death_duration) ------------------------------------------------------required
unloadMovie(fairy_array[i])//unload fairy that left the wall
fairy_array.splice(i,1)//remove fairy from array space
FAIRYINDEX--;//subtract the index so that more fairies missing can be added later if needed.
}
}
}
function removeFairyDeath(){
//while(fairy_death_spawn_array[0].name=="hit"){//---------------------------------------------------------required
//unloadMovie(fairy_death_spawn_array[0])//unload splat
//fairy_death_spawn_array.shift()
//}
if(fairy_death_spawn_array.length>0){
unloadMovie(fairy_death_spawn_array[0])//unload fairy death
fairy_death_spawn_array.shift()
}
}
function Splat() {
depth_num++
attachMovie("hit", "hit", depth_num);
//var hit_var:hit=new hit()//attach for as3
//stage.addChild(hit_var)//add to stage
//hit_var.name="hit"//give name
//hit_var._x = Swatter._x;
//hit_var._y = Swatter._y;
//hit_var.x = stage.getChildByName("Swatter").x;
//hit_var.y = stage.getChildByName("Swatter").y;
// fairy_death_spawn_array.push(_root["hit"]);//add to same array as fairy death for removing both at once later
}
//playSound(placeholder_DnB)
//playSound(Controls)
//function playSound(a){
//var snd:Sound=new a()
//snd.play()
//}
//SoundMixer.stopAll() //stop all sounds
/*
//soundtrack is a placeholder
// Play Sound Behavior
_global.Behaviors.Sound.placeholder_spunk.start(0, 1);
// End Play Sound Behavior
//Play Internal Sound Behavior
if (_global.Behaviors == null) {
_global.Behaviors = {};
}
if (_global.Behaviors.Sound == null) {
_global.Behaviors.Sound = {};
}
if (typeof this.createEmptyMovieClip == 'undefined') {
this._parent.createEmptyMovieClip('BS_placeholder_spunk', new Date().getTime()-(Math.floor((new Date().getTime())/10000)*10000));
_global.Behaviors.Sound.placeholder_spunk = new Sound(this._parent.BS_placeholder_spunk);
} else {
this.createEmptyMovieClip('_placeholder_spunk_', new Date().getTime()-(Math.floor((new Date().getTime())/10000)*10000));
_global.Behaviors.Sound.placeholder_spunk = new Sound(this.BS_placeholder_spunk);
}
_global.Behaviors.Sound.placeholder_spunk.attachSound("placeholder_DnB.mp3");
if (true) {
_global.Behaviors.Sound.placeholder_spunk.start(0, 1);
}
//End Behavior
*/
If you still have an issue you can just take parts from the program and put it in yours