Checking whether character is facing left or right...

Started by Snake, Tue 09/10/2007 15:09:28

Previous topic - Next topic

Snake

Hello again,

This is so simple yet boggling the hell out of me. All I want to do is to check whether a character is looking to the LEFT or RIGHT before running the appropriate animation. I thought it would be simple, but I can't seem to make it work.

How do I check where they are looking?

Here's what one of my many tries looked like:
Code: ags
if (character[11].FaceLocation(character[11].x-160, character[11].y)==1){


The game runs fine but the character just doesn't do anything. The only thing I'm doing is forcing this character to look that way all the time, canceling out any animations.


Thanks a million in advance,


--Snake
Grim: "You're making me want to quit smoking... stop it!;)"
miguel: "I second Grim, stop this nonsense! I love my cigarettes!"

Khris


Snake

Blargh...

That didn't seem to want to work either. All it did was spin the character around in circles.


--Snake
Grim: "You're making me want to quit smoking... stop it!;)"
miguel: "I second Grim, stop this nonsense! I love my cigarettes!"

Ashen

How are you using it? Something like:
Code: ags

if (character[11].Loop == 1) {
  // Character facing left code here
}


Should work without spinning the character around. (Unless that's what the code does, of course...)
I know what you're thinking ... Don't think that.

Khris

The following WILL work:

if (character[11].Loop==1) Display("The character is facing left.");
else if (character[11].Loop==2) Display("The character is facing right.");

This code doesn't turn the character in any way, it only checks her orientation.

Snake

Nope, that didn't work either. Nothing even displayed, I actually thought it would result in an endless loop.

Here's what I'm doing (this code is in the Repeatedly Execute):

The dragon pops up through a lava pit and follows the character. The dragon stays in the pit.

When a timer is finished he is supposed to shoot fireballs at the player.
This part works fine, no problems there.
This morning I made the animation of the dragon's mouth opening to release the fireball.
That worked fine. The only problem was that no matter where he was facing, the animation played and showed him facing the right.
Now I need to check when he's facing left or right.
My first try was checking where the player was, but the resulted in the same thing: No fireballs being released.
Here's the code:
Code: ags
//////FIREBALL SHOOTING//////////////////////

if (((GetGlobalInt(0)==0)&&(IsTimerExpired(1)==1)&&(character[11].Room==8))){ //fireball ready to be fired
  if (character[11].Loop==2){//facing right
    Display("Dragon is facing RIGHT");
    SetGlobalInt(0, 1);
    object[4].Visible=1;
    PlaySound(4);
    character[11].Animate(4, 2, eOnce, eBlock, eForwards);
    //Wait(1);
    object[4].Move(character[1].x,character[1].y-10,5,eNoBlock,eAnywhere);//Fireball is released
    fireball=1;
  }
  else if (character[11].Loop==1){//facing left
    Display("Dragon is facing LEFT");
    SetGlobalInt(0, 1);
    object[4].Visible=1;
    PlaySound(4);
    character[11].Animate(5, 2, eOnce, eBlock, eForwards);
    //Wait(1);
    object[4].Move(character[1].x,character[1].y-10,5,eNoBlock,eAnywhere);//Fireball is released
    fireball=1;
  }
}



--Snake
Grim: "You're making me want to quit smoking... stop it!;)"
miguel: "I second Grim, stop this nonsense! I love my cigarettes!"

Ashen

There's no obvious reason that code wouldn't work - how much, if any, actually runs?
One thing I'd suggest is add a Display command, just to check what the character's Loop is before it starts:
Code: ags

if (((GetGlobalInt(0)==0)&&(IsTimerExpired(1)==1)&&(character[11].Room==8))){ //fireball ready to be fired
  Display("%d", character[11].Loop);
  if (character[11].Loop==2){//facing right
  // Etc.


EDIT:
Oh, there's the very, very obvious (so obvious it's one of the easiest things to miss...) - have you set up the animation Loops properly? Do Loops 4 and 5 have the dragon facing the right way?
I know what you're thinking ... Don't think that.

Khris

It depends on the condition and other things, but as a general rule, one should always check for the timer's expiration separately.
Because afaik, IsTimerExpired() will only return true once.

Although in this case, IsTimerExpired() won't be tested unless GlobalInt(0) equals 0, so it should work.

You can shorten the code to the following:

Code: ags
if (GetGlobalInt(0)==0 && IsTimerExpired(1) && character[11].Room==8) { //fireball ready to be fired
  SetGlobalInt(0, 1);
  object[4].Visible=1;
  PlaySound(4);
  fireball=1;

  if (character[11].Loop==2) { //facing right
    Display("Dragon is facing RIGHT");
    character[11].Animate(4, 2, eOnce, eBlock, eForwards);
  }
  else if (character[11].Loop==1) { //facing left
    Display("Dragon is facing LEFT");
    character[11].Animate(5, 2, eOnce, eBlock, eForwards);
  } 

  object[4].Move(character[1].x,character[1].y-10,5,eNoBlock,eAnywhere); //Fireball is released
}


You could even use character[11].Animate(character[11].Loop+3, 2, eOnce, eBlock, eForwards); instead of the if-else-block.

frission

You could also, for debugging purposes, have it tell you which loop the dragon was facing. If it isn't triggering for ==1 or ==2, it might be set up so that facing right is on a different loop number. You could have an }else if{ in there which would tell you what loop the dragon was on if it wasn't 1 or 2.

Snake

Ashen:
Displaying before-hand worked.

So after watching this for a few minutes, it seems to me that the code I have waits to throw a fireball when all of those things happen similtaniously. It checks the GI, the timer, CHAR[11] to be in room8 AND if the dragon is facing either right or left before shooting the fireball. That's not what I intended.

After the GI, timer and dragon are all set together, THEN that's when I wanted to check if he's facing right or left.

Let me try to reconstruct my code unless someone's got another idea.

Kris, I still have to try your code...


--Snake
Grim: "You're making me want to quit smoking... stop it!;)"
miguel: "I second Grim, stop this nonsense! I love my cigarettes!"

Ashen

What did it display? I take it it was either 1 or 2 since you didn't say otherwise?

Quote
After the GI, timer and dragon are all set together, THEN that's I wanted to check if he's facing right or left.

What does 'all set together' mean? At the moment, IF all the conditions are met (GI value and Room, at the time the Timer expires) THEN then rest of the code runs. Since the Display command works, it looks like they ARE 'all set together'.
Do you mean you need to make the dragon face the player AFTER the timer expires, but BEFORE firing the ball? In that case, you'll need a FaceLocation command in there, as well as the Loop check:
Code: ags

cDragon.FaceCharacter(player);
if (cDragon.Loop == 1) 
  cDragon.Animate(5, 2, eOnce, eBlock, eForwards);
else if (cDragon.Loop == 2) 
  .Animate(4, 2, eOnce, eBlock, eForwards);
I know what you're thinking ... Don't think that.

Snake

Yes, I wanted the timer, the GI and room8 to be checked (simultaneously) BEFORE I checked if he is facing left or right. That's how I coded it.

But after seeing the loops displayed, it seems to me that the code only fires a fireball if the dragon is facing either left or right directly and only after the timer ends.

--EDIT--
So it's like a 1 out of 100 chance that he'll be facing left or right when the timer ends. I wanted it so the fireballs are launched any time after the timer is finished counting down. Meaning, there's a slight pause before the dragon starts spitting them out.

I hope that cleared it up :)


--Snake
Grim: "You're making me want to quit smoking... stop it!;)"
miguel: "I second Grim, stop this nonsense! I love my cigarettes!"

Ashen

Quote
I wanted the timer, the GI and room8 to be checked (simultaneously) BEFORE I checked if he is facing left or right. That's how I coded it.
And that's what it does, in your code and Khris'. If ALL of those conditons aren't met, the direction check doesn't happen.

Quote
But after seeing the loops displayed, it seems to me that the code only fires a fireball if the dragon is facing either left or right directly.
Well, you asked for it to check if the character was FACING left or right, and so that's what the code does ... If you need it to judge where the player is in relation the the dragon (i.e. he's slightly to the left, but also below, so the dragon's loop is actually 0), you're probably better comaparing the characters' x coords. (You said you tried something like that, and it didn't work, but not exactly what you did, or how it didn't work).

Quote
...and only after the timer ends
Why would you want the fireball to fire before the timer expires? Isn't the point of the timer to tell the dragon when to fire?

EDIT AFTER SNAKE'S EDIT:

Ah, Gotcha....
As Khris said, IsTimerExpired is only true once - the exact loop the timer runs out. After that, it's false again.
How about making two seperate checks: One checks the Timer, and when it expires sets a CanFireNow variable to true (or is that what fireball = 1; is meant to do in your code?), the other checks CanFireNow (or fireball) AND the other things, and handles the actual firing. You might need some other check (e.g. against character[11].Animating) to make the animation run at the proper time, but perhaps not as it's a Blocking anim.
I know what you're thinking ... Don't think that.

Snake

Fireball = 1; is something else.

Yeah, I'll try setting a seperate variable. This nonsence must come to an end. It's always the stupid little things that seems to take the longest. Without worrying which way the dragon is facing, he spits out fireballs flawlessly.

Haha, I could easily make him a two or three headed dragon and it wouldn't matter ;) But, meh, I'll try to finish this the way I wanted it :)


--Snake
Grim: "You're making me want to quit smoking... stop it!;)"
miguel: "I second Grim, stop this nonsense! I love my cigarettes!"

Ashen

Quote
Without worrying which way the dragon is facing, he spits out fireballs flawlessly.

Does he spit out fireballs flawlessly, or does he spit out a fireball, singular?
It might be a bit pedantic, but even without the Loop check, that code should only be good for a single ball before the conditions go out of alignment, so to speak. Sorry, just trying to get a handle on what exactly you're after...

Sometime after the timer has expired, but not necessarily the very second it does, the dragon can shoot a fireball, yes?
What determines when it fires, if not the timer? For example, should it be the first time the dragon faces left or right after the Timer expires? If the dragon is facing left/right AND the player is in a certain area (on a direct line, within a given radius, etc)?

Is the dragon automatically updated to face the player at all times, or does there need to be some code to handle that, too?
I know what you're thinking ... Don't think that.

Snake

The dragon himself is following the player from inside the lava pit.

The fireballs are on their own timer and variable. Once a fireball is shot the variable shows that it has been fired. A short timer is set and the variable is updated again.

Like I said, everything works fine except when I try to check where the dragons head is turned before shooting. Obviously I don't want him shooting a fireball at the character when his head is turned in the opposite direction.


I'm working on a new code now. I'm just about finished. I've got a good feeling it'll work.

--EDIT--
Alright. My new code works great except for one small thing that I absolutely do not understand.

After the dragon fires his fireballs (when the timer is up for him to dive back under) he dives back down and I get an error saying that there's an invalid loop number specified. Now, I've checked and checked and the loop number is the right one. It happens for both left and right. Lines 162 and lines 172. Invalid loop number. Loops 4 and 5. They are the exact loops I want for that animation. They worked fine before, now they don't.
???


--Snake
Grim: "You're making me want to quit smoking... stop it!;)"
miguel: "I second Grim, stop this nonsense! I love my cigarettes!"

Ashen

Do you change the View when the dragon 'dives back down'? If so, does the new View have loops 4 and 5?

Whatever the answer to them; if you've split it into two checks like I suggested, changing the CanFireNow variable back to false when the dragon disappears might solve the problem. Assuming I've read it right, and the error only happens after that - I'd guess there's some change that stops them working (View seems most likely), so preventing the Animate command from trying to run could stop the error.
I know what you're thinking ... Don't think that.

Snake

Alrighty.

I don't know what I've done exactly but I've taken care of the error messege and it works.

Now, it seems like every time I fix one thing, more crap happens.

When the dragon surfaces now, a fire ball shoots from where he dove down. I've looked through the code and what I've changed and I don't see anything that would cause this. It doesn't happen every time, but quite a bit.

Also, as he surfaces, the player either stops moving or keeps going (without holding the arrow keys) while the dragon's animating. I'm now using eNoBlock and no wait commands - I don't understand why that's happening. I think I may just go back to basics where everything worked fine. I'm sick of this ;)


-----E D I T-------
Problems solved! I scanned and I analyzed and I scoped and killed those annoying bugs.

Thanks guys for all of your help!


--Snake
Grim: "You're making me want to quit smoking... stop it!;)"
miguel: "I second Grim, stop this nonsense! I love my cigarettes!"

SMF spam blocked by CleanTalk