Hello,
This is the minigame I'm working on :
(https://i.imgur.com/0GPiNp2.jpg)
You have to collect ressources by moving the bucket left and right.
The left and right arrows are GUI buttons and players can click on it to move the bucket.
Now, I also want to give the players the option to use the right/left arrow keys.
But when I click on those keys, the bucket goes from left to right without stopping in the middle.
Unless I tap really fast/shortly on the arrow keys! Then it works.
My question is: is there a way to make a normal key pressing just move one place?
This is the code I'm using:
if (IsKeyPressed(eKeyLeftArrow))
{
if (object[3].X > 500)
{
aConductors_01.Play();
object[3].X = object[3].X - 70;
}
}
if (IsKeyPressed(eKeyRightArrow))
{
if (object[3].X < 640)
{
aConductors_01.Play();
object[3].X = object[3].X + 70;
}
}
Thx! :)
EDIT PS: Those lines are in room_RepExec() because that's the only place it kinda "works."
I'm assuming you don't want a Wait command in there as it would mess up other stuff, so what you could do is add a timer a different way. If you set an int to zero when the key is pressed, and then prevent further keypresses from doing anything until the timer runs out. Inside exec_repeatedly you then increment the timer until it hits the figure you want to allow another keypress to be recognised. (Each increment of the timer is 1 cycle so you can easily work out fractions of a second according to GameSpeed.)
if (IsKeyPressed(eKeyLeftArrow))
{
if ((object[3].X > 500) && (KP_timer==20))
{
aConductors_01.Play();
object[3].X = object[3].X - 70;
KP_timer=0;
}
}
if (KP_timer<20) KP_timer++;
Shouldn't it be KP_timer == 20 in line 3?
Quote from: Matti on Sat 16/12/2023 11:50:08Shouldn't it be KP_timer == 20 in line 3?
Oops yeah I'll correct that, clearly wasn't thinking straight! Thanks.
Now that I see Matti's answer I understand why i couldn't make your code work. (laugh)
(and also because I didn't fully understand it( I was coming back with questions))
But you DID give me a solution because of that great idea to use a TIMER! (nod)
And I came up with this one:
if (IsKeyPressed(eKeyLeftArrow))
{
if (object[3].X > 500 && IsTimerExpired(3))
{
aConductors_01.Play();
object[3].X = object[3].X - 70;
SetTimer (3, 5);
}
}
So thank you for your answer and your inspiration. :)
Cheers!
Why not use on_key_press instead?
You can just add the function to the room script and AGS will call it before the global one.
int pos = 1; // 0, 1 or 2
function on_key_press(eKeyCode k) {
if (k == eKeyLeftArrow && pos > 0) pos--;
if (k == eKeyRightArrow && pos < 2) pos++;
oBucket.X = 500 + pos * 70;
}
Quote from: Khris on Sun 17/12/2023 19:10:14Why not use on_key_press instead?
Because I didn't know it existed. :P
I'm pretty happy with my solution, inspired by CaptainD, but I'm intruiged by this.
For what I'm able to understand, it seems like you manage both left and right movements with that ekeyCode k, thus making the script like half the size.
After some trying I made it work (means I figured out where to place it ), so I have some questions if you don't mind:
- Are the signs ++ and -- the same as +=1 and -=1 ?
- What does the last line ( oBucket.X = 500 + pos * 70; ) mean? I can see that it calculates the movement but how?
Does the math involves the position number (0,1,2)? Is it the * sign? How does it go to the left?
AGS calls the function when a key is pressed, and passes along the keycode. Thus you can use this function to react to keypresses by checking k (or whatever else you called the variable in the declaration) against various keycodes to handle specific keys.
The bucket has three positions, so I'm using an int to store the position and make the key handling code alter the int between 0, 1 and 2.
The ++ and -- operators are shorter versions of adding / subtracting one, yes.
The last line calculates the new position (not movement) by multiplying the 0/1/2 int by 70 and adding 500.
Not sure why you're asking how the bucket goes to the left; if it's in the center at x = 570, pressing the left arrow key will set the position to 0, then set the bucket's x coordinate to 500 + 0 * 70 i.e. 500.
To be fair, the function should look like this:
function on_key_press(eKeyCode k) {
if (k == eKeyLeftArrow && pos > 0) {
ClaimEvent();
pos--;
oBucket.X = 500 + pos * 70;
}
if (k == eKeyRightArrow && pos < 2) {
ClaimEvent();
pos++;
oBucket.X = 500 + pos * 70;
}
// handle other keys
}
Yeah sorry for the basic math thing , at that point of that day my brain froze somehow :P
I will finally use your second script, it's more clear to me and the ClaimEvent will secure it al.
And I can continue adding key_press stuff.
Also, thank you for your explanations!
Cheers!
:)