A
AnimusRex
Guest
So, I'm following HeartBeast's tutorial for randomly generating a grid based level. Everything up until the movement has worked fine.
The way I'm having movement work is comparable to old-school Resident evil games; you turn left and right on the spot, and the up arrow key is what moves your character forward. As this is a tile-based Roguelike, there are 8 cardinal directions you can go.
With that explained, here is the code I have for "grid_place_meeting"
And here is the code I have for movement.
I'm not using snapping commands for the grid movement, but rather a timer that allows 32 pixels of movement time. The grid based movement works fine, until I try to enable the collision detection, whereupon I can move 1 pixel in any direction at a time.
I'm sure my math is wrong for this somewhere, and I'm dividing 32 by 32 and ending up with 1, but I can't for the life of me see where. Any help appreciated!
EDIT:
After looking at the code more and fiddling with the character sprites, I've determined that I'm moving 4 pixels per input of the up arrow, but no collision detection is taking place between the walls and the player character in any direction.
If I have simple movement, I can do collision detection. If I don't do collision detection, my 8 directional grid-based movement works fine.
I guess I'll try to put a collision check into the movement key itself rather than check the script, see if that does anything...
The way I'm having movement work is comparable to old-school Resident evil games; you turn left and right on the spot, and the up arrow key is what moves your character forward. As this is a tile-based Roguelike, there are 8 cardinal directions you can go.
With that explained, here is the code I have for "grid_place_meeting"
Code:
///Checking collisions
var xx = argument[0];
var yy = argument[1];
//Remember player position
var xp = x;
var yp = y;
//Update position for the mask calculations
x = xx;
y = yy;
//Check for an x meeting on the grid in either left or right according to the sprites bounding box
var x_meeting = (Level.grid[# bbox_right div CELL_WIDTH, bbox_top div CELL_HEIGHT] != FLOOR) ||
(Level.grid[# bbox_left div CELL_WIDTH, bbox_top div CELL_HEIGHT] != FLOOR);
//Check for a y meeting on the grid in either top or bottom according to the sprites bounding box
var y_meeting = (Level.grid[# bbox_right div CELL_WIDTH, bbox_bottom div CELL_HEIGHT] != FLOOR) ||
(Level.grid[# bbox_left div CELL_WIDTH, bbox_bottom div CELL_HEIGHT] != FLOOR);
//Move back if collided
x = xp;
y = yp;
//Return true or false
return x_meeting or y_meeting;
Code:
var speedx = argument [0];
var speedy = argument [1];
//Horizontal Collisions
if (grid_place_meeting(x+speedx, y)) {
while (!grid_place_meeting(x+sign(speedx), y)) {
x+= sign(speedx);
}
speedx = 0;
}
//Vertical Collisions
if (grid_place_meeting(x, y+speedy)) {
while (!grid_place_meeting(x, y+sign(speedy))) {
y+= sign(speedy);
}
speedy = 0;
}
//Setting up animation frames
if imageNumber == 1 {image_index = 1}
if imageNumber == 2 {image_index = 2}
if imageNumber == 3 {image_index = 3}
if imageNumber == 4 {image_index = 4}
if imageNumber == 5 {image_index = 5}
if imageNumber == 6 {image_index = 6}
if imageNumber == 7 {image_index = 7}
//Code for turning on the spot
if (ismoving == false)
{
if (keyboard_check_pressed(vk_right))
{
image_index += 1;
}
if (keyboard_check_pressed(vk_left))
{
image_index -= 1;
}
if (keyboard_check(vk_up))
{
if image_index = 0 //Moving North
{
ismoving = true;
movetimer = 32;
speedy = -movespeed;
speedx = 0;
}
if image_index = 1 //Moving North East
{
ismoving = true;
movetimer = 32;
speedy = -movespeed;
speedx = movespeed;
}
if image_index = 2 //Moving East
{
ismoving = true;
movetimer = 32;
speedy = 0;
speedx = movespeed;
}
if image_index = 3 //Moving South-East
{
ismoving = true;
movetimer = 32;
speedy = movespeed;
speedx = movespeed;
}
if image_index = 4 //Moving South
{
ismoving = true;
movetimer = 32;
speedy = movespeed;
speedx = 0;
}
if image_index = 5 //Moving South-West
{
ismoving = true;
movetimer = 32;
speedy = movespeed;
speedx = -movespeed;
}
if image_index = 6 //Moving West
{
ismoving = true;
movetimer = 32;
speedy = 0;
speedx = -movespeed;
}
if image_index = 7 //Moving North-West
{
ismoving = true;
movetimer = 32;
speedy = -movespeed;
speedx = -movespeed;
}
}
}
if (ismoving == true)
{
y += speedy;
x += speedx;
movetimer -= movespeed
if (movetimer == 0) ismoving = false;
}
I'm sure my math is wrong for this somewhere, and I'm dividing 32 by 32 and ending up with 1, but I can't for the life of me see where. Any help appreciated!
EDIT:
After looking at the code more and fiddling with the character sprites, I've determined that I'm moving 4 pixels per input of the up arrow, but no collision detection is taking place between the walls and the player character in any direction.
If I have simple movement, I can do collision detection. If I don't do collision detection, my 8 directional grid-based movement works fine.
I guess I'll try to put a collision check into the movement key itself rather than check the script, see if that does anything...
Last edited by a moderator: