PDA

View Full Version : merging code so it goes on only one mc


Kwiftee
07-25-2008, 02:17 AM
Hi.
I'm working on my first flash game, and I've done pretty well so far. I've got all the physics and stuff down, and they all work fine, it's just I want to see if I can do some stuff to make it easier for me.
You see, every time I add a new enemy, I have to add code to THREE different objects: the main character (cursor_mc), the bullets it can shoot (shota), and the enemy itself. (enemya, or enemyb, or whatever)

Here's the code I put on the main character for every enemy - inside the 'onClipEvent (EnterFrame) {' :

if (_root.enemya.hitTest(this) and (_alpha >= 100)) {
_root.hpcount -= 1;
_alpha = 10
}

(and the alpha increases every frame by about 5, giving you time to get away from the enemy before you take damage again)

The code for 'shota':

if (_root.enemya.hitTest(this) and _alpha == 100 and _alpha.enemya == 100) {
_root.score += 1;
_alpha = 0
_root.enemyahp -=1
}

So, this makes the bullet disappear after it hits something, makes your score increase by 1, and makes enemies hp decrease by 1

And, the code for the enemy itself:

onClipEvent (load) {
_root.enemyahp = 1
}

onClipEvent (enterFrame) {
if (_root.enemyahp == 0) {
_alpha = 0
}
}



This ensures that, if the hp of the enemy is 0, it disappears.



This all works fine, but it's kinda a pain, since, as I said, every time I add a new enemy, I have to paste this code in three seperate locations.

Is there anyway I can merge all this code, so I can put it on only one object? (the enemy)?

I tried just pasting it all in the one mc, changing a few things so it wasn't performing a hittest with itself and stuff, but it didn't work.
So, yeah, help?


EDIT: Almost forgot, I'm using AS2.0, Flash MX.

Kwiftee
07-26-2008, 12:46 PM
Wokay, I found a way around that, only problem is, now the damn bullets don't recognise when they're hitting the enemy.

This is the code on the enemy:

if (_root.shota.hitTest(this)) {
_root.score += 1;
_root.enemyahp -= 1;
}


And I can't see anything wrong with this. The (entire) code on the bullet is this:

onClipEvent(load)
{
if(this._name=="shota")
{
this._visible=false;
}
else
{
this._visible=true;
}
this._x=_root.cursor_mc._x;
this._y=_root.cursor_mc._y;
shotaxspeed=20*Math.sin(_root.cursor_mc._rotation* (Math.PI/180));
shotayspeed=20*Math.cos(_root.cursor_mc._rotation* (Math.PI/180));
}
onClipEvent(enterFrame)
{
this._x+=shotaxspeed;
this._y-=shotayspeed;
if(this._x<-200)
{
this.removeMovieClip();
}
if(this._x>1000)
{
this.removeMovieClip();
}
if(this._y>800)
{
this.removeMovieClip();
}
if(this._y<-200)
{
this.removeMovieClip();
}




}



And I've got the following code on the character that tells it to duplicate the bullet whenever I click:

onClipEvent(mouseDown) {
if (_root.score <= 499) {
_root.shota.duplicateMovieClip( "shota"+shotacount, shotacount+7000);
_root["shota"+shotacount]._visible=true;
shotacount++;
if(shotacount>100) {
shotacount=1;
}
}
}


NOTE: I don't -really- have an idea of how this code works, since I basically copied+pasted it from a tutorial. It seemed to work before now though, so I don't know what I'm doing wrong.

Help? Once again, I'd prefer it if changes could be made to the enemy code, so I don't have to paste anything into the bullet/character code every time I make a new enemy.

Kwiftee
07-29-2008, 11:01 PM
Uh...
Help? I can't continue really until I figure out this problem with the bullets. ;_;

Kwiftee
08-08-2008, 03:11 AM
Still been ignored. ;_;

flashaddik
08-08-2008, 03:25 AM
for (a=0;a<=100;a++) {
if (_root["shota"+a].hitTest(this)) {
_root["shota"+a]._visible=false;
_root.score += 1;
_root.enemyahp -= 1;
}
}

this should detect all of the duplicated movie clips. It should be located in an onEnterFrame.


if(this._name=="shota")
{
this._visible=false;
}
else
{
this._visible=true;
}

this would prob make all of the duplicated movie clips _visible as well b/c their name would be shota1,shota2,shota3 etc and not "shota" so take that out and put it with the hitTest as above.
Edit:
Better yet don't even use _visible remove the movie clip as below...

for (a=0;a<=100;a++) {
if (_root["shota"+a].hitTest(this)) {
_root["shota"+a].removeMovieClip();
_root.score += 1;
_root.enemyahp -= 1;
}
}