GameMaker: Studio

GameMaker: Studio

통계 보기:
regardtvisagie 2013년 9월 8일 오전 2시 33분
Movement towards a point on a grid
I want to move certain objects towards my player object. I know for this I can use the following code: move_towards_point(obj_player.x,obj_player.y,speed). However, what code can I use to make the objects move along a grid, for instance like a chess board (or the game Civilization if you will) towards my player object? I have no idea where to even begin with this...
< >
전체 댓글 6개 중 1~6개 표시 중
Blyxx 2013년 9월 8일 오전 3시 20분 
For each "step" your object takes (basically each time you want to move it one square closer), check the difference between object.x and player.x, and the difference between object.y and player.y. If the x difference is greater, move the object horizontally. If the y distance is greater, move it vertically. If they are the same, choose one at random.

Something like:

xDiff = abs(obj.x - player.x);
yDiff = abs(obj.y - player.y);

if xDiff > yDiff
{
if obj.x > player.x
{
obj.x -= 32; //for a grid with 32x32 squares
}
else
{
obj.x += 32;
}
if yDiff > xDiff
.....same stuff but for y movement.....

if xDiff == yDiff
{
var i = irandom(1);
if i > 0
......conditionals to figure out whether to move left or right.....
else if i < 1
......conditionals to figure out whether to move up or down.....
Scarabian 2013년 9월 8일 오전 4시 22분 
The best way in my opinion would be to make all of your objects have their own grid co-ordinates.Then all of your calculations would be against the grid co-ordinates. Then claculate the drawing (x,y) postion from the grid. So for example you want an object to start on grid (6,3). I would make a script called scr_calc_pos which could be called rom any object on the grid code:

object create event
grid_x = 6 grid_y = 3
in scr_calc_pos
x = argument0 * 32 y=argument1 * 32

then its just a matter of working with the grid co-ordinates. in the object step event this is where the code differs for the player object as it is moved by the user rather than the game. But for your other objects:
x_steps = abs(grid_x - player.grid_x); y_steps = abs(grid_y - player.grid_y); if (x_steps>0 or y_steps > 0) { if (x_steps > y_steps or x_steps = y_steps) //if x is further away or the same { if grid_x < player.grid_x //if obj is left of player { grid_x++ } else { grid_x-- } } else if (grid_y < player.grid_y)// if obj is above player { grid_y++ } else { grid_y-- } } scr_calc_pos(grid_x,grid_y) // calculate the x,y position of the object
To use this method you will have to make sure that all of your sprites have the same origin (0,0)
hope this helps,
Steve.
Scarabian 님이 마지막으로 수정; 2013년 9월 8일 오전 4시 26분
regardtvisagie 2013년 9월 8일 오전 7시 43분 
Thanks for replying. Scarabian, please explain "x = argument0 * 32". I'm new to gml and don't understand what the code means.
regardtvisagie 2013년 9월 8일 오전 7시 46분 
Also what does 'grid_y++' do?
Scarabian 2013년 9월 8일 오전 11시 12분 
When you use a script which is not attached to an object you can "pass" variables to it Other wise known as arguments GameMaker gives arguments that you pass the names argument0 argument1 up to argument15 in order. In this case we are passing grid_x and grid_y the two grid co-ordinates. ++ is merely a quick way of incrementing an integer by 1. I think we have discussed this before regardt if you enter operator into help contents or ++ the answers in the help file are more than adequate for either

Hope this helps.
Steve
regardtvisagie 2013년 9월 8일 오후 11시 33분 
Thanks Scarab! I do make use of the help contents quite often, I'm just not always sure if the code is part of the program or user defined. I don't want to come across as lazy. Thanks again. Regardt
< >
전체 댓글 6개 중 1~6개 표시 중
페이지당 표시 개수: 1530 50

게시된 날짜: 2013년 9월 8일 오전 2시 33분
게시글: 6