• 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!

GameMaker change the power of my shoot

Sawyer

Member
Hello everyone,

Actually i have a system to shoot :
- i press a key, and my player shoot
- i use alarm to make a couldown

It works well, but now i want to improve my system, i want to build something like in this video (at 5.15) :

if i press for exemple A 0.1 second i do i little shoot (the minimum 1 %)
if i press for exemple A 1 second i do i big shoot (50%)
if i press for exemple A 1.5 seconds i do i big shoot (75%)
if i press for exemple A 2 seconds i do i huge shoot (the maximum 100%)
if i press for exemple A 4 seconds i do i big shoot (50%)
if i press for exemple A 5 seconds i do i little shoot (the minimum, 1%)...

Can you help me?
 

Nidoking

Member
So, like, move the shooting code from Key Pressed to Key Released, and set stuff in Key Pressed to start charging and have a counter that tells you how many steps happened in between?
 

Sawyer

Member
So, like, move the shooting code from Key Pressed to Key Released, and set stuff in Key Pressed to start charging and have a counter that tells you how many steps happened in between?
Indeed,
Key_pressed = i increment from 0 % to 100%
if i reach 100%, i decrement from 100% to 0%

If i released i shoot with my xx %
 

Sawyer

Member
i tried this witout success, it stay blocked at 99%
GML:
if (mouse_check_button(mb_left)){
    power_of_shoot +=1
        if power_of_shoot >=100
            {power_of_shoot -=1}}
 

TsukaYuriko

☄️
Forum Staff
Moderator
You have no logical decoupling in your code that separates the code into the two separate thought processes (hold to charge from 0% to 100%; once it hits 100% decharge back to 0%) you've outlined above. Use a boolean variable to indicate which way the charge is moving (increasing or decreasing) and do not allow for it to increase again until it has fully depleted.

The way your code currently works, after reaching 99% charge, every step you're holding the button down, you're incrementing the charge, only to then decrement it because it's now 100.
 

Sawyer

Member
You have no logical decoupling in your code that separates the code into the two separate thought processes (hold to charge from 0% to 100%; once it hits 100% decharge back to 0%) you've outlined above. Use a boolean variable to indicate which way the charge is moving (increasing or decreasing) and do not allow for it to increase again until it has fully depleted.

The way your code currently works, after reaching 99% charge, every step you're holding the button down, you're incrementing the charge, only to then decrement it because it's now 100.
ok i understand why it don't works, can you explain me this boolean variable please
 

TsukaYuriko

☄️
Forum Staff
Moderator
A boolean variable can have only one of two possible values - true or false. In this case, I suggested using a boolean because you'll only ever need to use two different values for it.

The general idea is to assign one of the above values to this variable while the shot is charging up - for example true if the variable is named charging_up - and once it is fully charged, change it to false. While this variable is false, instead of counting up, it counts down. Once the charge is fully depleted, you'd change this variable back to true again to resume counting up.
 

Sawyer

Member
This is what i did, it works well, thank you

GML:
if (mouse_check_button(mb_left)){
    if power_of_shoot = 0{
        charge = true;
        decharge=false;}
    if power_of_shoot = 100{
        charge = false
        decharge=true;}
        
if charge = true {power_of_shoot +=1}
if decharge = true {power_of_shoot -=1}}
            
if (mouse_check_button_released(mb_left)){canShoot = true}

if (global.shoot = true && canShoot = true){   
        var dir = point_direction(x, y-sprite_height/2, mouse_x, mouse_y);
        var flipped = (mouse_x > x)*2-1;
        var gun_x = x-4*flipped;
        var x_offset = lengthdir_x(16, dir);
        var y_offset = lengthdir_y(16, dir);

        var bullet = instance_create_depth(gun_x+x_offset, y+y_offset, -1, obj_shoot);
        //global.hp -= 1;
        bullet.direction = dir;
        bullet.image_angle = dir;
        horizontal_speed = -lengthdir_x(6, dir);
        vertical_speed = -lengthdir_y(6, dir);
        power_of_shoot = 0;
        canShoot = false;   
}
 

NightFrost

Member
You've solved this already but here's an additional trick. Since there's only two directions you can use a single boolean to track current direction. Your ten first lines then turn to:
GML:
if(mouse_check_button(mb_left)){
    if(charge){
        if(++power_of_shoot == 100) charge = false;
    } else {
        if(--power_of_shoot == 0) charge = true;
    }
}
And at the end after after power_of_shoot = 0 add charge = true because you always want it to start charging upwards on every new shot.
 
Top