Tile-Based Pokemon Style Movement

Learn to use DS Game Maker by Example.

Tile-Based Pokemon Style Movement

Postby speedsterx99 » October 12th, 2013, 11:40 pm

I am not great at explaining tutorials but this is for any newbies who want to see some working code and want to learn from it.

This tutorial shows how to make movement like that of the pokemon games where the player can only move on 32 pixel tiles . You can press B to move faster and cannot move when you hit an object that has been specified.

First, I'll describe the variables

Global variables:
canwalk = if the player can move (as only a yes or no answer is needed, the variable is a boolean or bool)
walking = is the player moving
direction = direction of movement clockwise on a d-pad. e.g. 1 = up, 2 = right...
animating = is the player being animated. Mainly used to decide if the player should start to be animated. If the animate action if used constantly on the step event, the object will stay on the same frame as the same frame is being started at every 1/60th of a second

local variables
canwalkleft/right/up/down = whether there is an object in a certain direction of the player.

Now for the code:

Code: Select all
If (!Object_Under_V_Line(Bottom_Screen, [Y], [Y] + 32, [X], Obj_Tree)) && (!Object_Under_V_Line(Bottom_Screen, [Y], [Y] + 32, [X], Obj_Mart))
   canmoveleft = 1
Else
   canmoveleft = 0
End If
If (!Object_Under_V_Line(Bottom_Screen, [Y], [Y] + 32, [X] + 32, Obj_Tree)) && (!Object_Under_V_Line(Bottom_Screen, [Y], [Y] + 32, [X] + 32, Obj_Mart))
   canmoveright = 1
Else
   canmoveright = 0
End If
If (!Object_Under_H_Line(Bottom_Screen, [X], [X] + 32, [Y], Obj_Tree)) && (!Object_Under_H_Line(Bottom_Screen, [X], [X] + 32, [Y], Obj_Mart))
   canmoveup = 1
Else
   canmoveup = 0
End If
If (!Object_Under_H_Line(Bottom_Screen, [X], [X] + 32, [Y] + 32, Obj_Tree)) && (!Object_Under_H_Line(Bottom_Screen, [X], [X] + 32, [Y] + 32, Obj_Mart))
   canmovedown = 1
Else
   canmovedown = 0
End If

This code decides if there is a object to any side of the player and prevents the object from moving in that direction if there is. By adding more '&&'s to the if statement, this can work with more objects. Just Obj_Tree and Obj_Mart are used in this case

Code: Select all
If Pad.Held.Left && canmoveleft = 1
  if canwalk = 1
    walking = 1
    canwalk = 0
    direction = 4
  End If
  if walking = 1 && direction = 4
    If Pad.Held.B != 1
      [X] = [X] - 1
    End If
    If Pad.Held.B
      [X] = [X] - 2
    End If
    if animating = 0
      if Pad.Held.B != 1
        Animate([Me], 9, 11, 6)
      Else
      Animate([Me], 9, 11, 12)
      End If
      animating = 1
    End If
  End If
End If
If direction = 4 && Pad.Held.Left != 1
  if (Is_Divisible([X],32))
    canwalk = 1
    walking = 0
    direction = 0
    animating = 0
    PA_SpriteAnimPause([Me], 0, 1)
    Set_Frame([Me], 9)
  Else
  [X] = [X] - 1
End If
End If


now, lets divide this code up into sections.

This part decides if the player is already moving and, if so, stops any further movement in a different direction. If the player is not moving, the player begins to move.
Code: Select all
If Pad.Held.Left && canmoveleft = 1
  if canwalk = 1
    walking = 1
    canwalk = 0
    direction = 4
  End If


Now, if the player is moving, this code decides how quickly the player should move. If the B button is held down, the player runs at twice the speed.

Code: Select all
if walking = 1 && direction = 4
    If Pad.Held.B != 1
      [X] = [X] - 1
    End If
    If Pad.Held.B
      [X] = [X] - 2
    End If


If the player is not already being animated, this starts the animation. If it is already animated, it prevents the animate action from being used again. Otherwise, the object would remain on a single frame. It also animates at different speeds if the player is running or not.

Code: Select all
if animating = 0
      if Pad.Held.B != 1
        Animate([Me], 9, 11, 6)
      Else
      Animate([Me], 9, 11, 12)
      End If
      animating = 1
    End If
  End If
End If


If the button is released, the player continues to move in that direction until it lands on a 32-pixel tile. Once it does land upon one of these tiles, movement is stopped and the code returns to how it was in the beginning.

Code: Select all
If direction = 4 && Pad.Held.Left != 1
  if (Is_Divisible([X],32))
    canwalk = 1
    walking = 0
    direction = 0
    animating = 0
    PA_SpriteAnimPause([Me], 0, 1)
    Set_Frame([Me], 9)
  Else
  [X] = [X] - 1
End If
End If


All this code is repeated 4 times, once for each direction.

Following this is the code that moves the camera with the player (means that the background and other objects seem to move when the player, in fact, moves)

Code: Select all
RoomData.BottomX = ([X] - 112)
RoomData.BottomY = ([Y] - 80)


This sets the camera to look directly at the center of the sprite.

Well, that's it for this tutorial. An .nds file has been attached

This may not be the most efficient way to do this. If anyone can come up with a better way, feel free to post it.
Attachments
pkmn movement 1.dsgm
(11.88 KiB) Downloaded 329 times
Three things that I know for sure:

    I am English
    I am 16 years old
    I am God

Projects

Pokemon-type game (70% complete; just need to add AI) will resume at later date

Hackulation http://dsgamemaker.com/dsgmforum/viewtopic.php?f=13&t=3439

Current: A new project that I will likely never finish and will never be able to create the AI for. A project bigger than anything that I have done before. A project that I know is impossible for me to ever complete. But screw reality. I'm doing it anyway.
User avatar
speedsterx99
 
Posts: 69
Joined: May 18th, 2011, 8:39 pm

Re: Tile-Based Pokemon Style Movement

Postby Buck_7 » October 15th, 2013, 5:01 pm

Nice, this is pretty good.
User avatar
Buck_7
 
Posts: 1460
Joined: February 19th, 2011, 1:44 am

Re: Tile-Based Pokemon Style Movement

Postby Foxi4 » October 18th, 2013, 11:54 pm

Very well-made. :P
Current Projects:

FalloutDS: Postponed until proper libraries are created, 10%
Foxi_Lib: A completely revised version of NitroSprites[FINAL], NitroBackgrounds and NitroText[FINAL], 40%
Foxi4
 
Posts: 1221
Joined: December 31st, 2010, 3:25 am

Re: Tile-Based Pokemon Style Movement

Postby boe6eod7nty » October 19th, 2013, 6:21 pm

does anyone care to say what I'd have to change to use a 16x16 sprite on a 16x16 grid? Also how do i make it so It doesn't walk of the map/background?
that akward moment when somthing akward happens.....
wait.....
what?
boe6eod7nty
 
Posts: 209
Joined: September 11th, 2012, 4:28 pm
Location: USA. WI.

Re: Tile-Based Pokemon Style Movement

Postby EgoTrip » October 19th, 2013, 7:13 pm

Simply change all the 32's to 16's. To stop it walking off the edge of the map then just insert:

[X]>0 // if [X]<240 (256-16) // if [Y]>0 // if [Y]<176 (192-16) into the appropriate line of code.

For example:

Code: Select all
If ([X]>0 && !Object_Under_V_Line(Bottom_Screen, [Y], [Y] + 32, [X], Obj_Tree)) && (!Object_Under_V_Line(Bottom_Screen, [Y], [Y] + 32, [X], Obj_Mart))
   canmoveleft = 1


But really if you are using this method, its better to use a collision map, where each pixel represents something such as a wall, or water or whatever, then you wont need to use the above code as there will be block pixels all around the edge of the map. Obviously you still want to check for collisions with dynamic objects though, they wont usually be represented on the collision map. I used this method in Jewel Warehouse.

Thats the point of this code though, its there as a basic template and you can expand on it with more features and techniques.
EgoTrip
 
Posts: 80
Joined: June 5th, 2012, 3:04 pm

Re: Tile-Based Pokemon Style Movement

Postby Allinardo » January 7th, 2015, 8:51 am

I tried changing the player from the bottom screen to the top screen, but now he's suspended in animation. I mean the movement and everything works, but now the animation never stops :/
User avatar
Allinardo
 
Posts: 33
Joined: January 7th, 2015, 3:41 am
Location: Los Angeles California

Re: Tile-Based Pokemon Style Movement

Postby Allinardo » January 19th, 2015, 8:27 pm

I tried transferring the script to the top screen, but for some reason the animation doesn't pause now. Any idea what this might be?
User avatar
Allinardo
 
Posts: 33
Joined: January 7th, 2015, 3:41 am
Location: Los Angeles California

Re: Tile-Based Pokemon Style Movement

Postby Nobody » January 19th, 2015, 10:15 pm

we can't tell what's wrong unless you upload your code. Do note that if you are using the above code, arguments that specify the use of the bottom screen must be changed to use the top screen if the code is to run properly. But include the DSGM file and we'll be able to fix the issue much eaiser
Feel free to ask me for help. I'm on the forum a lot.

Recent Projects:
Retro Ducks: viewtopic.php?f=13&t=3533 (latest)
The Living Dead 2: viewtopic.php?f=13&t=3483
Crafters DS (Minecraft 2D Clone!) viewtopic.php?f=13&t=3082
User avatar
Nobody
 
Posts: 2323
Joined: February 14th, 2011, 12:08 am
Location: United States of America

Re: Tile-Based Pokemon Style Movement

Postby Allinardo » January 21st, 2015, 1:36 am

I changed every instance of "Bottom_Screen" with "Top_Screen", and changed the map scrolling to the top as well.
Attachments
pkmn movement 1-topscreen.dsgm
Top Screen Movement
(11.88 KiB) Downloaded 181 times
User avatar
Allinardo
 
Posts: 33
Joined: January 7th, 2015, 3:41 am
Location: Los Angeles California

Re: Tile-Based Pokemon Style Movement

Postby Nobody » January 21st, 2015, 4:13 am

I actually don't have an answer for that...
Feel free to ask me for help. I'm on the forum a lot.

Recent Projects:
Retro Ducks: viewtopic.php?f=13&t=3533 (latest)
The Living Dead 2: viewtopic.php?f=13&t=3483
Crafters DS (Minecraft 2D Clone!) viewtopic.php?f=13&t=3082
User avatar
Nobody
 
Posts: 2323
Joined: February 14th, 2011, 12:08 am
Location: United States of America

Re: Tile-Based Pokemon Style Movement

Postby Allinardo » January 21st, 2015, 4:59 am

It's strange...I looked through the code several times, but it doesn't really seem to make sense. I copied the bottom screen exactly.
User avatar
Allinardo
 
Posts: 33
Joined: January 7th, 2015, 3:41 am
Location: Los Angeles California

Re: Tile-Based Pokemon Style Movement

Postby Nobody » January 21st, 2015, 3:54 pm

The weird part is that I took the original example, changed no code, created a new room and planted the objects, and I experienced the same glitch you did. I then deleted the objects and replanted a few and if seemed to work from there out and have no idea why...
Feel free to ask me for help. I'm on the forum a lot.

Recent Projects:
Retro Ducks: viewtopic.php?f=13&t=3533 (latest)
The Living Dead 2: viewtopic.php?f=13&t=3483
Crafters DS (Minecraft 2D Clone!) viewtopic.php?f=13&t=3082
User avatar
Nobody
 
Posts: 2323
Joined: February 14th, 2011, 12:08 am
Location: United States of America

Re: Tile-Based Pokemon Style Movement

Postby Allinardo » January 21st, 2015, 5:38 pm

Hey you're right...This script ONLY works in the first room made in the example. Could this have something to do with DSGM 5.2?
User avatar
Allinardo
 
Posts: 33
Joined: January 7th, 2015, 3:41 am
Location: Los Angeles California

Re: Tile-Based Pokemon Style Movement

Postby Nobody » January 21st, 2015, 6:00 pm

I did get it to work in a new room, but I have no idea what is causing the glitch...
Feel free to ask me for help. I'm on the forum a lot.

Recent Projects:
Retro Ducks: viewtopic.php?f=13&t=3533 (latest)
The Living Dead 2: viewtopic.php?f=13&t=3483
Crafters DS (Minecraft 2D Clone!) viewtopic.php?f=13&t=3082
User avatar
Nobody
 
Posts: 2323
Joined: February 14th, 2011, 12:08 am
Location: United States of America

Re: Tile-Based Pokemon Style Movement

Postby Buck_7 » January 21st, 2015, 6:04 pm

No, it depends on which object you place in the room first because this piece of code is wrongly set:

Code: Select all
PA_SpriteAnimPause([Me], 0, 1)

The 0 in the middle argument is set to the 0th sprite and the screen argument was set to [Me] :?: the only argument that was set correctly was the last one where pause is set to true/1

Take a look at how it should be done:

Code: Select all
PA_SpriteAnimPause (u8 screen, u8 sprite, u8 pause)

The sprite arguments are in the wrong places. It should be :

PA_SpriteAnimPause (Top_Screen, [Me], 1)

The problem was with the placement of the arguments
User avatar
Buck_7
 
Posts: 1460
Joined: February 19th, 2011, 1:44 am

Re: Tile-Based Pokemon Style Movement

Postby Nobody » January 21st, 2015, 10:37 pm

Buck_7 wrote:No, it depends on which object you place in the room first because this piece of code is wrongly set:

Code: Select all
PA_SpriteAnimPause([Me], 0, 1)

The 0 in the middle argument is set to the 0th sprite and the screen argument was set to [Me] :?: the only argument that was set correctly was the last one where pause is set to true/1

Take a look at how it should be done:

Code: Select all
PA_SpriteAnimPause (u8 screen, u8 sprite, u8 pause)

The sprite arguments are in the wrong places. It should be :

PA_SpriteAnimPause (Top_Screen, [Me], 1)

The problem was with the placement of the arguments

I knew that this was the problem but could not figure it out last night (I guess I was tired). I forgot that the screen argument typically comes first! Thanks yet again!

Buck: 2
Nobody: -1
Feel free to ask me for help. I'm on the forum a lot.

Recent Projects:
Retro Ducks: viewtopic.php?f=13&t=3533 (latest)
The Living Dead 2: viewtopic.php?f=13&t=3483
Crafters DS (Minecraft 2D Clone!) viewtopic.php?f=13&t=3082
User avatar
Nobody
 
Posts: 2323
Joined: February 14th, 2011, 12:08 am
Location: United States of America

Re: Tile-Based Pokemon Style Movement

Postby Allinardo » January 22nd, 2015, 1:00 am

Thank you so much! I guess I didn't think about the order of placement...I'm still new to this engine.
It works a lot better now ^-^
User avatar
Allinardo
 
Posts: 33
Joined: January 7th, 2015, 3:41 am
Location: Los Angeles California

Re: Tile-Based Pokemon Style Movement

Postby BLsquared » January 22nd, 2015, 2:07 am

Glad to hear it worked out.
...
Are we keeping score now, guys? Really?
Sorry I started this... XD
You may call me BLsquared, Commodore, or TexasBatman. Preferably BLsquared.
My avatar was made by this guy: http://christianmanga.com/smf/index.php ... bum;in=374

"Every puzzle has an answer." - Professor Layton
"In war, there is indeed no substitute for victory." - Douglas MacArthur
"Love and Peace!" - Vash the Stampede
"It's High Noon!" - McCree
Member of Memefefe's Squad
User avatar
BLsquared
 
Posts: 997
Joined: August 10th, 2011, 4:39 pm
Location: United States of America

Re: Tile-Based Pokemon Style Movement

Postby Buck_7 » January 22nd, 2015, 2:27 am

lol don't worry I can't count (JK)
User avatar
Buck_7
 
Posts: 1460
Joined: February 19th, 2011, 1:44 am

Re: Tile-Based Pokemon Style Movement

Postby Nobody » January 22nd, 2015, 3:35 am

BLsquared wrote:Glad to hear it worked out.
...
Are we keeping score now, guys? Really?
Sorry I started this... XD

nah, just a quick joke to keep the fun of the forum alive :lol:
Feel free to ask me for help. I'm on the forum a lot.

Recent Projects:
Retro Ducks: viewtopic.php?f=13&t=3533 (latest)
The Living Dead 2: viewtopic.php?f=13&t=3483
Crafters DS (Minecraft 2D Clone!) viewtopic.php?f=13&t=3082
User avatar
Nobody
 
Posts: 2323
Joined: February 14th, 2011, 12:08 am
Location: United States of America


Return to Tutorials

Who is online

Users browsing this forum: No registered users and 1 guest