Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: barefoot on Mon 15/08/2011 11:53:59

Title: SOLVED: Determine players y and display Label text
Post by: barefoot on Mon 15/08/2011 11:53:59
Hi

EDIT: I guess I'm trying to determine if player y is between int and int then show this TEXT on labels.

IE (y from top) 200/170  Display this, 170/140 Display this, 140/100 Display this, 100/70 Display this,


It's if player between y ints that the problem.

Hope you understand.

-----------------------------

I'm trying to make a global so that whatever the players y is will determine Labels text
IE Lheight: 400 feet / Lspeed: 300 mph.

I would like the screen split up into say 4 horizontal lines, each will display Lheight and Lspeed Labels with appropriate text depending on y.

I am using regions to do this but fails when with an eBlock.

So far I have this but need help beacause it's not quite enough.

In Global:

function repeatedly_execute_always()

{
 
 if (cfighter1a.y >  99)
{
 Lheight.Text="0 feet";
Lspeed.Text="0 mph";

{


if (cfighter1a.y <=  219)
{
 Lheight.Text="500 feet";
Lspeed.Text="300 mph";
}


 if (cfighter1a.y < 209)
{
 Lheight.Text="300 feet";
Lspeed.Text="200 mph";


}


 if (cfighter1a.y <=  99)
{
 Lheight.Text="0 feet";
Lspeed.Text="0 mph";

}

}

}}


Also trying to STOP firing missile continuously when key is pressed.
Tried Game.DoOnceOnly but this does not stop the firing. If key is pressed action happens only once until released and pressed again.

Can you help please

barefoot

Title: Re: Determine players y and display Label text
Post by: arj0n on Mon 15/08/2011 13:01:00
Quote from: barefoot on Mon 15/08/2011 11:53:59
Also trying to STOP firing missile continuously when key is pressed.
Tried Game.DoOnceOnly but this does not stop the firing. If key is pressed action happens only once until released and pressed again.

Why not something like:

function on_key_press(eKeyCode keycode)
{
  if (((keycode == <your key>) && (IsKeyPressed(<your key>) == 0) && (<your possibly extra if-statement>))){
  .....; \\your missile-firing code
}
else {
.....; \\other possible not-missile-firing code
}}



Not tested - not sure if the 'else' part is needed...

EDIT:
For your y-position-label, do you mean you need a 'between' thing? Like:

if ((cfighter1a.y >  99) && (cfighter1a.y <  219)){
Lheight.Text="0 feet";
Lspeed.Text="0 mph";
}

I guess not because you would have thought of it yourself.... ;)
Title: Re: Determine players y and display Label text
Post by: Khris on Mon 15/08/2011 14:43:45
I have cleaned up the indentations. You need to do this yourself, especially when you want help with your code.

function repeatedly_execute_always()

  if (cfighter1a.y >  99)
  {
    Lheight.Text="0 feet";
    Lspeed.Text="0 mph";

    {
      if (cfighter1a.y <=  219)
      {
        Lheight.Text="500 feet";
        Lspeed.Text="300 mph";
      }

      if (cfighter1a.y < 209)
      {
        Lheight.Text="300 feet";
        Lspeed.Text="200 mph";
      }

      if (cfighter1a.y <=  99)
      {
        Lheight.Text="0 feet";
        Lspeed.Text="0 mph";
      }
    }
  }
}

There weren't any missing or wrong brackets though.

Just look at this code, it should be obvious why it doesn't work.
For instance the inner three if conditions are all true for a value of 99 or smaller. This doesn't mean it doesn't work but it's not exactly a good way of doing things.
Also, the last one is never going to be true since y can't be greater than 99 and at the same time smaller than or equal to 99.

function repeatedly_execute_always()
{
  int height, speed, y = cfighter1a.y;
 
  if (y <= 99)
  {
    speed = 0;
    height = 0;
  }
  else if (y <= 209)   // y: 100-209
  {   
    speed = 200;
    height = 300;
  }
  else                    // y > 209
  {                   
    speed = 300;
    height = 500;
  }

  Lspeed.Text = String.Format("%d mph", speed);
  Lheight.Text = String.Format("%d feet", height);
}


Not sure if this really is what you want, I'd find it kinda weird if the height (of my jet I suppose) jumped around like that instead of having a linear relation to the position of what I'm moving up and down on the screen.
Why not something like height = player.y * 2; or similar?
Title: Re: Determine players y and display Label text
Post by: barefoot on Mon 15/08/2011 20:02:05
Thank you Khris

Edit: seems to work ok. Needs small tweeking. Will report back.

Looking at: height = player.y * 2;

---------------------------------------------

Sorry about code...  and again, thanks for your reply.

As far as it goes it does relate to the position of the main player (plane) which can only move up and down. As player moves up or down so the height and speed labels change.

I will try what you have put. Thanks also to Arj0n.

cheers  :=

barefoot




Title: Re: Determine players y and display Label text
Post by: barefoot on Mon 15/08/2011 20:52:44
Tweeked and works well...


:=



function repeatedly_execute_always()

{

int height, speed, y = cfighter1a.y;
 
  if (y <= 99)
  {
    speed = 400;
    height = 700;

}
  else if (y <= 150)
  {
    speed = 300;
    height = 500;
  }
  else if (y <= 209)   
  {   
    speed = 200;
    height = 300;


}
  else if (y <= 220)   
  {   
    speed = 100;
    height = 200;


  }
  else                   
  {                   
    speed = 0;
    height = 0;
  }

  Lspeed.Text = String.Format("Speed: %d mph", speed);
  Lheight.Text = String.Format("%d feet", height);

 
}


Cheers for your assistance again Khris

barefoot