Player Movement on Tile Collisions

boy656

Member
I found a solution to jittering. Now I need help on player movement on the tiles. I got the player to switch to the jumpsprite when jumping and the player to switch back to the idle sprite when landing onto the tiles. But I cant figure out how to get the player to switch to the running sprite on the ground when i press the right or left arrow key. I would really appreciate some help, thank you!

Here is the code:

Create:

velocity_ = [0, 0];
gravity_ = 1.5;
jump_speed_ = 38;
max_velocity_ = [14, 32];
acceleration_ = 50.1;
// Get the tilemap id
var layer_id = layer_get_id("Collision");
collision_tile_map_id_ = layer_tilemap_get_id(layer_id);

Step:

velocity_[vector2_x] = clamp(velocity_[vector2_x]+x_input, -max_velocity_[vector2_x], max_velocity_[vector2_x]);
// Friction
if x_input == 0 {
velocity_[vector2_x] = lerp(velocity_[vector2_x], 0, .2);
}
// Gravity
velocity_[vector2_y] += gravity_;
// Move and contact tiles
move_and_contact_tiles(collision_tile_map_id_, 64, velocity_);
// Jumping
var on_ground = tile_collide_at_points(collision_tile_map_id_, [bbox_left, bbox_bottom], [bbox_right-1, bbox_bottom]);
if on_ground {
sprite_index = spriteIdle;
// Jumping
if keyboard_check_pressed(vk_up) {
velocity_[vector2_y] = -jump_speed_;
sprite_index = objectJump;
}
} else {
// Control jump height
if keyboard_check_released(vk_up) && velocity_[vector2_y] <= -(jump_speed_/3) {
velocity_[vector2_y] = -(jump_speed_/3);
}
}

Script move_and_contact_tiles:

//@param tile_map_id
///@param tile_size
///@param velocity_array
var tile_map_id = argument0;
var tile_size = argument1;
var velocity = argument2;
// For the velocity array
var vector2_x = 0;
var vector2_y = 1;
// Move horizontally
x += velocity[vector2_x];
// Right collisions
if velocity[vector2_x] > 0 {
var tile_right = tile_collide_at_points(tile_map_id, [bbox_right-1, bbox_top], [bbox_right-1, bbox_bottom-1]);
if tile_right {
x = bbox_right & ~(tile_size-1);
x -= bbox_right-x;
velocity[@ vector2_x] = 0;
}
} else {
var tile_left = tile_collide_at_points(tile_map_id, [bbox_left, bbox_top], [bbox_left, bbox_bottom-1]);
if tile_left {
x = bbox_left & ~(tile_size-1);
x += tile_size+x-bbox_left;
velocity[@ vector2_x] = 0;
}
}
// Move vertically
y += velocity[vector2_y];
// Vertical collisions
if velocity[vector2_y] > 0 {
var tile_bottom = tile_collide_at_points(tile_map_id, [bbox_left, bbox_bottom-1], [bbox_right-1, bbox_bottom-1]);
if tile_bottom {
y = bbox_bottom & ~(tile_size-1);
y -= bbox_bottom-y;
velocity[@ vector2_y] = 0;
}
} else {
var tile_top = tile_collide_at_points(tile_map_id, [bbox_left, bbox_top], [bbox_right-1, bbox_top]);
if tile_top {
y = bbox_top & ~(tile_size-1);
y += tile_size+y-bbox_top;
velocity[@ vector2_y] = 0;
}
}

Script tile_collide_at_points:

///@param tile_map_id
///@param point_arrays...
var tile_map_id = argument[0];
// Found variable
var found = false;
// for the point arrays
var vector2_x = 0;
var vector2_y = 1;
// Loop through the points and check for a tile
for (var i=1; i<argument_count; i++) {
var point = argument;
found = found || tilemap_get_at_pixel(tile_map_id, point[vector2_x], point[vector2_y]);
}
// return found
return found;



Thank you!
 
Top