Changing rooms
[ September 20, 2003 ] by Mad Sci
This article explains how to change rooms for example in Role Playing Games, maze games and so on.


This is the final result we want to achieve:

Download the source code of this example

First outline the floor and rooms on paper. I used 4x4 grid which gives me 16 rooms in total. Then position the doors between the rooms. Look at the Floor Map above. Once you are done with the map create the Floor Movie Clip with intact walls. This movie clip will have 16 keyframes; each keyframe will represent one room. Then cutout the doors according to the floor map. Keep in mind that each object you put in this movie clip will be a non-walkable object: this means that our hero will not be able to walk over it. This is usefull if you want to decorate the interior of each room with let say furniture, or other non-walkable objects. Once done with the Room Design create an empty layer with a stop() action in it. Go back to the Main Time line and create a new layer for the hero on top of the Floor Movie clip. Inside we put the following code wraped in OnClipevent(enterFrame){...}:

function change_room(dir)
{
	if(dir == "right") _root.floor.gotoAndStop(_root.floor._currentframe + 1);
	else if(dir == "left") _root.floor.gotoAndStop(_root.floor._currentframe - 1);
	else if(dir == "up") _root.floor.gotoAndStop(_root.floor._currentframe - 4);
	else if(dir == "down") _root.floor.gotoAndStop(_root.floor._currentframe + 4);
}

What this function will do is every time it receives a direction it will change the frame (room) in our Floor Movie Clip. You can see from the floor map that if you move right the room number will increase by 1, if you go left it decreases by 1, if you go up the room number will decrease by 4 and opposite if you go down the room number will increase by 4. Because the room number is actually key_frame number we can use _currentframe option to change the rooms.
So now we have to move the hero:

if(Key.isDown(Key.UP))
{
	Ystep = -5;
	Xstep = 0;
}
else if(Key.isDown(Key.DOWN))
{
	Ystep = 5;
	Xstep = 0;
}
else if(Key.isDown(Key.LEFT))
{
	Xstep = -5;
	Ystep = 0;
}
else if(Key.isDown(Key.RIGHT))
{
	Xstep = 5;
	Ystep = 0;
}
if(!_root.floor.hitTest(this._x + Xstep, this._y + Ystep, true))
{
	this._x += Xstep;
	this._y += Ystep;
	Ystep = 0;
	Xstep = 0;
}

This script will move the hero and at the same time will check for a collision detection with the Floor Movie clip.

NOTE: we use pixel based collision detection. Since the Doors are cutout at this place the hero will be allowed to get true or leave the room. What we have to do is simply to wrap the hero arround:

if(this._y >= 280)
{
	this._y = 45;
	change_room("down");
}
if(this._y <= 40)
{
	this._y = 270;
	change_room("up");
}
if(this._x >= 320)
{
	this._x = 45;
	change_room("right");
}
if(this._x <= 35)
{
	this._x = 315;
	change_room("left");
}

When the hero leaves the scene in will snap back on the opposite end. This will give the illusion that the hero is going through the door. At the same time we call the function to change the room.

Thats all folks!



 
 
Name: Mad Sci
Location: N/A
Age: N/A
Flash experience: N/A
Job: N/A
Website: http://mad-sci.lazoom.com/
 
 
| Homepage | News | Games | Articles | Multiplayer Central | Reviews | Spotlight | Forums | Info | Links | Contact us | Advertise | Credits |

| www.smartfoxserver.com | www.gotoandplay.biz | www.openspace-engine.com |

gotoAndPlay() v 3.0.0 -- (c)2003-2008 gotoAndPlay() Team -- P.IVA 03121770048