M
Mattw11486
Guest
Hello all,
I need some help with Collision Masks. I created a sprite and converted it into a tileset. I then placed the mask around the borders of my map to create a collision mask.
My problem is when I move my Character (object) to test, it interacts with the mask but seems to act funny and gets stuck or it throws the Character to the opposite side of the bounding box. It is not interacting as it should.
I am not sure what I am missing here to get this to work flawlessly. Any help would be much appreciated.
Step Code:
I need some help with Collision Masks. I created a sprite and converted it into a tileset. I then placed the mask around the borders of my map to create a collision mask.
My problem is when I move my Character (object) to test, it interacts with the mask but seems to act funny and gets stuck or it throws the Character to the opposite side of the bounding box. It is not interacting as it should.
I am not sure what I am missing here to get this to work flawlessly. Any help would be much appreciated.
Code:
/// @description Insert description here
image_speed = 0; //How quickly the frames are played in the game, at 0 it wont' animate
walkSpeed = 3; //The variable controlling how quickly the player walks
//Tile map info
tilemap = layer_tilemap_get_id("collisionmask");
Code:
/// @description Insert description here
if(keyboard_check(ord("D"))) { //Move to the right
x += walkSpeed; //Physically moves the object the amount of walkSpeed to the right
image_speed = walkSpeed / 2; //Moves through the frames of the playing sprite
sprite_index = sprDMWalkRight; //Assigns the walking right sprite when D is pressed
}
if(keyboard_check(ord("A"))) { //Move to the left
x -= walkSpeed;
image_speed = walkSpeed / 2;
sprite_index = sprDMWalkLeft;
}
if(keyboard_check(ord("W"))) { //Move Up
y -= walkSpeed;
image_speed = walkSpeed / 2;
sprite_index = sprDMWalkUp;
}
if(keyboard_check(ord("S"))) { //Move Down
y += walkSpeed;
image_speed = walkSpeed / 2;
sprite_index = sprDMWalkDown;
}
if(keyboard_check(vk_shift)) { //Run faster
walkSpeed = 3; //Doubles the walk speed into a running speed
}
if(keyboard_check(vk_nokey)) { //Stop animating
image_speed = 0; //Set speed of playing frames to 0
image_index = 0; //Set current frame to 0
walkSpeed = 2; //Return speed to normal
}
//Collision Checks
//Horizontal Collision
var bbox_side;
if (walkSpeed > 0) bbox_side = bbox_right; else bbox_side = bbox_left;
if (tilemap_get_at_pixel(tilemap,bbox_side+walkSpeed,bbox_top) != 0) or (tilemap_get_at_pixel(tilemap,bbox_side+walkSpeed,bbox_bottom) != 0)
{
if (walkSpeed > 0) x = x - (x mod 32) + 31 - (bbox_right - x);
else x = x - (x mod 32) - (bbox_left - x);
walkSpeed = 0;
}
//Vertical Collision
if (walkSpeed > 0) bbox_side = bbox_bottom; else bbox_side = bbox_top;
if (tilemap_get_at_pixel(tilemap,bbox_left,bbox_side+walkSpeed) != 0) or (tilemap_get_at_pixel(tilemap,bbox_right,bbox_side+walkSpeed) != 0)
{
if (walkSpeed > 0) y = y - (y mod 32) + 31 - (bbox_bottom - y);
else y = y - (y mod 32) - (bbox_top - y);
walkSpeed = 0;
}