GMS 2 Help with a HnS Platformer (GM: Studio 2)

I need help for a script: i'm starting with a test for a future HnS Platformer game (first I make a test, then I will make a game about). My character is supposed to attack both when is on the floor, and when in jumping. The problem: when I start the game, the code doesn't work correctly
Here is the script:
Code:
//Create Event

image_index = 0;

image_speed = 0;

hsp = 0;

vsp = 0;

grv = 0.3;

walksp = 3;

attack = 0

//Step Event

key_left = keyboard_check(vk_left);

key_right = keyboard_check(vk_right);

key_jump = keyboard_check_pressed(vk_up);

var move = key_right - key_left;

hsp = move * walksp;

vsp = vsp + grv;

if (place_meeting(x,y+1,obj_wall)) && (key_jump)

{

vsp = -7;

}

if (place_meeting(x+hsp,y,obj_wall))

{

while (!place_meeting(x+sign(hsp),y,obj_wall))

{

  x = x + sign(hsp); 
}

hsp = 0

}

x = x + hsp;

if (place_meeting(x,y+vsp,obj_wall))

{

while (!place_meeting(x,y+sign(vsp),obj_wall))

{

  y = y + sign(vsp); 
}

vsp = 0

}

y = y + vsp;

if (!place_meeting(x,y+1,obj_wall))

{

sprite_index = spr_jump;

image_speed = 0;

}

else

{

if (hsp == 0)

{

  sprite\_index = spr\_walka; 

  image\_speed = 0; 
}

else

{

  sprite\_index = spr\_walk; 

  image\_speed = 1; 
}

}

if (hsp = -3)

{

image_xscale = -1

}

else if (hsp = 3)

{

image_xscale = 1

}

kShoot = keyboard_check_pressed(ord("X"));

if (kShoot) {

attack=1

}

if(place_meeting(x,y+1,obj_wall)){

grounded = true

}else{

grounded = false

}

if(grounded){

if attack==1 {

sprite_index = spr_attack;

hsp = 0;

image_speed = 1;

} else {

sprite_index = spr_walk

}

} else {

if attack==1 {

sprite_index = spr_attack_jump;

image_speed = 1;

} else {

sprite_index = spr_jump

}

}

//Animation End Event

if sprite_index = spr_attack_jump

{

sprite_index = spr_jump;

}

else if sprite_index = spr_attack

{

sprite_index = spr_walk;

}
where did I go wrong?
 

TsukaYuriko

šŸŒ 
Forum Staff
Moderator
The problem: when I start the game, the code doesn't work correctly
That's about as specific as me telling you that your post lacks critical information.

Please describe what you expect the code to do and what it does instead - there's no way we can extract that from the absolute minimum of information you've provided without being mind readers. ;)
 
That's about as specific as me telling you that your post lacks critical information.

Please describe what you expect the code to do and what it does instead - there's no way we can extract that from the absolute minimum of information you've provided without being mind readers. ;)
When is on the floor o jumping, the sprite is the same (spr_jump), and when attack is spr_attack_jump but looped
 
It wasn't written in my script o_O
What are you saying? your actual script does not contain those characters? Are you certain? Maybe try copying the script again, but either way please fix the code in your post if those slashes shouldn't be there.

Also, if your still having the problem you should post what the problem is. Is the compiler telling you there is an error, and if so what is it? If there is no error, what is the problem your having exactly?
 
What are you saying? your actual script does not contain those characters? Are you certain? Maybe try copying the script again, but either way please fix the code in your post if those slashes shouldn't be there.

Also, if your still having the problem you should post what the problem is. Is the compiler telling you there is an error, and if so what is it? If there is no error, what is the problem your having exactly?
Maybe this has happened because of copy and paste
Here is the script in the Step Event
key_left = keyboard_check(vk_left);

key_right = keyboard_check(vk_right);

key_jump = keyboard_check_pressed(vk_up);

var move = key_right - key_left;

hsp = move * walksp;

vsp = vsp + grv;

if (place_meeting(x,y+1,obj_wall)) && (key_jump)

{

vsp = -7;

}

if (place_meeting(x+hsp,y,obj_wall))

{

while (!place_meeting(x+sign(hsp),y,obj_wall))

{

x = x + sign(hsp);
}

hsp = 0

}

x = x + hsp;

if (place_meeting(x,y+vsp,obj_wall))

{

while (!place_meeting(x,y+sign(vsp),obj_wall))

{

y = y + sign(vsp);
}

vsp = 0

}

y = y + vsp;

if (!place_meeting(x,y+1,obj_wall))

{

sprite_index = spr_jump;

image_speed = 0;

}

else

{

if (hsp == 0)

{

sprite_index = spr_walk;

image_speed = 0;
}

else

{

sprite_index = spr_walk;

image_speed = 1;
}

}

if (hsp = -3)

{

image_xscale = -1

}

else if (hsp = 3)

{

image_xscale = 1

}

kShoot = keyboard_check_pressed(ord("X"));

if (kShoot) {

attack=1

}

if(place_meeting(x,y+1,obj_wall)){

grounded = true

}else{

grounded = false

}

if(grounded){

if attack==1 {

sprite_index = spr_attack;

hsp = 0;

image_speed = 1;

} else {

sprite_index = spr_walk

}

} else {

if attack==1 {

sprite_index = spr_attack_jump;

image_speed = 1;

} else {

sprite_index = spr_jump

}

}
 

MaxLos

Member
Hmm well for the looping problem I think it's because you never set your 'attack' variable back to 0 when the animation ends so you always remain in your attack state. Try changing your animation end to this:
Code:
//Animation End Event

if sprite_index = spr_attack_jump

{
attack = 0;

}

else if sprite_index = spr_attack

{

attack = 0;

}
 
Hmm well for the looping problem I think it's because you never set your 'attack' variable back to 0 when the animation ends so you always remain in your attack state. Try changing your animation end to this:
Code:
//Animation End Event

if sprite_index = spr_attack_jump

{
attack = 0;

}

else if sprite_index = spr_attack

{

attack = 0;

}
The animation for the attack is fixed, but not for attack_jump. And there is another problem to fix, this: https://imgur.com/a/gJCh6is
 

MaxLos

Member
The animation for the attack is fixed, but not for attack_jump. And there is another problem to fix, this: https://imgur.com/a/gJCh6is
Try this:
Code:
//Create Event
image_index = 0;
image_speed = 0;
hsp = 0;
vsp = 0;
grv = 0.3;
walksp = 3;
attack = 0;
grounded = false;
Code:
//Step Event

if place_meeting(x,y+1,obj_wall) grounded = true; else grounded = false; //Grounded

//Inputs
key_left = keyboard_check(vk_left);
key_right = keyboard_check(vk_right);
key_jump = keyboard_check_pressed(vk_up);
kShoot = keyboard_check_pressed(ord("X"));

//Movement
var move = key_right - key_left;
hsp = move * walksp;
vsp = vsp + grv;


//Jump
if (grounded) && (key_jump)
{
    vsp = -7;
}

//Horizontal Collision
if (place_meeting(x+hsp,y,obj_wall))
{
    while (!place_meeting(x+sign(hsp),y,obj_wall))
    {
        x = x + sign(hsp);
    }
    hsp = 0
}
x = x + hsp;

//Vertical Collision
if (place_meeting(x,y+vsp,obj_wall))
{
    while (!place_meeting(x,y+sign(vsp),obj_wall))
    {
        y = y + sign(vsp);
    }
    vsp = 0;
}
y = y + vsp;


//Animation

if !(hsp = 0) image_xscale = sign(hsp); //Flip sprite to direction it's moving in

//Not Attacking
if (attack = 0)
{  
    //Airborn
    if !(grounded)
    {
        sprite_index = spr_jump;
        image_speed = 0;
    }
  
    //Grounded
    else if (grounded)
    {
        sprite_index = spr_walk;
        if (hsp = 0) image_speed = 0; else image_speed = 1;
    }
}

//Attack
if (kShoot) and (attack = 0)
{
    if (grounded) //Ground Attack
    {
        sprite_index = spr_attack;
        image_speed = 1;
        image_index = 0;
        hsp = 0;
        attack = 1;
    }
    else if !(grounded) //Air Attack
    {
        sprite_index = spr_attack_jump;
        image_speed = 1;
        image_index = 0;
        attack = 1;
    }
}
Code:
//Animation End Event
if (attack = 1) attack = 0;
 
Try this:
Code:
//Create Event
image_index = 0;
image_speed = 0;
hsp = 0;
vsp = 0;
grv = 0.3;
walksp = 3;
attack = 0;
grounded = false;
Code:
//Step Event

if place_meeting(x,y+1,obj_wall) grounded = true; else grounded = false; //Grounded

//Inputs
key_left = keyboard_check(vk_left);
key_right = keyboard_check(vk_right);
key_jump = keyboard_check_pressed(vk_up);
kShoot = keyboard_check_pressed(ord("X"));

//Movement
var move = key_right - key_left;
hsp = move * walksp;
vsp = vsp + grv;


//Jump
if (grounded) && (key_jump)
{
    vsp = -7;
}

//Horizontal Collision
if (place_meeting(x+hsp,y,obj_wall))
{
    while (!place_meeting(x+sign(hsp),y,obj_wall))
    {
        x = x + sign(hsp);
    }
    hsp = 0
}
x = x + hsp;

//Vertical Collision
if (place_meeting(x,y+vsp,obj_wall))
{
    while (!place_meeting(x,y+sign(vsp),obj_wall))
    {
        y = y + sign(vsp);
    }
    vsp = 0;
}
y = y + vsp;


//Animation

if !(hsp = 0) image_xscale = sign(hsp); //Flip sprite to direction it's moving in

//Not Attacking
if (attack = 0)
{ 
    //Airborn
    if !(grounded)
    {
        sprite_index = spr_jump;
        image_speed = 0;
    }
 
    //Grounded
    else if (grounded)
    {
        sprite_index = spr_walk;
        if (hsp = 0) image_speed = 0; else image_speed = 1;
    }
}

//Attack
if (kShoot) and (attack = 0)
{
    if (grounded) //Ground Attack
    {
        sprite_index = spr_attack;
        image_speed = 1;
        image_index = 0;
        hsp = 0;
        attack = 1;
    }
    else if !(grounded) //Air Attack
    {
        sprite_index = spr_attack_jump;
        image_speed = 1;
        image_index = 0;
        attack = 1;
    }
}
Code:
//Animation End Event
if (attack = 1) attack = 0;
It works, thank you
Anyway, when I'm moving and then I attack, the character is still moving. Just a little bug
 
Also, MagicFool I'm just going to throw this out there, but seeing as your just getting started I think you should look into using a state machine. Writing all the conditions right into the object like that can get tedious and overly confusing after a while. A solution to that is to use a state machine, which is essentially just an organized means of getting and setting the different states/animations of your object. The problem is, if you wait to implement it 6 months after writing 20 thousand lines of code and a couple hundred conditions going back through and implementing it will be a nightmare. Just something to think about.
 

MaxLos

Member
Weird.. it's not doing that for me, the hsp = 0; for the ground attack should be stopping you. You can try adding this to the step event
Code:
if (sprite_index = spr_attack) hsp = 0;
 

MaxLos

Member
Also yeah, what robproctor83 said. I was gonna mention this too but I don't know if your planning to add more states since the "attack = 0" and "attack = 1" are basically his normal and attacking states rn
 
Top