:pac:Hey! I’ve been working on my first game for a little while. Although it’s my first one I’ve been doing a little bit of Java before, and I followed this great tutorial in the beginning:
http://www.kirupa.com/developer/mx2004/platform_game.htm
I’ve been digging into the scripts and tweeking it to my liking, adding bunches of stuff, pretty neat graphics and so on, and I have to say I’m pretty happy with my job programming on my own so far … until I reached this annoyance.
I have a number of different kinds of enemies - all MovieClips and all with the same scripts on them (which I think is the problem…). When I only use ONE enemy on the stage it all works great, but when I add more my “pushback” code seem to mess up a bit… Here’s the deal:
When my character (_root.char) touches an enemy (currently no instance name) I want this to happen:
- Character is pushed back (speed is set to positive/negative X, depending on which side of the enemy you’re on)
- Character looses _root.health by set amount Y
- Character is invulnerable for Z milliseconds (along with pushback preventing multiple, painful hits to the face)
This is what the relevant code on my Enemies MC look like:
onClipEvent (enterFrame) {
...
...
if (this.hitTest(_root.char)) {
_root.enemytouch = true;
[COLOR="Silver"]//This part determines direction ("leftby" if on left side of enemy) for pushback.[/COLOR]
if (_root.char._x<this._x) {
_root.leftby = true;
} else {
_root.leftby = false;
}
[COLOR="silver"]//This part sets hurt = true if my character isn't dead, already hurt, or attacking.[/COLOR]
if (!_root.dead && !_root.hurt && !_root.attacking) {
_root.hurt = true;
}
} else {
_root.enemytouch = false;
}
...
...
}
This is the “hurt-detection” code part on my _root.char MC:
onClipEvent (enterFrame) {
...
...
if (_root.hurt) {
[COLOR="silver"]//This section triggers the actual pushback[/COLOR]
if (!_root.pushback) {
if (_root.leftby) {
speed = (-20);
} else {
speed = 20;
}
_root.pushback = true;
}
[COLOR="silver"]//This part checks resets it all, if I have gotten away from the enemy[/COLOR]
if (!_root.enemytouch) {
_root.hurt = false;
_root.pushback = false;
_root.health -= 5;
this.gotoAndPlay("hurt");
}
}
...
...
}
As I said, it all works dandy, as far as there’s only one enemy with the code on the stage. In that case I might go far into an enemy, take a bunch of hits, fly out, and still take only Y damage.
Now if I use two enemies or more my character may drain half his healthbar if I jump into them (if I stand still and they only nudge me I’m fine, I only loose Y health).
I think the problem might be the “_root.enemytouch”, as it is the same for all enemies, but I’m pretty sure it’s just bad code. Maybe setInterval would help for pushback, but I don’t know how to use it…
Anyway, I’m just wondering if anyone of you elite-skilled guys have any tips on better code? I appreciate all help I can get, being the noob I am!
Cheers!