Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: [Solved] 16x16 Grid Movement  (Read 7449 times)

0 Members and 1 Guest are viewing this topic.

Ryuza

That one guy
[Solved] 16x16 Grid Movement
« on: January 29, 2010, 06:19:12 pm »
  • RyuKage2007
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 290
~Solved~

Well, time for another rather small problem. I need a bit of help with working grid based movement into my movement code (Game Maker 8), by grid based I mean in the same style as the older Dragon Quest games or Pokemon, where each step aligns with the grid (16x16). I can't seem to find an efficient way of doing it with my movement engine. If anyone has any ideas please let me know. Here is my movement code:

Create Event:
Code: [Select]
image_speed = .15
//Establishes Player's sprite speed

moving = "D"
//Player's current direction
//Used to choose the correct sprites as he moves
//"D" -> Down, "U" -> Up, "L" -> Left, "R" -> Right

Step Event:
Code: [Select]
/*
The following code is used to control Player's Movement
---------------------------------------------------------
It checks to see if the Player is currently pressing the Up,
Down, Left, or Right arrow keys, then checks for objects and
finally moves the Player in the direction if there are no obstacles.
*/
if keyboard_check(vk_up) {
    moving = "U"
    if place_free(x, y-1)
        y -= 2
}

if keyboard_check(vk_down) {
    moving = "D"
    if place_free(x, y+1)
        y += 2
}

if keyboard_check(vk_left) {
    moving = "L"
    if place_free(x-1, y)
        x -= 2
}

if keyboard_check(vk_right) {
    moving = "R"
    if place_free(x+1, y)
        x += 2
}

if keyboard_check(vk_nokey)
    {image_speed = .15}
/*
The following code is used to correct Player's Direction
---------------------------------------------------------
It checks to see if he is moving left, right, up, or down
and changes his 'moving' variable accordingly
*/
if keyboard_check(vk_up) && keyboard_check(vk_right) {
    moving = "U"
}

if keyboard_check(vk_up) && keyboard_check(vk_left) {
    moving = "U"
}

if x > xprevious
    {moving = "R"}
    
if x < xprevious
    {moving = "L"}

if y > yprevious
    {moving = "D"}
    
if y < yprevious
    {moving = "U"}

Begin Step Event:
Code: [Select]
//The following code determines which sprite should be used based on the players current direction (the "moving" variable)
switch moving {
    case "U": sprite_index = spr_hero_up; break;
    case "D": sprite_index = spr_hero_down; break;
    case "R": sprite_index = spr_hero_right; break;
    case "L": sprite_index = spr_hero_left; break;
}

I'm hoping to be able to integrate grid movement into the code without having to completely rewrite it.

Thanks in advance to anyone that can help me solve this problem.

~Solved~
« Last Edit: February 02, 2010, 05:43:09 pm by RyuKage2007 »
Logged
<- Koholint Island - MC Style  <- Link's Awakening Photo Recolors  <- Wind Waker 3D Resources <- Super Mario Bros Crossover
  • RyuKage2007's Youtube

DJvenom

super-sage
Re: [Request] 16x16 Grid Movement
« Reply #1 on: January 29, 2010, 09:25:23 pm »
  • Colbydude was here.
  • *
  • Reputation: +7/-0
  • Offline Offline
  • Gender: Female
  • Posts: 2898
When I get home tnight I'll send you what I had programmed for my pokemon crystal game :) has 16x16 grid movement
Logged
I do art
I ermmm... DID do art
I do art

Kingknight

SiMelon
Re: [Request] 16x16 Grid Movement
« Reply #2 on: January 29, 2010, 10:13:24 pm »
  • The Hunter
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 495
Isn't it just " Align to Grid" or
Code: [Select]
move_snap(hsnap,vsnap);?
Logged
Re: [Request] 16x16 Grid Movement
« Reply #3 on: January 29, 2010, 10:27:25 pm »
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3725
In your step event you are checking for all for the arrow keys. Although in the end they cancel each other out if you press opposite sides, but you do not make corrections for it in case of collisions. Neither for diagonal movement by the way.

If you want some inside into grid based movement look at Theforeshadower's topic: http://www.zfgc.com/forum/index.php?topic=35732.0
Logged
Re: [Request] 16x16 Grid Movement
« Reply #4 on: January 30, 2010, 02:08:10 am »
  • *
  • Reputation: +8/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6604
For checking if you're on the grid, you can either use place_snapped(16,16) or you can check something like if (x mod 16 == 0 && y mod 16 == 0). The latter says that you are on an x and y position that is a multiple of 16 or where x == 0 or y == 0, which if you think about it is every grid-space.

I had noticed you looked at my Pokemon example earlier; which could still be improved considering all I have learned now about stuff - namely creating an objWall object which would represent things you could run into rather than initially doing a place_free check (assuming my code had that in it).

In the Pokemon movement at least, if you pressed both left/right or up/down, the character would move because one of those directions would just take primacy. The Gameboy control pad pretty much makes it so one can't press down keys like that though. Your key pressing for movement can thus be simplified to if, else if, and then an else statement for when no keys are pressed. Also don't know why you're doing sprite changing in the Begin Step: it won't reflect accurately what sprite you are when you move. Also about having a facing variable, use the built-in variable direction; it's more efficient.

The xprevious < x and similar checks for direction aren't really needed either if you set the direction correctly as the direction can only be set when you are on a grid-space, you shouldn't be able to change it when between them. Additionally that part about where you check if you're pressing up and left and then correcting the direction would also be unneeded if you coded the key movement correctly as I mentioned above.

I'd recommend looking at my Pokemon walking stuff again. If you want it to be more general but don't know how to make it so I could help you out with that too.
Logged

Ryuza

That one guy
Re: [Request] 16x16 Grid Movement
« Reply #5 on: January 30, 2010, 02:41:33 am »
  • RyuKage2007
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 290
Thanks for all the help you guys, I think I'll go give it another shot and see what I can come up with.

Also, thanks 4Sword for the small evaluation of my move code, I just put it together a little while ago and was hoping someone would post some ideas on improving it, I haven't tried doing my own move code in awhile as I usually used Goodnight's when overhead movement was needed. I think I'll rework it with some of your ideas before I start working on implementing all of the grid stuff.
About the sprite correction in the begin step, I had put it there to see if it made any difference and ended up leaving it there since I didn't think it made too much of a difference.
« Last Edit: January 30, 2010, 02:53:01 am by RyuKage2007 »
Logged
<- Koholint Island - MC Style  <- Link's Awakening Photo Recolors  <- Wind Waker 3D Resources <- Super Mario Bros Crossover
  • RyuKage2007's Youtube

DJvenom

super-sage
Re: [Request] 16x16 Grid Movement
« Reply #6 on: January 30, 2010, 04:13:30 am »
  • Colbydude was here.
  • *
  • Reputation: +7/-0
  • Offline Offline
  • Gender: Female
  • Posts: 2898
http://www.yoroshii.org/pokemoncrystal.gm6

Ooh~ in-tact day/night system too!?
« Last Edit: January 30, 2010, 04:33:12 am by DJvenom »
Logged
I do art
I ermmm... DID do art
I do art

Ryuza

That one guy
Re: [Request] 16x16 Grid Movement
« Reply #7 on: February 02, 2010, 05:42:12 pm »
  • RyuKage2007
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 290
Well, got it working finally, thanks for the help you guys.
Logged
<- Koholint Island - MC Style  <- Link's Awakening Photo Recolors  <- Wind Waker 3D Resources <- Super Mario Bros Crossover
  • RyuKage2007's Youtube
Pages: [1]   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.019 seconds with 51 queries.