Welcome Guest ( Log In | Register )

[ Big| Medium| Small] -

Post new topic Reply to topic  [ 1 post ] 
  Fri Mar 05, 2010 6:25 am
OS is not an ambiguous acronym. It is a way of life.
User avatar

Location: Over Seas
Tile-Based Movement

In order to learn some of the new features before I make my game for college, I decided to make some practice projects (in GM7, college is way-ay-ay-ay behind). Today's little success is a Tile-Based Movement engine scripted in GML.

What this does:

-Allows square objects to move one tile (32 square pixel sprites) at a time.
-Prevents objects from moving onto impassable tiles (invisible objects)

This is very similar to how some tutorials show you how to prevent the player from falling through tiles by using a collision mask.

First, create a player sprite (spr_player, a 32x32 circle) and a wall sprite (a 32x32 square). Next, create their objects (obj_player and obj_wall). obj_wall does not need anything. DO NOT MAKE IT SOLID! DO NOT GIVE IT CODE! Just leave it alone.

obj_player will only have two events; Create and Step.

Expand to see the code.

This code starts in the Create Event by declaring our instance variables. This is good practice, and when you make your player more complicated later, you can add more declarations here, such as animation_speed. The Step event is next. Here you must first collect the Input from the keyboard, so we grab some essential stuff, up/down/left/right, and some stuff for later (though this tutorial will not cover the SELECT or CANCEL branches of the system).

Next the tile is moved closer to its proper location. This would be the current Tile Position multiplied by 32. This code will prevent the player from switching the direction until the Real X (x) and the Tile X (tile_x * 32) are equal. The same goes for the Ys.

Next, based on the Input, a call is made to a script you are about to write, called scr_move.

Expand to see the code.

This code checks the direction passed in argument0 and changes the tile_x/y accordingly unless their is an obj_wall in the way. We have this code in a script so that we don't have to rewrite it for our NPCs and other moving objects. Notice that I use custom constants c_dir_*. All you have to do is put them in your Constants and give them any values (that are unique). Mine are 0 through 4 because I have no other constants.

Now, you should be able to create a map. Open up a Room and surround it with obj_wall objects. Feel free to build a maze. Place your obj_player object in the room, and run it.

WARNING: You need to place tiles on the 32x32 grid.

NOTE: You can replace all of the instances of 32 in the above code with a constant. Try changing the value of the constant to 16 or 64. Resize your sprites, and check it out.

Hope that was helpful. If ya'll have any questions, corrections, or suggestions, lemme know, yo.

~Optimist Shadow

Software Request Shop

Now taking requests. Written works, programs, games, lessons, whatever. Pricing negotiable.

Top Top
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

Who is online

Users browsing this forum: No users and 2 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

We are an independent, not-for-profit game making community.
Board Index
About Us
Downloadable Games
Free Browser Games
Games in Development
RPG Maker Support
Game Maker Support
Construct 2 Support
HBGames the eZine
Advanced RPG Maker
Site Announcements
Powered by phpBB © phpBB Group