Changing the values in variables

M

Marko Kajtazi

Guest
Hi everyone,
I have a problem with my variables. When I create a new instance for my bullet in the bullet object I have.

Code:
//create event
movespeed = 16;
grav = 0;
bounce_force = 5;
dir = oPlayer.image_xscale;
hspd = dir * 4;
vspd = 0;
image_xscale = oPlayer.image_xscale;

// step event 
hspd += dir * 4;
hspd = clamp(hspd, -movespeed, movespeed);
vspd += grav;
if(place_meeting(x + hspd, y, oCollision)) {
    while (!place_meeting(x + sign(hspd), y, oCollision)) {
        x += sign(hspd);
    }
    hspd = 0;
}

x += hspd;
The problem is when the bullet collides with oCollision and when I am setting the hspd to 0, I want hspd to remain to be 0. But in my game if the bullet stops colliding on the x with the wall it continues to go to the other wall.
Can anyone help me?
Thanks.
 

flerpyderp

Member
Code:
hspd += dir * 4;
This line does not simply disappear when a collision happens, so even though you've set hspd to 0 on collision, you set it back to "hspd += dir * 4" immediately after.
 
M

Marko Kajtazi

Guest
Code:
hspd += dir * 4;
This line does not simply disappear when a collision happens, so even though you've set hspd to 0 on collision, you set it back to "hspd += dir * 4" immediately after.
Thanks for writing back,
Right, I get why it is doing that but I can't find a way to change it
 

Slyddar

Member
Don't you want to destroy the bullet if it collides with the wall or ... oCollision, which I assume is the wall object? Also what do you want to happen to it after the collision occurs?
 
M

Marko Kajtazi

Guest
Don't you want to destroy the bullet if it collides with the wall or ... oCollision, which I assume is the wall object? Also what do you want to happen to it after the collision occurs?
When the bullet collides with the wall I want it to bounce back a little and fall to the the ground so that the player can pick it up again. But why does in this video wotk like that.
Meybe because he sets the hsp to a fixed variable.
 
Last edited by a moderator:

Slyddar

Member
Well after it hits the wall, you then want to control the movement in a different way. One way to do this would be to assign the bullet stages. Stage 0 is when it's fired, stage 1 is when it's hit a wall. You could then give it different movement commands depending on it's stage. Assign stage = 0 in create and after the collision happens do stage = 1.
In your step put the existing firing code in a switch statement as stage 0, and in stage 1 the bounce off wall code, then in stage 2 code to allow the player to pick it up. Something like this...
Code:
switch(stage)
{
case 0:
  //firing
  hspd += dir * 4;
  hspd = clamp(hspd, -movespeed, movespeed);
  vspd += grav;
  if(place_meeting(x + hspd, y, oCollision)) {
     while (!place_meeting(x + sign(hspd), y, oCollision)) {
         x += sign(hspd);
     }
     hspd = 0;
     stage = 1;
     hspd = 5;  //how far it bounces off wall
     dir *= -1;  //reverse dir
  }
  x += hspd;
break;

case 1:
  //hit wall
  hspd *= 0.95;  //slow bullet down;
  x += hspd * dir;
  vspd += grav;
  if abs(hspd) < 0.05 stage = 2;  //allow bullet to be picked up
  //vertical collision code goes here so bullet can slide on the ground.  You can add that.
break;

case 2:
  //player can pick up bullet
  //any code required for picking up goes here.
break;
}
That's only one way, there are many other methods. You could even change the object to another object after it collides.

Just play around with it and see how you go. Following Shaun's tutorial's is a really good start though. He has a GMS2 one that addresses bullet firing, and shells after the shot, so maybe look into that.
 
M

Marko Kajtazi

Guest
Well after it hits the wall, you then want to control the movement in a different way. One way to do this would be to assign the bullet stages. Stage 0 is when it's fired, stage 1 is when it's hit a wall. You could then give it different movement commands depending on it's stage. Assign stage = 0 in create and after the collision happens do stage = 1.
In your step put the existing firing code in a switch statement as stage 0, and in stage 1 the bounce off wall code, then in stage 2 code to allow the player to pick it up. Something like this...
Code:
switch(stage)
{
case 0:
  //firing
  hspd += dir * 4;
  hspd = clamp(hspd, -movespeed, movespeed);
  vspd += grav;
  if(place_meeting(x + hspd, y, oCollision)) {
     while (!place_meeting(x + sign(hspd), y, oCollision)) {
         x += sign(hspd);
     }
     hspd = 0;
     stage = 1;
     hspd = 5;  //how far it bounces off wall
     dir *= -1;  //reverse dir
  }
  x += hspd;
break;

case 1:
  //hit wall
  hspd *= 0.95;  //slow bullet down;
  x += hspd * dir;
  vspd += grav;
  if abs(hspd) < 0.05 stage = 2;  //allow bullet to be picked up
  //vertical collision code goes here so bullet can slide on the ground.  You can add that.
break;

case 2:
  //player can pick up bullet
  //any code required for picking up goes here.
break;
}
That's only one way, there are many other methods. You could even change the object to another object after it collides.

Just play around with it and see how you go. Following Shaun's tutorial's is a really good start though. He has a GMS2 one that addresses bullet firing, and shells after the shot, so maybe look into that.
yeah, that actually worked very well. Thank you.
 
M

Marko Kajtazi

Guest
Well after it hits the wall, you then want to control the movement in a different way. One way to do this would be to assign the bullet stages. Stage 0 is when it's fired, stage 1 is when it's hit a wall. You could then give it different movement commands depending on it's stage. Assign stage = 0 in create and after the collision happens do stage = 1.
In your step put the existing firing code in a switch statement as stage 0, and in stage 1 the bounce off wall code, then in stage 2 code to allow the player to pick it up. Something like this...
Code:
switch(stage)
{
case 0:
  //firing
  hspd += dir * 4;
  hspd = clamp(hspd, -movespeed, movespeed);
  vspd += grav;
  if(place_meeting(x + hspd, y, oCollision)) {
     while (!place_meeting(x + sign(hspd), y, oCollision)) {
         x += sign(hspd);
     }
     hspd = 0;
     stage = 1;
     hspd = 5;  //how far it bounces off wall
     dir *= -1;  //reverse dir
  }
  x += hspd;
break;

case 1:
  //hit wall
  hspd *= 0.95;  //slow bullet down;
  x += hspd * dir;
  vspd += grav;
  if abs(hspd) < 0.05 stage = 2;  //allow bullet to be picked up
  //vertical collision code goes here so bullet can slide on the ground.  You can add that.
break;

case 2:
  //player can pick up bullet
  //any code required for picking up goes here.
break;
}
That's only one way, there are many other methods. You could even change the object to another object after it collides.

Just play around with it and see how you go. Following Shaun's tutorial's is a really good start though. He has a GMS2 one that addresses bullet firing, and shells after the shot, so maybe look into that.
Hey, the code worked, but I changed it a little bit and now it doesn't work even unchanged, here is what I changed.
Code:
switch(stage)
{
    case 0: {
        hspd += dir * 4;
        hspd = clamp(hspd, -movespeed, movespeed);
     
        if(place_meeting(x + hspd, y, oCollision)) {
            while (!place_meeting(x + sign(hspd), y, oCollision)) {
                x += sign(hspd);
            }
            stage = 1;
            hspd = bounce_force;
            dir *= -1;
        }
    break;
    }
 
    case 1: {
        x += dir * hspd;
        vspd += grav;
     
        if abs(hspd) < 0.05 stage = 2;
     
        if(place_meeting(x + vspd, y, oCollision)) {
            while (!place_meeting(x, y + sign(vspd), oCollision)) {
                y += sign(vspd);
            }
            vspd = 0;
            hspd = 0;
        }
     
     
    break;
    }
 
    case 2: {
        if(place_meeting(x, y, oPlayer)) {
            instance_destroy();
            oPlayer.hp += 1;
        }
    break;
    }
}
x += hspd;
y += vspd;
it works on the left, it has a nice bounce but no very good collision whit the ground. On the right there is no bounce and no smooth falling, but good collision.
Can you see the problem?.
Can you help me?.
 
Last edited by a moderator:

Relic

Member
At the end of your code you have x+=hspd
. This is outside the switch statement and so runs every step. You may have found a collision when stage was 0, adjusting x pixel by pixel until it perfectly collided, but then you added hspd to x again after the switch statement
 
M

Marko Kajtazi

Guest
At the end of your code you have x+=hspd
. This is outside the switch statement and so runs every step. You may have found a collision when stage was 0, adjusting x pixel by pixel until it perfectly collided, but then you added hspd to x again after the switch statement
I changed it and it is still the same.
 

Relic

Member
In case 1:

if(place_meeting(x + vspd, y

You probably mean for vspd to be on the y coordinate.
 
Last edited:
Top