PDA

View Full Version : [AS2] Knockback after hitting enemy <Platform Game>


phinnycupcakes
12-23-2008, 06:49 AM
Hi. I'm making a platform like game based on this tutorial

http://www.kirupa.com/developer/mx2004/platform_game.htm

The thing is, with this tutorial, you die in one hit. I want it to be so that when you hit an enemy, you lose a life, and it sets you back a few units from the enemy.

Here is the code in question. It is attached to the enemy's movieclip code.

if (this.hitTest(_root.char) && !dead) {
// if this hits the character and is not dead
_root.char.jumping = false;
// characters jumping state is set false
_root.dead = true;
// _root.dead is set true
}

Really easy to fix, huh? I've tried this

if (this.hitTest(_root.char) && !dead) {
if(_root.health == 0)
_root.dead = true;
else
{
_root.char.jumping = false;
// characters jumping state is set false
_root.char._x -= this._x - 20;
// move him to the left a little bit
_root.health -= 20;
// subtract 20 health, effectively saying he got hit.
}

}

All that does is makes the character move back, but only for an instant, and then he's back to the enemy. Why does this happen? It's because the stage follows the character funny. (the illusion of a camera focusing on the character I guess..)

onClipEvent (enterFrame) {
//////////////////////
////Ammendment 2//
//////////////////
_x = Xpos-_root._x;
//sets the X pos to the starting X postition of this MC
_root.score._x = scoreX-_root._x;
//sets the scire MCs X pos to its starting point on the screen
_root.health._x = healthX-_root._x;
//sets the health MCs X pos to its starting point on the screen
if (!_root.ground.hitTest(this._x, this._y, true) && !jumping) {
// if NOT hitting X and Y postion with the ground and NOT jumping
this._y += 6;
// Y positon moves up 6
}

So how can I get around this so that the character jumps back, and not everything in _root follows him??

I was thinking of making Everything in _root move to the left, and having the enemy move the same amount to the right, thus canceling out him moving, but what THIS does is it screws up the number of steps he has taken (a key factor of making him move left and right). In fact, this would screw up pretty much EVERY enemy if their code is based on numbers of steps taken.. Enemies would be flying off screen and such.

someone help me out! =o

Just download the FLA file on the website I gave you, and fiddle around a bit.

I hope you guys can help me out!!!! =DDD

phinnycupcakes
12-23-2008, 09:07 AM
FORTUNATELY! I was able to contact the original creator of the tutorial, and with a little help from him (minimal.. it was solved pretty easily) it turns out that moving _root WOULDN'T screw up enemy movement.

so

if (this.hitTest(_root.char) && !dead)
{
if(_root.health.text > 0)
{
_root.char.jumping = false;
// characters jumping state is set false
if(_root.char.dir == "left")
{
_root._x -= 40;
_root.char._y -= 20;
}
else if(_root.char.dir == "right")
{
_root._x += 40;
_root.char._y -= 20;
}
// the bump
_root.health.text -= 20;
if(_root.health.text <= 0)
_root.dead = true;
// subtract 20 health, effectively saying he got hit.
}
}

Is the answer.

Note that this code looks at which direction you're moving, so if you hit the enemy while you're moving left, it bounces you to the right. So you'll always be bounced away from the enemy in the appropriate direction!

FluffyPapes
12-24-2008, 06:42 PM
You can also do
if(_root.char._rotation == 90[if you're facing right] or 270 [if you're facing left])
In my opinion, I like that better.

But you know, we're not all the same and there are many different ways to do the same thing. :D