PDA

View Full Version : Several problems with my SHMUP prototype


beeblebrox
07-01-2008, 09:03 AM
Hey guys,

i need some help with my shmup prototype.
this is what i want to achieve:

- remove bullet MC on impact
- scripted enemy formations

here's the prototype, feel free to study / rewrite / use the code:

beeblebrox
07-01-2008, 10:01 AM
- remove MCs
i solved this issue.
created another array for enemy ships and removing the shot-MC by:

_root.bullets[i].removeMovieClip();

in the hitTest-loop ---

now go for the formations ;)

GMaker0507
07-01-2008, 02:09 PM
pretty cool

Although, you need some type of animation for when you hit a ship down. It kinda confused me, and at first i didnt know if they were hit or not.

I already downloaded it, and did the animation thing, ill upload it, but i need to know what version of flash your using, to see if i can back convert it

GMaker0507
07-01-2008, 03:21 PM
For the formations, hmmm well i guess you would have preset formations such as

Box Formation, Horizontal Line Formation, Vertical Line Formation, Diagonal Line Formation, and the S Formation


What i did after i downloaded your sample was take the code from the enemy mc on the _root stage, and put it on the first frame inside itself

That way it would automatically be applied anytime its on stage

I changed

if (_x <= -40) {
this.removeMovieClip ();
}
to

if (this.getBounds (_root).xMax <= 0) {
this.removeMovieClip ();
}

that way, no matter how many movieclips it is nested in, when its right side(xMax) is below zero, remove it.

Therefore, all you have to do when you want a formation is create a separate movieclip for them. Then position them how you want to have them, then attach the formation to the stage. The code will do the rest.

theres a sample of what i've got in the attachments

if you have flash 8 you should be able to open the fla.

Other wise you might have to find some way to back convert it to whatever version you have

beeblebrox
07-02-2008, 03:02 PM
nice!
I like your explosions ;)

but there's a problem:
when using the 'enemy'-array to unload bullets on impact and attaching the formation MCs, the framerate drops immediately:

GMaker0507
07-02-2008, 03:18 PM
okay, something i noticed

I forgot the code to remove enemies when they leave the stage. Also, because not all the enemies are placed on the stage directly using the 'attachMovie' or 'duplicateMovieClip' function (the ones preplaced in the formation) the 'removeMovieClip' function will alone not be enough

on the enemy mc's code frame, After:

_x += _root.enemyspeed + speedup;

add:

// If were no longer viewable
if (getBounds (_root).xMax <= 0) {
/* If our parent is the root, meaning we were attached via 'attachMovie' or 'duplicateMovieClip'.*/
if (_parent == _root) {
// Use the removeMovieClip function to remove us.//
this.removeMovieClip ();
} else {// Othwerwise, if our parent isnt the root then we are inside of a formation.
/* If that be the case, set our falling variable to true, render us invisible, then change our onEnterFrame function so it doesnt do anything. Therefore technically were still on the stage, were just brain dead*/
falling = true;
visible = false;
this.onEnterFrame = function () {
};
}
}

Also, i would recommend that once you start implementing the formations into the gameplay without the buttons, you only put them on stage when it is clear. Therefore lets say you defeat all the random enemies, you see a message saying something 'get ready, here come the special forces', one or two seconds go by, the stage is clear, then you add the formation.

It should work fine if you add them when their are other enemies on the stage, but its just a precaution.

beeblebrox
07-02-2008, 04:33 PM
thanks. but that doesn't really speed up the whole thing...

i guess it has something to do with the removal of the bullets
_root.bullets[i].removeMovieClip();

when I comment this line out, everything works fine and fluid.
So the question is again:
how do I get rid of that bullets after impact (without frame drops)?

GMaker0507
07-02-2008, 05:16 PM
thats wierd

Well i did some more stuff to it, i kinda forgot what...oh yea

I added a FPS Counter, which is on the root stage

And i changed the code i previously posted to:

// If were no longer viewable
if (this.getBounds (_root).xMax <= 0 || this.getBounds (_root).yMin >= 400) {
/* If our parent is the root, meaning we were attached via 'attachMovie' or 'duplicateMovieClip'.*/
if (_parent == _root) {
// Use the removeMovieClip function to remove us.//
destroyed = true;
this.removeMovieClip ();
} else {// Othwerwise, if our parent isnt the root then we are inside of a formation.
/* If that be the case, set our falling variable to true, render us invisible, then change our onEnterFrame function so it doesnt do anything. Therefore technically were still on the stage, were just brain dead*/
destroyed = true;
this.unloadMovie ();
return;
}
}

That and some more things, you might notice when you go through it

beeblebrox
07-02-2008, 06:08 PM
cool!

meanwhile I figured out how to get rid of that inefficient line of code:
Instead of unloading the bulletMC it just goes to the next (empty) frame after splitting it from the array.

It works like a charm!

beeblebrox
07-06-2008, 12:21 PM
even if it's possible to have predesigned formation MCs
I'd rather like to make this more elegant.

fully scripted formations is what I request ;)
does anyone have an idea how to do this?

GMaker0507
07-06-2008, 01:01 PM
Open up an empty flash, create a 'enemy' mc, export for AS, and fool with this code.

Note the S does have the perfect code, but it is the general shape

For Horizontal/Vertical lines just change the boxes xRows/yCols to 1

////////////////////////////////////////////////////////////
/* The Box Formation. */
////////////////////////////////////////////////////////////

// How many enemies across
var xRows = 4;
// How many enemies up and down
var yCols = 4;
// How much space between them
var xSpacing = 5;
var ySpacing = 5;

var midX = 320;
var midY = 240;
// The second loop creates a column, the first creates rows
for (var i = 0; i < xRows; i++) {
for (var j = 0; j < yCols; j++) {
var enemyName = "Enemy" + _root.getNextHighestDepth ();
_root.attachMovie ("Enemy",enemyName,_root.getNextHighestDepth ());
var enemy = _root[enemyName];

// Position is like so. We want the box's middle at midX, and same with the y

/* The boxes width is equal to the the width of each enemy plus the x spacing
times the amount of enemies their are in each row.*/

/* The boxes height is equal to the the height of each enemy plus the y spacing
times the amount of enemies their are in each column.*/

/* So we place them in the middle, then subtract half the width and height, then position them
accordingly.*/

var boxWidth = ((enemy._width + xSpacing) * xRows);
var boxHeight = ((enemy._height + ySpacing) * yRows);

enemy._x = midX - (boxWidth / 2) + (i * (enemy._width + xSpacing));
enemy._y = midY - (boxHeight / 2) + (j * (enemy._height + ySpacing));
}
}


////////////////////////////////////////////////////////////
/* The S Formation. */
////////////////////////////////////////////////////////////

/* S's in general have four curves, but you might want more.*/
var numCurves:Number = 4;
/* This is the number of enemies per curve. Note you would probably need a number that could be divided by four and three and not have any decimals*/
var numEnemies:Number = 8;
// 1 for down, -1 for up
var Direction:Number = 1;
// How how, from the start(below), should the S curve
var CurveHeight:Number = 100;
//Where the S Starts at
var startX = 0;
var startY = 255;
//where you place the enemy
var placeY = startY;
// How many enemies have been made so far
var num = 0;
for (var j = 0; j < numCurves; j++) {
for (var i = 0; i < numEnemies; i++) {
var enemyName = "Enemy" + _root.getNextHighestDepth ();
_root.attachMovie ("Enemy",enemyName,_root.getNextHighestDepth ());
var enemy = _root[enemyName];
enemy._x = startX + (num * (enemy._width + 5));
enemy._y = placeY;
// Change the place spot, move it more towards the target spot
placeY += (startY + (CurveHeight * Direction) - placeY) / 5;
num++;
}
// Change the direction
Direction *= -1;
}

beeblebrox
07-13-2008, 10:51 AM
thanks GMaker, this looks quite good! I'll see how i can implement this.

Here's another quest to solve --
Enemies that shoot bullets at the player!

I tried the following:

onClipEvent (load) {
// difference between ship- and bullet-coordinates
dx = (_root.ship._x - _x)/100
dy = (_root.ship._y - _y)/100
//
}
onClipEvent (enterFrame) {
_x += dx
_y += dy
}


The problem here is, that the bullet goes faster when it was shot farther away from the ship...

any suggestions?

GMaker0507
07-13-2008, 12:50 PM
1000/100 = 10

50/100=.5

If you dont understand why a bullet goes faster when shot from a farther distance, you should know its simply becase 'n' divided by 100 returns a larger value as 'n' increases

Grab a calulator and check it out

To understand what i did, you should learn about vectors, if you dont already know that is.

onClipEvent(load) {
// difference between ship- and bullet-coordinates
dx = (_root.ship._x - _x)/100;
dy = (_root.ship._y - _y)/100;
var MovementSpeed=10;
var len=Math.sqrt(dx*dx+dy*dy);
dx/=len;
dy/=len;
dx*=MovementSpeed;
dy*=MovementSpeed;
}

beeblebrox
07-14-2008, 08:13 AM
Oh yeah...
I had this kind of math yeeeeears ago in school.
thanks for refreshing my memory, GMaker! :)