Speed Increase scripting

T

Treecase86

Guest
Hey! In my game, I want my character to steadily increase in speed the longer you hold the movement key, currently, I'm using code that is quite long, and it doesn't work all that well. Can you please improve upon it? 'objSolid' is the ground. I don't have a running animation yet, either.

In a create event:
Code:
acc=0.1;
global.vel=0;
maxSpeed=9;
ground=true
In a step event:
Code:
if keyboard_check(vk_left) && !place_meeting(x+(abs(global.vel)*-1)-1, y, objSolid)
{
    global.vel -= acc * 2;
    image_xscale = -1;
}
if keyboard_check(vk_right) && !place_meeting(x+abs(global.vel)+1, y, objSolid)
{
    global.vel += acc * 1;
    image_xscale = 1;
}if (global.vel > 0 && place_meeting(x+abs(global.vel)+1, y, objSolid)) or  (global.vel < 0 && place_meeting(x+(abs(global.vel)*-1)-1, y, objSolid))
    global.vel = 0;
 
//Speed limit
if global.vel > maxSpeed
    global.vel = maxSpeed;
else if global.vel < -maxSpeed
    global.vel = -maxSpeed;
 
if global.vel > -acc && global.vel < acc
    global.vel = 0;
 
x +=global.vel//Gravity
if place_meeting(x, y+vspeed+1, objSolid)
{
   ground = true;
   gravity = 0;
}
else
{
   ground = false;
   gravity = 0.25;
   if vspeed > 8
       vspeed = 8;
}//Handle sprites
if ground == true
{
    if global.vel == 0
        sprite_index = playerR;
    else if global.vel > -8 && global.vel < 8
        sprite_index = playerR;
    else
         sprite_index = playerR;
       
     image_speed = abs(global.vel / 20);
}
In a collision event:
Code:
move_contact_solid(direction,12);
vspeed=0;
 
Last edited by a moderator:

jo-thijs

Member
Sure can do, but I have a couple of questions first.

Why does the player accelerate by acc * 2 to the left, but only by acc to the right?

Do objSolid objects have certain properties as being rectangular or being aligned to a grid of some specific size and offset?

And where do you currently deal with animations?
Or don't you have any animations yet?
 
T

Treecase86

Guest
Sure can do, but I have a couple of questions first.

Why does the player accelerate by acc * 2 to the left, but only by acc to the right?

Do objSolid objects have certain properties as being rectangular or being aligned to a grid of some specific size and offset?

And where do you currently deal with animations?
Or don't you have any animations yet?
1. Must be an error, should be the same acc.
2.objSolid is a rectangular object
3.I don't have any animations yet, almost made some, though
 

jo-thijs

Member
How about this?

Create event:
Code:
acc = 0.1;
hvel = 0;
vvel = 0;
maxSpeed = 9;
img_xscl = 1
Step event:
Code:
// Horizontal input
var horInput = keyboard_check(vk_right) - keyboard_check(vk_left);

// Facing direction
if horInput != 0
    img_xscl = horInput;

// New horizontal speed
hvel = clamp(hvel + acc * horInput, -maxSpeed, maxSpeed);

if abs(hvel) < acc
    hvel = 0;

// Horizontal movement and collision
x += hvel;
var d = sign(hvel);
var b = true;
while b {
    b = false;
    with instance_place(x, y, objSolid) {
        if d // Moving right and collide
            other.x += bbox_left - other.bbox_right - 1;
        else // Moving left and collide
            other.x += bbox_right + 1 - other.bbox_left;
        other.hvel = 0;
        b = true;
    }
}

//Gravity
vvel = min(vvel + 0.25, 8);

// Vertical movement and collision
y += vvel;
var d = sign(vvel);
var b = true;
while b {
    b = false;
    with instance_place(x, y, objSolid) {
        if d // Moving down and collide
            other.y += bbox_top - other.bbox_bottom - 1;
        else // Moving up and collide
            other.y += bbox_bottom + 1 - other.bbox_top;
        other.vvel = 0;
        b = true;
    }
}

//Handle sprites
if place_meeting(x, y + 1, objSolid) {
    if hvel == 0
        sprite_index = playerR;
    else if abs(global.vel) < 8
        sprite_index = playerR;
    else
        sprite_index = playerR;
   
    image_speed = abs(hvel / 20);
}
Draw event:
Code:
image_xscale = img_xscl;
draw_self();
image_xscale = 1;
No collision event and no solid objects (make objSolid non-solid, it might save trouble in the future).

You could also best give the player a static mask (not dependent of the player's sprite or image index).
 
I

icuurd12b42

Guest
code shortcuts

basic left right control detect
var hdir = keyboard_check(vk_right) - keyborad_check(vk_left); //give -1,0,1;

basic left right applying to hspeed
hspeed = hdir*4; //-1 * 4 or 0 * 4 or 1 * 4 = -4 or 0 or 4

or acceleration
hspeed+=hdir*.01;

xscaling
if(hdir!=0) image_xscale = hdir;

basic clamping of speed to +-8
hspeed = clamp(hspeed,-8,8);

simplified friction using damping
hspeed *= 0.9;
 
T

Treecase86

Guest
How about this?

Create event:
Code:
acc = 0.1;
hvel = 0;
vvel = 0;
maxSpeed = 9;
img_xscl = 1
Step event:
Code:
// Horizontal input
var horInput = keyboard_check(vk_right) - keyboard_check(vk_left);

// Facing direction
if horInput != 0
    img_xscl = horInput;

// New horizontal speed
hvel = clamp(hvel + acc * horInput, -maxSpeed, maxSpeed);

if abs(hvel) < acc
    hvel = 0;

// Horizontal movement and collision
x += hvel;
var d = sign(hvel);
var b = true;
while b {
    b = false;
    with instance_place(x, y, objSolid) {
        if d // Moving right and collide
            other.x += bbox_left - other.bbox_right - 1;
        else // Moving left and collide
            other.x += bbox_right + 1 - other.bbox_left;
        other.hvel = 0;
        b = true;
    }
}

//Gravity
vvel = min(vvel + 0.25, 8);

// Vertical movement and collision
y += vvel;
var d = sign(vvel);
var b = true;
while b {
    b = false;
    with instance_place(x, y, objSolid) {
        if d // Moving down and collide
            other.y += bbox_top - other.bbox_bottom - 1;
        else // Moving up and collide
            other.y += bbox_bottom + 1 - other.bbox_top;
        other.vvel = 0;
        b = true;
    }
}

//Handle sprites
if place_meeting(x, y + 1, objSolid) {
    if hvel == 0
        sprite_index = playerR;
    else if abs(global.vel) < 8
        sprite_index = playerR;
    else
        sprite_index = playerR;

    image_speed = abs(hvel / 20);
}
Draw event:
Code:
image_xscale = img_xscl;
draw_self();
image_xscale = 1;
No collision event and no solid objects (make objSolid non-solid, it might save trouble in the future).

You could also best give the player a static mask (not dependent of the player's sprite or image index).
I'm assuming it works, but I can't see my character on-screen.
 

jo-thijs

Member
No, I'm not sure.
Is the draw event executed?
What is the value of img_xscl?
What is the position of the player?

(use show_debug_message for this)
 
T

Treecase86

Guest
No, I'm not sure.
Is the draw event executed?
What is the value of img_xscl?
What is the position of the player?

(use show_debug_message for this)
stupid question,Where do I use show_debug_message?
 

jo-thijs

Member
In the draw event or step end event.
In the draw event will tell you if the draw event is being executed.
In the end step event you can ask for the values of the object.
 
T

Treecase86

Guest
In the draw event or step end event.
In the draw event will tell you if the draw event is being executed.
In the end step event you can ask for the values of the object.
It was supposed to be a step end event?
 
T

Treecase86

Guest
Doesn't matter that much, but I would suggest the end step event.
Everything is functioning right, no character, though. My character should be to the left (X:0, Y:320) of the street light.
 
Last edited by a moderator:

jo-thijs

Member
The issue is that you spawn the player object inside the wall.
My code then pushes the player outside of it horizontally,
resulting in a high x coordinate, which you must have missed with show_debug_message.

To fix this, just put the player at least 6 pixels higher, or the floor 6 pixels lower, or change bounding boxes.
 
T

Treecase86

Guest
The issue is that you spawn the player object inside the wall.
My code then pushes the player outside of it horizontally,
resulting in a high x coordinate, which you must have missed with show_debug_message.

To fix this, just put the player at least 6 pixels higher, or the floor 6 pixels lower, or change bounding boxes.
Thanks, after tons of messages, we finally sorted things out. Thanks much for your help!
 
Top