# keypress problem

5 replies to this topic

### #1 KevinBlazeCoolerz

KevinBlazeCoolerz

GMC Member

• GMC Member
• 130 posts
• Version:GM8

Posted 27 April 2012 - 02:10 AM

I want to make an engine like Auditions game, the game like this:

it have some arrows (up, down, right, left) and we must type the arrow. if the first arrow typed, then we can type the second arrow, and so...

And I have make an engine, but I'm too confused to solve the problem
this is my *.gmk
http://www.mediafire...503tefuasysyd6n

you can reupload if you have solve it
thx before
• 0

### #2 04hockey

04hockey

GMC Member

• New Member
• 12 posts
• Version:GM8

Posted 27 April 2012 - 02:18 AM

I don't understand, is it like a dance dance revolution thing with the arrows?
• 0

### #3 KevinBlazeCoolerz

KevinBlazeCoolerz

GMC Member

• GMC Member
• 130 posts
• Version:GM8

Posted 27 April 2012 - 03:29 AM

I don't understand, is it like a dance dance revolution thing with the arrows?

no, the arrow cannot moving, just stay and have a random arrow

like this game
http://sandbox.yoyog...com/games/51436
• 0

### #4 FoxInABox

FoxInABox

GMC Member

• GMC Member
• 5585 posts
• Version:GM:Studio

Posted 27 April 2012 - 06:38 AM

at first I though it was a game like this:
memory_sequence_GM70.gmk

but after looking at the url it seems to be more like a:
button_match_engine_GM80.gmk

basicly what I did here was to make a list:

```tot=3; // 3 items in the list

list[0]=0 // where the last number is a direction, 0 left, 1 up, 2 rigth, 3 down
list[1]=2
list[2]=3

cur=0; // and this is what position in the list we are in, since```

we find the key we need to type in by list[cur] .. and since cur is 0 atm, so is the first key at list[0] which is 0 .. left

now ..
vk_left, vk_up, vk_right, vk_down are actually the numbers 37 38 39 40 .. so if I take away 37 from each of them, then I get the numbers we use for each direction, 0 1 2 3..

so to detect a keypress:
```if keyboard_check_pressed(vk_left)
if list[cur]=0 // if it is the correct direction for that position in the list
cur+=1 // then move to the next key in the list

if keyboard_check_pressed(vk_up)
if list[cur]=1
cur+=1```
and the same for the rest of the buttons
```for(i=0;i<tot;i+=1)
if cur>i // if we have typed in this one
draw_sprite(spr_button2, list[i] , x+i*32, y) // then draw it with blue
else
draw_sprite(spr_button, list[i] , x+i*32, y) // else draw it with the white one```

this will go thru every item in the list until i gets higher then tot..

it will use the key numbers as image index, so put your sprites in the rigth order and it should show up correctly..

subimage list[0] will be drawn at x+0*32
subimage list[1] will be drawn at x+1*32
and so on

hopes this makes any sence..

and yeah .. I noticed now that I have typed right wrong ALL the way thru this post ><;;
• 0

### #5 annihilator127

annihilator127

GMC Member

• GMC Member
• 156 posts
• Version:GM8

Posted 27 April 2012 - 06:39 AM

Hi Kevin!

I think your problem is the order in which it checks each arrow. When it checks one, and finds that it's the 'global.done' one and that you've pressed the correct key, it advances global.done. Then, it checks the next arrow, and since global.done has just been advanced and a full step has not yet passed with the input key down, it registers the input a second time.
One way to solve that would be to create a new variable, global.key_handled, to prevent the multiple input. Set it to true when an input is passed, set it to false in a Begin Step or End Step event (probably in obj_control), and refuse to register an input if it's still true.

obj_btn1 Step event:
```
//sc_auditions(image_index)

//you can use the code above or you can use the code below

if myid = global.done && !global.key_handled
{
if keyboard_check_pressed(vk_right)
{
spr = spr_button2
global.done +=1
global.key_handled = true;
}
else
if keyboard_check_pressed(vk_left) or keyboard_check_pressed(vk_down)
or keyboard_check_pressed(vk_up)
game_restart()
}
```

Edited by annihilator127, 27 April 2012 - 06:40 AM.

• 0

### #6 KevinBlazeCoolerz

KevinBlazeCoolerz

GMC Member

• GMC Member
• 130 posts
• Version:GM8

Posted 27 April 2012 - 12:17 PM

at first I though it was a game like this:
memory_sequence_GM70.gmk

but after looking at the url it seems to be more like a:
button_match_engine_GM80.gmk

basicly what I did here was to make a list:

```tot=3; // 3 items in the list

list[0]=0 // where the last number is a direction, 0 left, 1 up, 2 rigth, 3 down
list[1]=2
list[2]=3

cur=0; // and this is what position in the list we are in, since```

we find the key we need to type in by list[cur] .. and since cur is 0 atm, so is the first key at list[0] which is 0 .. left

now ..
vk_left, vk_up, vk_right, vk_down are actually the numbers 37 38 39 40 .. so if I take away 37 from each of them, then I get the numbers we use for each direction, 0 1 2 3..

so to detect a keypress:
```if keyboard_check_pressed(vk_left)
if list[cur]=0 // if it is the correct direction for that position in the list
cur+=1 // then move to the next key in the list

if keyboard_check_pressed(vk_up)
if list[cur]=1
cur+=1```
and the same for the rest of the buttons
```for(i=0;i<tot;i+=1)
if cur>i // if we have typed in this one
draw_sprite(spr_button2, list[i] , x+i*32, y) // then draw it with blue
else
draw_sprite(spr_button, list[i] , x+i*32, y) // else draw it with the white one```

this will go thru every item in the list until i gets higher then tot..

it will use the key numbers as image index, so put your sprites in the rigth order and it should show up correctly..

subimage list[0] will be drawn at x+0*32
subimage list[1] will be drawn at x+1*32
and so on

hopes this makes any sence..

and yeah .. I noticed now that I have typed right wrong ALL the way thru this post ><;;

wow, it's nice

Hi Kevin!

I think your problem is the order in which it checks each arrow. When it checks one, and finds that it's the 'global.done' one and that you've pressed the correct key, it advances global.done. Then, it checks the next arrow, and since global.done has just been advanced and a full step has not yet passed with the input key down, it registers the input a second time.
One way to solve that would be to create a new variable, global.key_handled, to prevent the multiple input. Set it to true when an input is passed, set it to false in a Begin Step or End Step event (probably in obj_control), and refuse to register an input if it's still true.

obj_btn1 Step event:

```
//sc_auditions(image_index)

//you can use the code above or you can use the code below

if myid = global.done && !global.key_handled
{
if keyboard_check_pressed(vk_right)
{
spr = spr_button2
global.done +=1
global.key_handled = true;
}
else
if keyboard_check_pressed(vk_left) or keyboard_check_pressed(vk_down)
or keyboard_check_pressed(vk_up)
game_restart()
}
```

thx annihilator127, I will try this too
• 0

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users