Be a Supporter!

As: Faux Tile Bounds

  • 2,124 Views
  • 4 Replies
New Topic Respond to this Topic
PiGPEN
PiGPEN
  • Member since: May. 11, 2001
  • Offline.
Forum Stats
Member
Level 22
Animator
As: Faux Tile Bounds Nov. 30th, 2005 @ 11:18 PM Reply

As:Main

What all this here about?
This is a code i regularly use when working on games to make perfect boundaries, its a fake tile based system. The concept is that we move a perfect little box in boundaries of perfect little boxes and have the graphics commanded to follow.

Setup
First you'll want to decide what size your tiles will be, for the sake of this tutorial we'll use 20x20. Right-Click on the stage, select Grid>Edit Grid... Set the grid to 20x20 and tap on show grid and snap to grid. Draw some boundaries on screen and make them a movie clip and give them an instance name of "bounds". Now make a single square and make it a movie clip and give it an instance name of "hero". And finally make a circle into a movieclip and give it the instance name "graphics".

Code
//the following code goes inside the hero clip
onClipEvent (load) {
//the following vars are keycodes so if i choose to change the keys later i can
north = 38;
south = 40;
east = 39;
west = 37;
//these vars are for calling bounds and graphics
bounds = _root.bounds;
graphics = _root.graphics;
//these vars are the important ones that do useful stuff yay!
wait=false;
dir = "S";
speed = 20; //speed should be the same amount as your grids
grafspeed = 4; //alter this one to change the speed of walking
//and the following just makes sure the graphics are where we want them to be when we start
graphics._x = _x;
graphics._y = _y;
}
onClipEvent (enterFrame) {
//You may want to skip down to see the key codes first
if (wait) { //if the player has moved the hero and we now need to move the graphics
if (dir == "E") { //if we're goin east
graphics._x += grafspeed; //then the graphics go east
if (graphics._x>=_x) { //if the graphics are past or equal to the hero
graphics._x = _x; //the graphics eual the hero
wait = false; //allow the player to control again
}
//repeat for other directions
} else if (dir == "W") {
graphics._x -= grafspeed;
if (graphics._x<=_x) {
graphics._x = _x;
wait = false;
}
} else if (dir == "S") {
graphics._y += grafspeed;
if (graphics._y>=_y) {
graphics._y = _y;
wait = false;
}
} else if (dir == "N") {
graphics._y -= grafspeed;
if (graphics._y<=_y) {
graphics._y = _y;
wait = false;
}
}
}
if (wait == false) { //if we're all ready for the player to run around
if (Key.isDown(north)) { //yknow what this means fool
dir = "N"; //we set the dir var to N-north
_y -= speed; //we move the hero up in the grid
if (bounds.hitTest(_x, _y, true)) { //if we hit bounds on our way
_y += speed; //counteract our movement
}
wait = true; //wait for te graphics to catch up
//repeat for other directions
} else if (Key.isDown(south)) {
dir = "S";
_y += speed;
if (bounds.hitTest(_x, _y, true)) {
_y -= speed;
}
wait = true;
} else if (Key.isDown(east)) {
dir = "E";
_x += speed;
if (bounds.hitTest(_x, _y, true)) {
_x -= speed;
}
wait = true;
} else if (Key.isDown(west)) {
dir = "W";
_x -= speed;
if (bounds.hitTest(_x, _y, true)) {
_x += speed;
}
wait = true;
}
}
}

Wrap-Up
You can also add more to the graphics by making multiple frames in the graphics movieclip and labelling them things such as "walkN" "standS" etc etc. then adding
graphics.gotoAndStop("walk"+dir);
in the graphic movement codes and an
}else{
graphics.gotoAndStop("stand"+dir);
at the end of the hero movement code.


BBS Signature
TXR
TXR
  • Member since: Sep. 7, 2005
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to As: Faux Tile Bounds Nov. 30th, 2005 @ 11:23 PM Reply

Very nice tutorial...should help out alot of kids...

Keep it up.

GuyWithHisComp
GuyWithHisComp
  • Member since: Nov. 10, 2005
  • Offline.
Forum Stats
Member
Level 28
Blank Slate
Response to As: Faux Tile Bounds Dec. 3rd, 2005 @ 01:10 PM Reply

Maybe post a sample?


BBS Signature
PiGPEN
PiGPEN
  • Member since: May. 11, 2001
  • Offline.
Forum Stats
Member
Level 22
Animator
Response to As: Faux Tile Bounds Dec. 5th, 2005 @ 09:32 AM Reply

At 12/3/05 01:10 PM, GuyWithHisComp wrote: Maybe post a sample?

Wish Granted.

The sample includes some stuff not covered in the tute (diagonal movement, s-attack, camera stuff).


BBS Signature
seel
seel
  • Member since: Jun. 27, 2005
  • Offline.
Forum Stats
Member
Level 21
Musician
Response to As: Faux Tile Bounds May. 23rd, 2007 @ 06:23 PM Reply

At 12/5/05 09:32 AM, PiGPEN wrote:
At 12/3/05 01:10 PM, GuyWithHisComp wrote: Maybe post a sample?
Wish Granted.

The sample includes some stuff not covered in the tute (diagonal movement, s-attack, camera stuff).

"This account has been suspended."

Lol, tough luck.