How Can I Do To Make Somebody To Kill My Main Character? [Finally Closed!]

Started by Khristian Cammilleri, Sun 26/07/2009 19:56:48

Previous topic - Next topic

Khristian Cammilleri

I Have Tried Many Times But I Can't Find The Right Script...
The Basic Idea Is :  ???
If The Main Char Is Very Close The Bad Guy Attacks Him...

What Should I Use?
IsCollidingWithChar Or AreThingsOverlapping....And How It Works?? :'(

Please Need Help  ::)


PizzaNostra!

Ghost

CollidingWithChar is okay, but it will only work if the two characters really overlap- that will be VERY close. What I would do is check the x values, as in:

if (player.x <= cEnemy.x+20)
{
 cEnemy.say("I keel u now!");
  player.say("Ohnoes I be dead!");
}

This will make the enemy attack if the player is within 20px range, or less.
If the player is able to approach the enemy from left and right, you can:

if (player.x <= cEnemy.x+20 || player.x <= cEnemy.x-20)

Khristian Cammilleri

So I Just Have To Copy Exactly That Using My Own Characters Right?
:D
That's A Very Common Question But I Never Finish To Understand It...


Thanks!  ;D
PizzaNostra!

Ghost

You will need to put the code into any room where the enemy attack can happen. if it's just one room, put it into that room's repeteadly_execute. There are more elegant ways, but it should work without cutting the framerate.

Ethan D

well you don't need to copy that exactly in fact if you copy it exactly your main character wont die it will just say what it is told to.  If you actually want him to be attacked then you will have to run the animation of the attack and then change the room to a gameover screen you have.

Also  If you only have
if (player.x <= cEnemy.x+20 || player.x <= cEnemy.x-20)

then your player will be attacked if the enemy is 20 or less pixels to the right or left but if your for instance 30 pixels above the enemy then the attack will still occur but it will look very strange and probably wouldnt make sense.  I would recommend adding to that

if(player.x  cEnemy.x +20 || player.x <= cEnemy.x=20) && ((player.y <= cEnemy.y + 3)  && (player.y >= cEnemy.y -3))


This will make it so the enemy has to be within the 20 pixels to the left or right but also has to be within a 6 pixel area either 3 above or 3 below the character so that the enemy will actually be close to the character.
If this isn't clear enough feel free to ask.

Khris

It's supposed to be
Code: ags
if (player.x <= cEnemy.x+20 && player.x >= cEnemy.x-20)


I'd do:
Code: ags
function DistWithin(this Character*, int dist) {
  int xd = player.x - this.x, yd = player.y - this.y;
  yd = yd*2;
  return xd*xd + yd*yd <= dist*dist;
}


Now you can use
  if (cEnemy.DistWithin(20)) {
    ...
in repeatedly_execute.

Note that I'm doubling yd so the area is an ellipse that's dist high and dist*2 wide.

.M.M.

Or make sprites with big transparent areas for the enemy and call AreThingsOverlapping. This works only with "Pixel-prefect detection" set as "true" (look into General setings, the "Visual" part).

Khristian Cammilleri

PizzaNostra!

Khristian Cammilleri

Ok..Now The Player is Attacked...Then He Loses 10 HP of 100 hp
But He Loses His Health Instantly...

My Code:

Code: ags
function room_RepExec()
{
if ((cEgo.x <= cZombie1.x+20 || cEgo.x <= cZombie1.x-20) && ((cEgo.y <= cZombie1.y + 10)  && (cEgo.y >= cZombie1.y -10)))
 {
 cZombie1.LockViewFrame(5, 8, 0);cZombie1.Animate(8, 3,eOnce, eNoBlock);
 PlaySound(0);gHealth-=10;
 }

How Can I Fix It?
PizzaNostra!

Matti

You could decrease the HP only during a certain frame of the zombie's animation.

Or you could use a timer variable like in the following pseudocode:

int attack=0;

// in the rep-ex:

if (zombie is close && attack==0){
 attack=30;
 hp-=10;
}

if(attack>0) attack--;

Khristian Cammilleri

Quote from: Mr Matti on Tue 28/07/2009 16:18:49
int attack=0;

// in the rep-ex:

if (zombie is close && attack==0){
 attack=30;
 hp-=10;
}

if(attack>0) attack--;
Mmm It Does The Same..  :-[
Or Perhaps I'm Putting it In the Wrong Place
PizzaNostra!

Matti

Yeah, that's because 30 is a quite small number. You should try 200 or something. One second is 40 gameloops, so if the counter is set to 30, it means, the zombie attacks (more than) once a second..

EDIT:

But you should really connect the hitpoint reduction to a certain frame of the zombie's animation. Another pseudocode:

if(zombie is close && cZombie.Frame==4){
  blablabla
}

if your animation (or the frame's) delay is larger than 0, you should add the attack variable, because otherwise the HP would be reduced as long as the frame is shown..

Khristian Cammilleri

PizzaNostra!

Khristian Cammilleri

PizzaNostra!

Khristian Cammilleri

PizzaNostra!


Khristian Cammilleri

I Need To Add One More Instance:

The Enemy Must Be In The Same Room...
Becouse If The Enemy Is Gone When I'm In The Same Coordinates of the npc
he stills taking me health!

I Need A New Instance plus the other ones

it should be like this
Code: ags
cEnemy.isInTheSameRoom(Player)


How Can I Check If they are in the same room?
PizzaNostra!

Khris


SMF spam blocked by CleanTalk