【GAMEMAKER】Key Mapped

Information about object: obj_MapKey

Sprite: spr_Map
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:
Children:
Mask:
No Physics Object
Create Event:
execute code:

Mapping = false
scr_MapNames()
//you cannot use the variable "kd"
//if you want to use it, change the variable name in the scripts

//don't forget to check each of my instances' creation codes.
//go to the room I'm in, and ctrl+RightClick one of my instances.
//then click "Creation Code." You can see a few of my variables there.
//This is so each instance of me has different variable values.
Mouse Event for Left Released:
execute code:

//only this key is mapping
obj_MapKey.Mapping = false
Mapping = true
Draw Event:
execute code:

draw_set_halign(fa_center)
draw_sprite(sprite_index,-1,x,y) //draw the box
draw_text(x-48,y+8,draw_string) //draw the description
draw_text(x+112,y+8,draw_mapped) //draw the key
draw_set_halign(fa_left)
//draw an outline
draw_line(x-96,y,x+160,y)
draw_line(x-96,y,x-96,y+32)
draw_line(x-96,y+32,x+160,y+32)
draw_line(x+160,y,x+160,y+32)

//if waiting for remap, draw remap info
if Mapping {
 //draw_background(bg_White,272,238)
 draw_set_halign(fa_center)
 draw_text(400,260,"Press Desired Key for Action:")
 draw_text(400,276,draw_string)
}

Key Release Event for <any key> Key:
execute code:

if Mapping {
 scr_MapSet() //remap the key pressed
}

Information about object: obj_Player
Sprite: spr_A1_W_D
Solid: true
Visible: true
Depth: 1
Persistent: true
Parent:
Children:
Mask:
No Physics Object
Create Event:
execute code:

image_speed = 0 //don't animate yet
Step Event:
execute code:

//handle movement
//notice how it checks each key based on variable
//check the room creation code to see these variables initialized
//the variables are reset (remapped) using scr_MapSet - the execute string

if keyboard_check(global.DownKey) {
 //move; set sprite; animate sprite
 y += 4; sprite_index=spr_A1_W_D; image_speed = .4
}
if keyboard_check(global.LeftKey) {
 x -= 4; sprite_index=spr_A1_W_L; image_speed = .4
}
if keyboard_check(global.RightKey) {
 x += 4; sprite_index=spr_A1_W_R; image_speed = .4
}
if keyboard_check(global.UpKey) {
 y -= 4; sprite_index=spr_A1_W_U; image_speed = .4
}

//if he's not moving
if !keyboard_check(global.DownKey) && !keyboard_check(global.LeftKey)
&& !keyboard_check(global.RightKey) && !keyboard_check(global.UpKey) {
 image_index = 0 //place on first frame of sprite (standing)
 image_speed = 0 //stop animation
 //image_single is no longer supported
}

///////////
//Set Key//
///////////
var kk;
kk = keyboard_lastkey
if kd[kk] != "" { //make sure the key has a name
 draw_mapped = kd[kk] //set the drawing name to the name
 if(mapped_var == "global.UpKey"){
    global.UpKey = kk
 }
 if(mapped_var == "global.DownKey"){
    global.DownKey = kk
 }
if(mapped_var == "global.RightKey"){
    global.RightKey = kk
 } 
 if(mapped_var == "global.LeftKey"){
    global.RightKey = kk
 } 
 //execute_string(mapped_var+" = "+string(kk)) //set the variable to the key
 Mapping = false
}

//////////////////////////
//define each key's name//
//////////////////////////
/*
This is pretty much just a collection of names for the keys.
This way, the user knows what key each thing is set to.
*/
var m;
for (m = 0; m <= 255; m += 1) { //set all keys to nameless
 kd[m] = ""                     //only named keys can pass
}
kd[8] = "Backspace"
kd[13] = "Enter Key"
kd[16] = "Shift Key"
kd[17] = "Control Key"
kd[18] = "Alt Key"
kd[19] = "Pause/Break"
kd[27] = "Escape Key"
kd[32] = "Space Key"
kd[33] = "Page Up"
kd[34] = "Page Down"
kd[35] = "End Key"
kd[36] = "Home Key"
kd[37] = "Left Arrow"
kd[38] = "Up Arrow"
kd[39] = "Right Arrow"
kd[40] = "Down Arrow"
kd[45] = "Insert Key"
kd[46] = "Delete Key"
for (m = 65; m <= 90; m += 1) { //the letter keys
 kd[m] = chr(m)+" Key" //"A Key", "Z Key"
}
for (m = 96; m <= 105; m += 1) { //the numpad keys
 kd[m] = "Numpad "+string(m) //"Numpad 0", "Numpad 9"
}
kd[106] = "Multiply Key"
kd[107] = "Add Key"
kd[109] = "Subtract Key"
kd[110] = "Decimal Key"
kd[111] = "Divide Key"
for (m = 112; m <= 123; m += 1) { //the F keys
 kd[m] = "F"+string(m-1)+" Key" //"F1 Key", "F12 Key"
}

No comments:

Your Brain on Art

My Q&A with Susan Magsamen and Ivy Ross ͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏    ...

Contact Form

Name

Email *

Message *