00:00
00:00
Newgrounds Background Image Theme

Thrll just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

Help with spawner code?

852 Views | 1 Reply
New Topic Respond to this Topic

Help with spawner code? 2016-12-31 15:48:32


I've written this spawn code myself, which works... ...kinda.

The problem I'm having is that an enemy doesn't appear when it should. Like if I've told it to spawn a middle and right enemy, sometimes only a middle enemy appears.

This image: http://bit.ly/2iBclJl ...is what it should look like. There's a variable that holds the default monster total which I've set to 2, so two monsters have spawned properly at their positions.

On some occasions though, I get this: http://bit.ly/2iBff0G, which isn't really supposed to happen since the starting total is still set to 2, and only 1 enemy has spawned.

However, I did notice that this issue always occurred whenever two exact enemies are picked, as you can see in the debug list in the top-left of the image.

I'm not brilliant at Actionscript to be honest, so this code may be written completely wrong and is probably a total mess.

var testmonster: placeholder_enem = new placeholder_enem();
var enem_TG: tg_MC = new tg_MC();
var enem_femmelhorn: fhc_MC = new fhc_MC();

var array_testlist: Array = new Array();

var rand_back: MovieClip;
var rand_midd: MovieClip;
var rand_front: MovieClip;

array_testlist.push(testmonster, enem_TG, enem_femmelhorn);

function spwn_back(): void {
	
	get_rand_back();
	addChildAt(rand_back, numChildren-4);

	rand_back.x = -2355.75;
	rand_back.y = 10.35;

	MovieClip(root).curr_monster_total++;
	MovieClip(root).back_active = true;

	back_name();
	
}

function spwn_middle(): void {
	
	get_rand_midd();
	addChildAt(rand_midd, numChildren-3);
	rand_midd.x = -2215.70;
	rand_midd.y = 70.40;

	MovieClip(root).curr_monster_total++;
	MovieClip(root).middle_active = true;

	middle_name();
	
}

function spwn_front(): void {
	
	get_rand_front();
	addChildAt(rand_front, numChildren-2);
	rand_front.x = -2067.70;
	rand_front.y = 140.40;

	MovieClip(root).curr_monster_total++;
	MovieClip(root).front_active = true;

	front_name();
	
}

function get_rand_back(): void {
	rand_back = array_testlist[int(Math.random() * array_testlist.length)];
}
function get_rand_midd(): void {
	rand_midd = array_testlist[int(Math.random() * array_testlist.length)];
}
function get_rand_front(): void {
	rand_front = array_testlist[int(Math.random() * array_testlist.length)];
}


function back_name(): void {
	if (rand_back == testmonster) {
		MovieClip(root).enem_name_back = "Testmonster";
	}
	if (rand_back == enem_TG) {
		MovieClip(root).enem_name_back = "test_tree";
	}
	if (rand_back == enem_femmelhorn) {
		MovieClip(root).enem_name_back = "Femmelhorn & Crow";
	}
}

function middle_name(): void {
	if (rand_midd == testmonster) {
		MovieClip(root).enem_name_middle = "Testmonster";
	}
	if (rand_midd == enem_TG) {
		MovieClip(root).enem_name_middle = "test_tree";
	}
	if (rand_midd == enem_femmelhorn) {
		MovieClip(root).enem_name_middle = "Femmelhorn & Crow";
	}
}

function front_name(): void {
	if (rand_front == testmonster) {
		MovieClip(root).enem_name_front = "Testmonster";
	}
	if (rand_front == enem_TG) {
		MovieClip(root).enem_name_front = "test_tree";
	}
	if (rand_front == enem_femmelhorn) {
		MovieClip(root).enem_name_front = "Femmelhorn & Crow";
	}
}

I'm not that good at signatures.

Response to Help with spawner code? 2017-01-19 08:03:31


Hi!

Its been over 6 years since i have used flash, so bare with me.

I believe you have found the issue yourself, you said the problem occurs when it picks the same enemy twice, and you are right.

It is a problem(I think) because you only have one instance of testmonster. So you are reassigning the same object.
Not sure how experienced with programming you are, so sorry if this low level answer insults your intelligence. In programming there is objects and there is primitives. Primitives being numbers etc, if you assign two variables to the value 7, they both point at a different 7, and changing one will not affect the other.

Objects are referenced by a variable. So two variables can reference the same object, which is what I believe is happening here.

You only see one monster when they pick the same enemy name, because it is the same enemy object.
In your array at the top of the script you create an array with three monsters, array_testlist.push(testmonster, enem_TG, enem_femmelhorn); Every time you call array_testlist[0] you will get the same object instance, testmonster.

e.g you call spwn_middle();
then get_rand_midd() is called, and it sets rand_midd = testmonster;
it then sets test monsters position to the middle of the screen. and increments the curr_monster_total by 1.

then you call spwn_front()
and int(Math.random() * array_testlist.length) returns the value 0 again,
now rand_front = testmonster The same object instance testmonster as rand_midd is referencing.
Then you move testmonster to the front of the screen. and increments the curr_monster_total by 1 again,setting it to 2.

So now rand_midd and rand_front point at the same object instance testmonster, and thats why you have only one monster on the screen. What you want to do is create a new instance of placeholder_enem for both rand_front and rand_middle.

e.g

function get_rand_monster(rand_position:Object): void {
	var pos = Math.floor( Math.random() * 3 );   // get a number between 0 and 2 and store it in the variable pos
	if(pos == 0){
		rand_position = new placeholder_enem(); // if pos is 0, then return a new object instance of placeholder enem()
	}
	else if(pos == 1){
		rand_position = new tg_MC();
	}
	else if(pos == 2){
		rand_position = new fhc_MC();
	}
}

get_rand_monster(rand_front); // call the get random monster method and pass it the variable to store the new monster object instance inside
get_rand_monster(rand_midd);

The above code should be actionsctipt3 valid (again, its been 6 years so not 100% sure it will run) and it will create a new instance of the monsters each time its called, so even if you call it a hundred times and the rand num is 0 every time, you will get 100 new instances of placeholder_enem, each with its own coordinates etc as they will be unique instances of the same object.

I hope all of the above makes sense, good luck with your game.

At 12/31/16 03:48 PM, Geel7989 wrote: I've written this spawn code myself, which works... ...kinda.

The problem I'm having is that an enemy doesn't appear when it should. Like if I've told it to spawn a middle and right enemy, sometimes only a middle enemy appears.

This image: http://bit.ly/2iBclJl ...is what it should look like. There's a variable that holds the default monster total which I've set to 2, so two monsters have spawned properly at their positions.

On some occasions though, I get this: http://bit.ly/2iBff0G, which isn't really supposed to happen since the starting total is still set to 2, and only 1 enemy has spawned.

However, I did notice that this issue always occurred whenever two exact enemies are picked, as you can see in the debug list in the top-left of the image.

I'm not brilliant at Actionscript to be honest, so this code may be written completely wrong and is probably a total mess.

var testmonster: placeholder_enem = new placeholder_enem();
var enem_TG: tg_MC = new tg_MC();
var enem_femmelhorn: fhc_MC = new fhc_MC();

var array_testlist: Array = new Array();

var rand_back: MovieClip;
var rand_midd: MovieClip;
var rand_front: MovieClip;

array_testlist.push(testmonster, enem_TG, enem_femmelhorn);

function spwn_back(): void {

get_rand_back();
addChildAt(rand_back, numChildren-4);

rand_back.x = -2355.75;
rand_back.y = 10.35;

MovieClip(root).curr_monster_total++;
MovieClip(root).back_active = true;

back_name();

}

function spwn_middle(): void {

get_rand_midd();
addChildAt(rand_midd, numChildren-3);
rand_midd.x = -2215.70;
rand_midd.y = 70.40;

MovieClip(root).curr_monster_total++;
MovieClip(root).middle_active = true;

middle_name();

}

function spwn_front(): void {

get_rand_front();
addChildAt(rand_front, numChildren-2);
rand_front.x = -2067.70;
rand_front.y = 140.40;

MovieClip(root).curr_monster_total++;
MovieClip(root).front_active = true;

front_name();

}

function get_rand_back(): void {
rand_back = array_testlist[int(Math.random() * array_testlist.length)];
}
function get_rand_midd(): void {
rand_midd = array_testlist[int(Math.random() * array_testlist.length)];
}
function get_rand_front(): void {
rand_front = array_testlist[int(Math.random() * array_testlist.length)];
}


function back_name(): void {
if (rand_back == testmonster) {
MovieClip(root).enem_name_back = "Testmonster";
}
if (rand_back == enem_TG) {
MovieClip(root).enem_name_back = "test_tree";
}
if (rand_back == enem_femmelhorn) {
MovieClip(root).enem_name_back = "Femmelhorn & Crow";
}
}

function middle_name(): void {
if (rand_midd == testmonster) {
MovieClip(root).enem_name_middle = "Testmonster";
}
if (rand_midd == enem_TG) {
MovieClip(root).enem_name_middle = "test_tree";
}
if (rand_midd == enem_femmelhorn) {
MovieClip(root).enem_name_middle = "Femmelhorn & Crow";
}
}

function front_name(): void {
if (rand_front == testmonster) {
MovieClip(root).enem_name_front = "Testmonster";
}
if (rand_front == enem_TG) {
MovieClip(root).enem_name_front = "test_tree";
}
if (rand_front == enem_femmelhorn) {
MovieClip(root).enem_name_front = "Femmelhorn & Crow";
}
}