PDA

View Full Version : [AS2] Spawn more enemies


ONeSolid
10-01-2009, 06:24 PM
I am developing a game as a school project but now I have encountered some problems.
The game consists a number of balls, one of them which you control is made so that when it hits another ball in the same color you gain score and the ball disappear. But here is my problem, there should always be "score balls" on the stage. So i added duplicate movieclip to make the scoreball duplicate if the amount of balls gets below 2. The problem starts when i the number goes below 2 a scoreball is added but does not move.

The scoreballs is added like this:
var GreenBall:MovieClip = this.attachMovie("GreenBall", "GreenBall", 110);
numEnemy=3;
GreenBall._x = random(1000);
GreenBall._y = random(800);
GreenBallgravity = 2;
GreenBallbounce = 0.92;
GreenBallspeedx = 0;
GreenBallspeedy = 0;
GreenBallxbounce = 60;
GreenBall._xscale = 50;
GreenBall._yscale = 50;

GreenBall.onEnterFrame = function() {

GreenBallspeedy = GreenBallspeedy+GreenBallgravity;
this._x += GreenBallspeedx/5;
this._y += GreenBallspeedy/5;

if (this._y>floor) {
this._y = floor;
GreenBallspeedy *= -GreenBallbounce;
GreenBallspeedy -= 15;
GreenBallspeedx += 40;
}
if (this._x<left) {
this._x = left;
GreenBallspeedx = +GreenBallxbounce;
}
if (this._x>right) {
this._x = right;
GreenBallspeedx = -GreenBallxbounce;
}
if (this._y<roof) {
this._y = roof;
GreenBallspeedy += GreenBallbounce*5;

}
if (eBall1.pixelHitTest(this) && this._currentframe == 1 && eBall1._currentframe == 1) {
this.gotoAndPlay(2);
numEnemy -= 1;
eBall1.gotoAndStop(random(3));
ArcadeExplosion.start(0,2);
ArcadeExplosion.setVolume(200);
}
if (this._currentframe == 2) {
point += 10;
GreenBallspeedx = 0;
GreenBallspeedy = 0;
GreenBallgravity = 0;

}
};

The duplicate looks like this:
}
for (i=2; i<=numEnemy; i++){
GreenBall.duplicateMovieClip( "GreenBall"+i, i+100);
}

I have a theory, that the ball is not assigned any actionscript when its duplicated because the actionscript goes below the attachMovieclip part.

I am grateful for every answer.

rrh
10-01-2009, 07:59 PM
One problem is
GreenBallspeedx = 0;
GreenBallspeedy =0;
You are assigning these globally instead of as something belonging to GreenBall, like with ._x ._y etc. Which means all the copies will share only one value.

Also, your code will be more readable if you put it in [ as ] tags.

But your main problem is that, yes, when a movieClip is duplicated, the actionscript is not part of what is duplicated.

ONeSolid
10-02-2009, 08:54 AM
Thank you, I will look at it again and see if I can change those things that you mentioned and hopfully i can get it too work. But is there any way to get the actionscript into the duplicated movieclip?
Should i put the AS under the duplicate command?

rrh
10-02-2009, 02:36 PM
Yes, something like this:

for (i=2; i<=numEnemy; i++){
var newBall = GreenBall.duplicateMovieClip( "GreenBall"+i, i+100);
newBall._x = random(1000);
...
}

ONeSolid
10-06-2009, 07:06 PM
Ok so I did what you told me to, and changed some things so that things worked somewhat what I want them to. This is what my script lookes like so far:

//-------------------GreenBall---------------------
var GreenBall:MovieClip = this.attachMovie("GreenBall", "GreenBall", 110);
GreenBall._y = -20;
GreenBall._x = -20;
numEnemy = 1;

onEnterFrame = function(){
//---------------------Duplicte Ball---------------
for (i=10; i>=numEnemy; numEnemy++) {
var newGreenBall = GreenBall.duplicateMovieClip("newGreenBall"+numEnemy, numEnemy+100);
newGreenBall._x = random(1000);
newGreenBall._y = random(800);
trace(newGreenBall);
newGreenBallgravity = 2;
newGreenBallbounce = 0.92;
newGreenBallspeedx = 0;
newGreenBallspeedy = 0;
newGreenBallxbounce = 60;
newGreenBall._xscale = 50;
newGreenBall._yscale = 50;
newGreenBall.onEnterFrame = function() {
newGreenBallspeedy = newGreenBallspeedy+newGreenBallgravity;
this._x += newGreenBallspeedx/5;
this._y += newGreenBallspeedy/5;
if (this._y>floor) {
this._y = floor;
newGreenBallspeedy *= -newGreenBallbounce;
newGreenBallspeedy -= 15;
newGreenBallspeedx += 40;
}
if (this._x<left) {
this._x = left;
newGreenBallspeedx = +newGreenBallxbounce;
}
if (this._x>right) {
this._x = right;
newGreenBallspeedx = -newGreenBallxbounce;
}
if (this._y<roof) {
this._y = roof;
newGreenBallspeedy += newGreenBallbounce*5;
}
if (eBall1.pixelHitTest(this) && this._currentframe == 1 && eBall1._currentframe == 1) {
this.gotoAndPlay(2);
numEnemy -= 1;
eBall1.gotoAndStop(random(3));
ArcadeExplosion.start(0,2);
ArcadeExplosion.setVolume(200);
}
if (this._currentframe == 2) {
point += 10;
newGreenBallspeedx = 0;
newGreenBallspeedy = 0;
newGreenBallgravity = 0;
}
};
}

The problem is that all the new GreenBalls are influenced by eachother and the same forces so that if one ball hits the floor all the other balls act as if they also did and bounce upwards.
How do I specify an onEnterFrame function to a specific duplicated movieclip?

rrh
10-06-2009, 07:14 PM
Reminder:
Also, your code will be more readable if you put it in [ as ] tags.

Also your new problem is cause by this:
One problem is

GreenBallspeedx = 0;
GreenBallspeedy =0;

You are assigning these globally instead of as something belonging to GreenBall, like with ._x ._y etc. Which means all the copies will share only one value.

-OneSolid
10-06-2009, 09:36 PM
Sorry for becoming an "impossible case" but do you mean like this?:

for (i=10; i>=numEnemy; numEnemy++) {
var newGreenBall = GreenBall.duplicateMovieClip("newGreenBall"+numEnemy, numEnemy+100);
newGreenBall._x = random(1000);
newGreenBall._y = random(800);
newGreenBall.newGreenBallgravity = 2;
newGreenBall.newGreenBallbounce = 0.92;
newGreenBall.newGreenBallspeedx = 0;
newGreenBall.newGreenBallspeedy = 0;
newGreenBall.newGreenBallxbounce = 60;
newGreenBall._xscale = 50;
newGreenBall._yscale = 50;
newGreenBall.onEnterFrame = function() {
newGreenBallspeedy = newGreenBallspeedy+newGreenBallgravity;
this._x += newGreenBallspeedx/5;
this._y += newGreenBallspeedy/5;
if (this._y>floor) {
this._y = floor;
newGreenBallspeedy *= -newGreenBallbounce;
newGreenBallspeedy -= 15;
newGreenBallspeedx += 40;
}
if (this._x<left) {
this._x = left;
newGreenBallspeedx = +newGreenBallxbounce;
}
if (this._x>right) {
this._x = right;
newGreenBallspeedx = -newGreenBallxbounce;
}
if (this._y<roof) {
this._y = roof;
newGreenBallspeedy += newGreenBallbounce*5;
}
if (eBall1.pixelHitTest(this) && this._currentframe == 1 && eBall1._currentframe == 1) {
this.gotoAndPlay(2);
numEnemy -= 1;
eBall1.gotoAndStop(random(3));
ArcadeExplosion.start(0,2);
ArcadeExplosion.setVolume(200);
}
if (this._currentframe == 2) {
point += 10;
newGreenBallspeedx = 0;
newGreenBallspeedy = 0;
newGreenBallgravity = 0;
}
};
}

If this is not what you meant, can you please post a more precise example/explanation?

PS. I'm almost there! :P

rrh
10-06-2009, 10:10 PM
Yes. But then in your onEnterFrame function, you'll need to reference them as this.newGreenBallgravity or whatever. (And I'd probably want to shorten these things to .gravity so it's shorter.)

-OneSolid
10-07-2009, 12:30 PM
HAALLLELUUJJAAA!!!!XD

It works!
Thanks you so much for taking time to help me! A great weightwas just lifted from my shoulders!

Thank you!!!