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

The player moves too much when it start shooting while walking

C

clyver

Guest
(Sorry for bad english)

I know it is a especific problem, and it's a lot of code do analyze, but, i just don't know what to do.

When the player is walking, and I command it to attack by pressing "X", he attacks, but in the moment I command it, before it starts to attack, it moves about 5 pixels to the direction i'm moving it.

It is too weird, and you may think it's about the sprite or something like that, but it is not. I tested a lot of things, and even without any command or walk sprite or walk and attack sprite, it happens.

Just when I press "X", it happens.

You don't need to read all of it, I will just show all my codes. Just read what you think is necessary.


PLAYER OBJECT EVENTS:

--CREATE--

Code:
hsp = 0;
vsp = 0;
grv = 0.2;
walksp = 4;

--STEP--

Code:
//Commands
key_left = keyboard_check(vk_left);
key_right = keyboard_check(vk_right);
key_jump = keyboard_check_pressed(vk_up);



//Movement
var move = key_right - key_left;
hsp = move * walksp;
vsp = vsp + grv;
if (place_meeting(x,y+1,OBloco2)) && (key_jump)
{
    vsp = -7.1;
}



//Running(this is not walk, it is another aside command)
if (keyboard_check(ord("Z")))
{
walksp = 6;
}
else
{
walksp = 3;
}
if (keyboard_check(ord("Z"))) && (keyboard_check(ord("X")))
{
walksp = 3;
}



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



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



//All kind of attack animation
if (!place_meeting(x,y+1,OBloco2)) && (keyboard_check(ord("X")))
{

       if(sign(vsp) > 0)
    {
    sprite_index = SPiresAtP;
    }
}
else
{
if (!place_meeting(x,y+1,OBloco2))
{

    sprite_index = SPiresP;
    image_speed = 0;
    if(sign(vsp) > 0) image_index = 1; else image_index = 0;
 
}
else
{
    image_speed = 1;
    if (hsp == 0)
    {
         sprite_index = SPires;
    }
    else
    {
         sprite_index = SPiresA;
         image_speed = 0.5
    }
if (keyboard_check(ord("X")))
{
sprite_index = SPiresAt
}

}
}
if (hsp != 0) image_xscale = sign(hsp);



//Attack while walk animation
if (keyboard_check(ord("X"))) && (key_left)

{
    if (!place_meeting(x,y+1,OBloco2))
    {
    exit;
    }
sprite_index = SPiresAtA;
image_speed = 0.5;
}

if (keyboard_check(ord("X"))) && (key_right)
{

    if (!place_meeting(x,y+1,OBloco2))
    {
    exit;
    }
sprite_index = SPiresAtA;
image_speed = 0.5;
}



//Running animation
if (keyboard_check(ord("Z"))) && (key_right)
{
if (!place_meeting(x,y+1,OBloco2))
    {
    exit;
    }
sprite_index = SPiresC;
image_speed = 0.75;
}
if (keyboard_check(ord("Z"))) && (keyboard_check(ord("X")))
{
sprite_index= SPiresAtA
}

GUN OBJECT EVENT:
Code:
x = OPires.x;
y = OPires.y+10;
image_xscale = OPires.image_xscale;


firingdelay = firingdelay - 1;
if (keyboard_check(ord("X")) && (firingdelay < 0))
{

firingdelay = 10;
with (instance_create_layer(x,y,"Balas",OTiro))
{
speed = 10;
direction = -90 + 90 * other.image_xscale;
image_angle = direction;
}
}

SHOT OBJECT CODE

Code:
dir = 1;
if keyboard_check(vk_right) +(-keyboard_check(vk_left)) !=0
{
dir = keyboard_check(vk_right) +(-keyboard_check(vk_left));
image_xscale = dir;
}

Code:
if (keyboard_check(ord("X")))
{
    sprite_index = STiro;
    image_speed = 1;
}

Code:
if (place_meeting(x,y,OBloco)) instance_destroy();

Here's the meaning of all sprites and object names

SPires = Player standing
SPiresA = Player walking
SPiresP = Player jumping
SPiresAt = Player attacking
SBloco2 = Standard block
STiro = Shot
SPiresAtP = Player attacking while jumping
SPiresAtA = Player attacking while walking
SpiresC = Player running

OPires = Player
OArma = Gun
OTiro = Shot
OBloco2 = Standard block
 
Last edited by a moderator:

Simon Gust

Member
Code seems ok I guess.
Are you absolutely sure, all sprites have their origin y coordinate set to center?

Can you elaborate a little more? Is the shift instant or what exactly happens.
Try tracking what hsp does at that time.
with a draw_text in the draw GUI event
Code:
draw_text(20, 20, "hsp: " + string(hsp));
 

OverLogic

Member
if all sprites are good do the draw_text like simon gust mentioned right here and if its not the sprites and the code is weird then fix your "else" on your keys
 

OverLogic

Member
yeah i just checked ti again remove the "else" since "else" means any other key on your keyboard and check it again.
 
C

clyver

Guest
Code seems ok I guess.
Are you absolutely sure, all sprites have their origin y coordinate set to center?

Can you elaborate a little more? Is the shift instant or what exactly happens.
Try tracking what hsp does at that time.
with a draw_text in the draw GUI event
Code:
draw_text(20, 20, "hsp: " + string(hsp));
I tested it, it's not any sprite. All of them have their origin set to center.
I made a video to show what happens. That white thing is the shot. When I press "X", it shoots, and in that shift moment, the problem happens.
The video shows the scene with and without the walking and shooting sprites.
 
Last edited by a moderator:
C

clyver

Guest
yeah i just checked ti again remove the "else" since "else" means any other key on your keyboard and check it again.
What elses exactly? from //All kind of attack animation?
I did it, the problem still happens.
I actually removed the //Attack while walk animation and //All kind of attack animation just to see what happens. The problem still happens.
 
C

clyver

Guest
Code seems ok I guess.
Are you absolutely sure, all sprites have their origin y coordinate set to center?

Can you elaborate a little more? Is the shift instant or what exactly happens.
Try tracking what hsp does at that time.
with a draw_text in the draw GUI event
Code:
draw_text(20, 20, "hsp: " + string(hsp));
Also, the draw GUI event
This is weird.
When the player is walking: hsp 0.50
When the shift instant and the bug is happening: hsp 10
When the player is walking and attacking: 0.50

(Also: i changed the player walskp from 3 to 0.50)
walksp
 

Simon Gust

Member
Also, the draw GUI event
This is weird.
When the player is walking: hsp 0.50
When the shift instant and the bug is happening: hsp 10
When the player is walking and attacking: 0.50

(Also: i changed the player walskp from 3 to 0.50)
walksp
That is weird, but notice how you set speed to 10 in the gun object step event where you create a bullet. What if you remove that line?
That wouldn't make sense but it is worth a shot.
 
C

clyver

Guest
That is weird, but notice how you set speed to 10 in the gun object step event where you create a bullet. What if you remove that line?
That wouldn't make sense but it is worth a shot.
That's true. I noticed it too, and removed any 10 from gun object step. But it didn't helped in anything.
 
Top