• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

HTML5 My keyboard_check_released is being delayed, why?

C

Cmi

Guest
So when ever I click on my keyboard letter like 'A' it is delayed and I have to click it multiple times. I have tried it with release and pressed. I think my problem may be that I am spawning to many objects. How can I fix that.
My code for my spawner:
Code:
Create Event:

execute code:

/** Wait 1 seconds and than trigger the alarm * As the room speed is 30
a second = 30 in the alarm code. 30 = 1s
*/
// Call alarm 0 every 1 second
alarm [0] = 30;
// Global boolean expression to control the letters from spawning each other
global.letterSpawn = true;


Alarm Event for alarm 0:

execute code:

// Call alarm 1
//alarm [1] = 30;
// Change was made nov 22 2016
// Goal make it spawn faster
alarm [1] = 20;

Alarm Event for alarm 1:

execute code:

// Randomize the spawn
randomize();
// Global variable to pick which spawns
global.spawn = irandom_range(1, 10);
// Spawn the letters if global.spawn equals there number
if (global.spawn == 1){
    instance_create(32 + irandom(544), 64 + irandom(352),obj_a);
    // Randomize the spawn
    randomize();
} else if (global.spawn == 2){
    instance_create(32 + irandom(544), 64 + irandom(352),obj_b);
    // Randomize the spawn
    randomize();
} else if (global.spawn == 3){
    instance_create(32 + irandom(544), 64 + irandom(352),obj_c);
    // Randomize the spawn
    randomize();
}else if (global.spawn == 4){
    instance_create(32 + irandom(544), 64 + irandom(352),obj_d);
    // Randomize the spawn
    randomize();
}else if (global.spawn == 5){
    instance_create(32 + irandom(544), 64 + irandom(352),obj_e);
    // Randomize the spawn
    randomize();
}else if (global.spawn == 6){
    instance_create(32 + irandom(544), 64 + irandom(352),obj_f);
    // Randomize the spawn
    randomize();
}else if (global.spawn == 7){
    instance_create(32 + irandom(544), 64 + irandom(352),obj_h);
    // Randomize the spawn
    randomize();
}else if (global.spawn == 8){
    instance_create(32 + irandom(544), 64 + irandom(352),obj_i);
    // Randomize the spawn
    randomize();
}else if (global.spawn == 9){
    instance_create(32 + irandom(544), 64 + irandom(352),obj_j);
    // Randomize the spawn
    randomize();
}

scr_spawn();
// Call alarm 0 to repeat it's self
alarm [0] = 1;
My code for objects that I want to spawn (This code is in all the objects I spawn only thing is the letter changes):

Code:
Create Event:

execute code:

// Set image speed to 0
image_speed = 2;
//total = 0;
// This will add anywhere between 1 and 5, an integer value, to the total.
total = irandom_range(1, 5);
// Check if total is equal to 1
if (total = 1){
// Change the image index to 1
image_index = 1;
// Set the image speed to 0
image_speed = 0;
} else if (total = 2){
// Change the image index to 2
image_index = 2;
// Set the image speed to 0
image_speed = 0;
} else if (total = 3){
// Change the image index to 3
image_index = 3;
// Set the image speed to 0
image_speed = 0;
}else if (total = 4){
// Change the image index to 4
image_index = 4;
// Set the image speed to 0
image_speed = 0;
}else if (total = 5){
// Change the image index to 5
image_index = 5;
// Set the image speed to 0
image_speed = 0;
}

Step Event:

execute code:

// If 'D' is pressed
if keyboard_check_pressed(ord('D')) {
// Add 1 to the score
global.score_1 ++;
//It clears the keyboard entres
io_clear();
// Destroy the object
instance_destroy();
}

// Set global.letterSpawn equal false
if (global.letterSpawn == false){
    // Destory the object
    instance_destroy();
    // Add 1 to the left score
    global.left ++;
}
I have tried changing the pressed to release but nothing is being solved.
 

TehCupcakes

Member
I'm actually not sure what's causing your issue, but I do see several things that could use optimizing.
Code:
/** Wait 1 seconds and than trigger the alarm * As the room speed is 30
a second = 30 in the alarm code. 30 = 1s
*/
// Call alarm 0 every 1 second
alarm [0] = 30;
As a word of advice, you can set "alarm[0] = room_speed;" This makes the code more dynamic in case you were to change the room speed.

It seems that alarm 0's only purpose is to set alarm 1; this is pointless. An alarm can reset itself when it is called. E.g. You could move your alarm 1 code to alarm 0, then at the end still set alarm[0] = 20; or what have you. This is also slightly more accurate than setting alarm[0] to 1 then alarm[1] to 20, as then there is actually a 21 step difference between spawns.

You are randomizing far more than necessary. Honestly, randomizing once at the start of the game is usually sufficient, so you don't need all those randomize() calls in alarm 1.

You could simplify your the letter creation a couple different ways. One way would be to make an array of the object ids, and access the appropriate index with the value of global.spawn. This would look something like this:
Code:
//Create
letter[0] = obj_a;
letter[1] = obj_b;
letter[2] = obj_c;
letter[3] = obj_d;
... //etc.

//Alarm 1
global.spawn = irandom(9);
instance_create(32 + irandom(544), 64 + irandom(352), letter[global.spawn]);
Another alternative would be to use a switch. While not necessarily faster to run, a switch often looks cleaner, is less tedious to code, and reduces the redundancy of multiple if conditions. I would still prefer the array in this situation, but a switch is suitable for many other applications.

Then in script spawn, you seem to have forgotten or are unaware that you can set a variable to the value of another variable. That is, you are always setting image_index to the value of total, so you can simplify all those conditions by making it into a single line:
Code:
image_index = total;
There are also a couple other inconsistencies here... For one, you say it will "add between 1 and 5 to the total", but it actually set total rather than adding to the value. Your comment appears to be different from the code's intended function. Also, be aware that subimages are indexed starting with 0 in GM:S (and most everything is zero indexed.) Therefore, you probably want 0-4, not 1-5. So you can simply do:
Code:
total = irandom(4);
image_index = total;
image_speed = 0;
Now onto the step event. This is the place where you are most likely having an issue, and here is where I am least clear about what you are intending to do. Are you sure you really want io_clear()? This may prevent other objects from reacting to key presses. If your just trying to make it so one key press = one object destroyed, even if there is more than one of the same letter... Then you could get around this by using a controller object. In this object, in step (or on key press), use instance_find(obj, 0) to get the first instance of the given object and use with() to destroy it. Make sure the instance exists before the with, though. Like so:
Code:
var obj;

if keyboard_check_press(ord('D'))
{
    obj = instance_find(obj_d, 0);
    if(obj != noone)
    {
        global.score_1++;
        with(obj)
        {
            instance_destroy();
        }
    }
}
Finally, you have this condition relating to letterSpawn... Again, your comment suggests you are setting letterSpawn but this is actually just checking the value. From the code you posted, letterSpawn is never set to false so this block will never trigger. You also seem to destroy the object again and add to some other score here, suggesting that maybe you want to add to one score or the other. I don't know because I have no idea what global.left is... This also is checked every step, not just when D is pressed, and I'm not sure if that's what you want or not.

So I'm sorry if this wasn't much help for solving your issue. I assume it's somehow related to what you have in the step event, but I'm really not sure what you're trying to do there and I couldn't figure it out based on the information you gave. I hope that you'll look over my other tips and consider them; even if they aren't useful for solving the bug, I think it's important to practice good coding style and doing so will help reduce exposure to bugs, so I thought I would provide some advice. Good luck!
 
C

Cmi

Guest
@TehCupcakes Thank you for the advice and I do have to agree some of the things I did were not good coding. I am not used to programming in GM so I am not sure how the programming style for it is. But I do appreciate your advice on how I can make my code more cleaner, which I will do right now thanks for the examples and code that you wrote. I think the
Code:
var obj;

if keyboard_check_press(ord('D'))
{
    obj = instance_find(obj_d, 0);
    if(obj != noone)
    {
        global.score_1++;
        with(obj)
        {
            instance_destroy();
        }
    }
}
will help a lot but what is var obj is that where I put my object? Also what would be a good way to declare a variable. I most likely declare using global.name but is there a better way if I may ask.
 
C

Cmi

Guest
@TehCupcakes I forgot to add the global.left is the letters that were not destroy I used a Boolean variable to try to check it that's what letterSpawn is but I think I am doing it wrong :/
 
C

Cmi

Guest
@TehCupcakes so I followed the code you gave me but am having trouble with my score it is being doubled. Cause when the letter is destroyed that's when I want to add 1 to the score. Is there a reason to why it's being doubled?
 

TehCupcakes

Member
You probably placed the code inside the letter object or some other object that there is more than one instance of.
 
Top