Now there's 2 set's of variables you need to know about.
- tile_size_x/y
- tiles_to_move_x/y
Tile_size is the size of your players or npcs movement, you can set it to anything as long as it's a dividend of 2. Now tiles_to_move is a variable that controls HOW many tiles the npc can move or if you having him like walk 3 squares to the left wait X(0.6 mili) then make the npc walk right. You won't have to modify this variable at all! you just have to switch the direction of movement. Moving left is 2, and right is 3 but that's the only difference in this script.
Now here's the script itself with an example object.
Creation Event:
CODE
//=========================//
// Author: Rukiri
// 10/29/10
// Completed: 10/30/10
// Updated 1/26/11
//========================//
// dir 4 or 8
dir_4 = true; // set to false if you want diagonal movements
// can this object move?
can_move=true
//Movement speed
move_speed = 2;
image_speed=0;
// Tile Size
tile_size_x = 2;
tile_size_y = 2;
// Tile X/Y
tile_x = x/tile_size_x;
tile_y = y/tile_size_y;
tile_x_old = xprevious/tile_size_x;
tile_y_old = yprevious/tile_size_y;
// Tiles to move x/y set to 0 for creation Down and Right
tiles_to_move_x = 0;
tiles_to_move_y = 0;
// Tiles to move x/y set to 0 for creation Up and Left
tiles_to_move_x_l = 0;
tiles_to_move_y_u = 0;
// the below code does not need to be placed anywhere, this only initializes the variables and is tracked in the movement script
// destination tile x/y right or down
dest_tile_x = tile_x_old + tiles_to_move_x;
dest_tile_y = tile_y_old + tiles_to_move_y;
// destination tile x/y left or up
dest_tile_x = tile_x_old - tiles_to_move_x;
dest_tile_y = tile_y_old - tiles_to_move_y;
// stop cut_scene movement, only use if your using scr_move_step without keyboard checks. Use an alarm for delays in
// switching directions.
stop_cutscene_move = false;
// move
move_down = 1;
move_left = 2;
move_right = 3;
move_up = 4;
// Author: Rukiri
// 10/29/10
// Completed: 10/30/10
// Updated 1/26/11
//========================//
// dir 4 or 8
dir_4 = true; // set to false if you want diagonal movements
// can this object move?
can_move=true
//Movement speed
move_speed = 2;
image_speed=0;
// Tile Size
tile_size_x = 2;
tile_size_y = 2;
// Tile X/Y
tile_x = x/tile_size_x;
tile_y = y/tile_size_y;
tile_x_old = xprevious/tile_size_x;
tile_y_old = yprevious/tile_size_y;
// Tiles to move x/y set to 0 for creation Down and Right
tiles_to_move_x = 0;
tiles_to_move_y = 0;
// Tiles to move x/y set to 0 for creation Up and Left
tiles_to_move_x_l = 0;
tiles_to_move_y_u = 0;
// the below code does not need to be placed anywhere, this only initializes the variables and is tracked in the movement script
// destination tile x/y right or down
dest_tile_x = tile_x_old + tiles_to_move_x;
dest_tile_y = tile_y_old + tiles_to_move_y;
// destination tile x/y left or up
dest_tile_x = tile_x_old - tiles_to_move_x;
dest_tile_y = tile_y_old - tiles_to_move_y;
// stop cut_scene movement, only use if your using scr_move_step without keyboard checks. Use an alarm for delays in
// switching directions.
stop_cutscene_move = false;
// move
move_down = 1;
move_left = 2;
move_right = 3;
move_up = 4;
Step Event:
CODE
// Get the tile ID number y/gridsize
if can_move = true or stop_cutscene_move = false { // checks to see if we're moving if not, x/y will not be added or subtracted from.
if (tile_x * tile_size_x > x) { // checks if tile_x is greater than x
x += move_speed; // moves your object to the right
} else if (tile_x * tile_size_x < x) { // checks if tile_x is less than x
x -= move_speed; // moves your object to the left
}
if (tile_y * tile_size_y > y) { // checks if tile_y is greater than y
y += move_speed; // moves your object down
} else if (tile_y * tile_size_y < y) { // checks if tile_y is less than y
y -= move_speed; // moves your object up
}
}
// Movement Code Block
if stop_cutscene_move = false { // set stop_cutscene to false when move_step has not been used
if (x = tile_x * tile_size_x and y = tile_y * tile_size_y) { // check to see if you're at the destination tile
// Place all your movements for this NPC here.
tiles_to_move_y = 28;
scr_move_step(1);
}
}
else { image_speed = 0; }
// make sure an NPC doesn't walk past it's destination tile
// Down & Right
if tiles_to_move_x >= 1 { if dest_tile_x = tile_x { stop_cutscene_move = true; } }
if tiles_to_move_y >= 1 { if dest_tile_y == tile_y { stop_cutscene_move = true;} }
// Up & Left
if tiles_to_move_x >= 1 { if dest_tile_x = tile_x { stop_cutscene_move = true; } }
if tiles_to_move_y >= 1 { if dest_tile_y == tile_y { stop_cutscene_move = true; } }
// stop event at the right tile.
if (tile_x * tile_size_x = x and tile_y * tile_size_y = y) { can_move = false; /* set can_move to false when we're not moving */ }
else { can_move = true } // set can_move to true when we are moving
if can_move = true or stop_cutscene_move = false { // checks to see if we're moving if not, x/y will not be added or subtracted from.
if (tile_x * tile_size_x > x) { // checks if tile_x is greater than x
x += move_speed; // moves your object to the right
} else if (tile_x * tile_size_x < x) { // checks if tile_x is less than x
x -= move_speed; // moves your object to the left
}
if (tile_y * tile_size_y > y) { // checks if tile_y is greater than y
y += move_speed; // moves your object down
} else if (tile_y * tile_size_y < y) { // checks if tile_y is less than y
y -= move_speed; // moves your object up
}
}
// Movement Code Block
if stop_cutscene_move = false { // set stop_cutscene to false when move_step has not been used
if (x = tile_x * tile_size_x and y = tile_y * tile_size_y) { // check to see if you're at the destination tile
// Place all your movements for this NPC here.
tiles_to_move_y = 28;
scr_move_step(1);
}
}
else { image_speed = 0; }
// make sure an NPC doesn't walk past it's destination tile
// Down & Right
if tiles_to_move_x >= 1 { if dest_tile_x = tile_x { stop_cutscene_move = true; } }
if tiles_to_move_y >= 1 { if dest_tile_y == tile_y { stop_cutscene_move = true;} }
// Up & Left
if tiles_to_move_x >= 1 { if dest_tile_x = tile_x { stop_cutscene_move = true; } }
if tiles_to_move_y >= 1 { if dest_tile_y == tile_y { stop_cutscene_move = true; } }
// stop event at the right tile.
if (tile_x * tile_size_x = x and tile_y * tile_size_y = y) { can_move = false; /* set can_move to false when we're not moving */ }
else { can_move = true } // set can_move to true when we are moving
For player movement remove the movement block and everything below it, this is where you code in directions, keyboard_checks, and how his/her sprite is handles.
but you still use scr_move_steps as your movement script. The movement block and everything below that are just for your NPCs. Your AI's you'll have to code differently.
This just shows you how NPC movement would be handled^^
scr_move_steps
CODE
// Moves the character based on tile position in direction argument0
// This set of conditionals is strictly for Movement!
/*
move_up = 4
move_down = 1
move_left = 2
move_right = 3
*/
// example, scr_move_step(1,5) will move up 5 tiles.
if (move_up = argument0)
{
dest_tile_y = tile_y_old - tiles_to_move_y;
if (!place_meeting(tile_x *32, (tile_y - 1) * 32, obj_wall))
tile_y -= 1;
}
else if (move_down = argument0)
{
dest_tile_y = tile_y_old + tiles_to_move_y;
if (!place_meeting(tile_x * 32, (tile_y + 1) * 32, obj_wall))
tile_y += 1;
}
else if (move_left = argument0)
{
dest_tile_x = tile_x_old - tiles_to_move_x;
if (!place_meeting((tile_x - 1) * 32, tile_y * 32, obj_wall))
tile_x -= 1;
}
else if (move_right = argument0)
{
dest_tile_x = tile_x_old + tiles_to_move_x;
if (!place_meeting((tile_x + 1) * 32, tile_y * 32, obj_wall))
tile_x += 1;
}
// usage
// scr_move_step(argument0)
// argument 0 is for direction
// This set of conditionals is strictly for Movement!
/*
move_up = 4
move_down = 1
move_left = 2
move_right = 3
*/
// example, scr_move_step(1,5) will move up 5 tiles.
if (move_up = argument0)
{
dest_tile_y = tile_y_old - tiles_to_move_y;
if (!place_meeting(tile_x *32, (tile_y - 1) * 32, obj_wall))
tile_y -= 1;
}
else if (move_down = argument0)
{
dest_tile_y = tile_y_old + tiles_to_move_y;
if (!place_meeting(tile_x * 32, (tile_y + 1) * 32, obj_wall))
tile_y += 1;
}
else if (move_left = argument0)
{
dest_tile_x = tile_x_old - tiles_to_move_x;
if (!place_meeting((tile_x - 1) * 32, tile_y * 32, obj_wall))
tile_x -= 1;
}
else if (move_right = argument0)
{
dest_tile_x = tile_x_old + tiles_to_move_x;
if (!place_meeting((tile_x + 1) * 32, tile_y * 32, obj_wall))
tile_x += 1;
}
// usage
// scr_move_step(argument0)
// argument 0 is for direction
Enjoy the script!