GML difficulties when creating player that shoot

R

royer14

Guest
Hi, I have difficulties creating a player to shoot, I have taken a course on the internet and decided to use the code and implement it in my source, but I have lost, can you check the code and say where can I place the said action?

Object Player
event create

Code:
///create event
spd = 8;
acc = 2;
vspd = 0;
hspd = 0;
grav =1.2;
image_speed = 0.4;
estate = mov_estate;
now I execute the state in the step event
Code:
///control of state
script_execute(estate);
Once the action is completed on the object player, we proceed to create the script
name script---> mov_estate

Code:
///mov_estate()
var right = keyboard_check(vk_right);
var left = keyboard_check(vk_left);
var up = keyboard_check(vk_up);
var up_release = keyboard_check_released(vk_up);
var down = keyboard_check(vk_down);
var shooting = keyboard_check(vk_space);  // Here is the variable, now I don't know where to create the action

if(!place_meeting(x,y+1,o_Solid))
    {
        vspd += grav;
        //player in air
        sprite_index = s_p_jump;
        image_index = (vspd > 0);
        //control jump
        if (up_release && vspd <-6)
        {
            vspd =-6;
        }
    }
    else {
            vspd = 0;
            //code for jump
            if (up)
            {
                vspd =-18;
            }
            //player in floor
            if (hspd == 0 and !shooting)  // Here I put the variable, but it is not correct
                {
                    sprite_index = s_p_idle; //sprite idle
                }
                else{
                        sprite_index = s_p_walk; //sprite walk
                }
    }
    //control speed
    if (right || left)
        {
            hspd +=(right - left)*acc;
            hspd_dir = right - left;       //
            if (hspd > spd)
                {
                    hspd = spd;
                }
            if (hspd < -spd)
                {
                    hspd = -spd;
                }
        }else
            {hspd =0;
               aplic_friccion(acc);
            }
        //orientation of sprite
        if (hspd != 0)
            {
                image_xscale =sign(hspd);   
            }
        //scrip move 
        movimiento(o_Solid);   // other script
//check of ledge grav
var falling = y-yprevious > 0;
var wasnt_wall = !position_meeting(x+17*image_xscale,yprevious,o_Solid);
var is_wall = position_meeting(x+17*image_xscale,y,o_Solid);

if (falling && wasnt_wall && is_wall)
    {
        hspd = 0;
        vspd = 0;
        
        //repair pixel fail of ledge grav
        while(!place_meeting(x+image_xscale,y,o_Solid))
            {
                x += image_xscale;
            }
        // create mov
        while(position_meeting(x+image_xscale,y-1,o_Solid))
            {
                y -=1;
            }
        sprite_index = s_p_ledge;
        estate = ledge_grab;   // other sprite
    }
Here is my variable and a stupid action that I put
var shooting = keyboard_check(vk_space);
if (hspd == 0 and !shooting)



name of script ----> ledge_grab()
Code:
///ledge_grab()
var up = keyboard_check(vk_up);
var down = keyboard_check(vk_down);

if (down)
    {
        estate =  mov_estate;
    }
if (up)
    {
        estate = mov_estate;
        vspd = -18
    }
collision
name script --->movimiento
Code:
///movimiento()
var colision_obj = argument0;  // O_solid is  the collision
//collision horizontal
if (place_meeting(x+ hspd,y,colision_obj))
    {
        while(!place_meeting(x+ sign(hspd),y,colision_obj))
            {
                x += sign(hspd);
            }
         hspd = 0;
    }
x +=hspd;
//collision vertical
if (place_meeting(x,y+vspd,colision_obj))
    {
        while(!place_meeting(x,y + sign(vspd),colision_obj))
            {
                y += sign(vspd);
            }
         vspd = 0;
    }
y +=vspd;
my only goal is that when I press the space key, the player changes sprite to shoot and also when he jumps execute the same action.
 
B

Becon

Guest
You could put it in a key press event to the proper key. You could have a "control" section in your player that has specific states that when you press certain buttons it will automatically change the sprite_index of the character. I see some sprite_index calls in one of your scripts but in your code above, maybe this isn't really a thing but I see:

Code:
///control of state
script_execute(estate);
But everything else has ///mov_estate()

Might just need to change it to
Code:
 script_execute(mov_estate)
Let me know if I was any help!!
 
R

royer14

Guest
You could put it in a key press event to the proper key. You could have a "control" section in your player that has specific states that when you press certain buttons it will automatically change the sprite_index of the character. I see some sprite_index calls in one of your scripts but in your code above, maybe this isn't really a thing but I see:

Code:
///control of state
script_execute(estate);
But everything else has ///mov_estate()

Might just need to change it to
Code:
 script_execute(mov_estate)
Let me know if I was any help!!
If I understand, I will write the code. I wanted to add a trigger action to the character
 
Top