AS: Main
Ok, in this tutorial, I will be explaining how to make boundaries or walls for your game. This might of been done before but Im not sure so here it goes.
What?
Boundaries are walls that your character cant go out of. For example, it keeps them from going off the screen, and from going into a certain spot you dont want.
How?
All you need is a very simple code that I will be explaining.
------------------------------------------
-----------------------------------------
Ok, the first thing you need to do is create your character. There is no instance name needed for it. Now, I will explain the code I am about to give you.
onClipEvent (load) {
speed=10;
}
Those are the variables we will be setting for the character. If you want to have accurate walls, you need variables. It is stating that speed will = 10 no matter what. Now, on to the next bit.
------------------------------------------
-----
onClipEvent (enterFrame) {
if(Key.isDown(Key.UP)) {
_y-=speed;
}
if(Key.isDown(Key.DOWN)) {
_y+=speed;
}
if(Key.isDown(Key.LEFT)) {
_x-=speed;
}
if(Key.isDown(Key.RIGHT)) {
_x+=speed;
}
}
That is the part that will make the character move. It is not ready to use yet since I didnt include the variable, so dont copy it. Ready for the next part?
------------------------------------------
--------------------------
if(this._y>400) {
this._y=400;
}
if(this._y<0) {
this._y=0;
}
if(this._x>550) {
this._x=550;
}
if(this._x<0) {
this._x=0;
}
Im assuming you are using a 550x400 screen. If not, you can change 400 and 500 to the width and height of your screen. This is the part that will not let the character move out of the screen. The way it works is it checks if _y and _x or in this case, the width and height, are larger than the screen. If they are, they will be equal to that number and not move out. Then, it checks if the width and height are less than 0. If they are, they are equal to 0 and can not move out. Sorry if I explained that weird but I couldnt do it any better. Now, the last part of the code, the actual walls.
------------------------------------------
------------------------------
Remember I said you needed variables to have accurate walls? Well, this is why. The code checks if the character is touching the wall, and if it is, the variable of speed will be equal to 0. Thus, making it so when the user presses the keys, they can not move becuase speed=0. So here is the last bit if code:
if(this.hitTest(_root.wall)) {
speed=0;
}
}
Its nothing special. Just the last part. Make sure you have a MC labled "walls" for the last part to work.
------------------------------------------
----------------------------
Now, what you have all been waiting for, I will give the actual code to put in the character.
------------------------------------------
--------
Just Movement:
onClipEvent (load) {
speed=10;
}
onClipEvent (enterFrame) {
if(Key.isDown(Key.UP)) {
_y-=speed;
}
if(Key.isDown(Key.DOWN)) {
_y+=speed;
}
if(Key.isDown(Key.LEFT)) {
_x-=speed;
}
if(Key.isDown(Key.RIGHT)) {
_x+=speed;
}
}
Movement with only boundaries:
onClipEvent (load) {
speed=10;
}
onClipEvent (enterFrame) {
if(Key.isDown(Key.UP)) {
_y-=speed;
}
if(Key.isDown(Key.DOWN)) {
_y+=speed;
}
if(Key.isDown(Key.LEFT)) {
_x-=speed;
}
if(Key.isDown(Key.RIGHT)) {
_x+=speed;
}
if(this._y>400) {
this._y=400;
}
if(this._y<0) {
this._y=0;
}
if(this._x>550) {
this._x=550;
}
if(this._x<0) {
this._x=0;
}
}
Movement with only walls:
onClipEvent (load) {
speed=10;
}
onClipEvent (enterFrame) {
if(Key.isDown(Key.UP)) {
_y-=speed;
}
if(Key.isDown(Key.DOWN)) {
_y+=speed;
}
if(Key.isDown(Key.LEFT)) {
_x-=speed;
}
if(Key.isDown(Key.RIGHT)) {
_x+=speed;
}
if(this.hitTest(_root.wall)) {
speed=0;
}
}
Now, the final code, with everything in it is this:
onClipEvent (load) {
speed=10;
}
onClipEvent (enterFrame) {
if(Key.isDown(Key.UP)) {
_y-=speed;
}
if(Key.isDown(Key.DOWN)) {
_y+=speed;
}
if(Key.isDown(Key.LEFT)) {
_x-=speed;
}
if(Key.isDown(Key.RIGHT)) {
_x+=speed;
}
if(this._y>400) {
this._y=400;
}
if(this._y<0) {
this._y=0;
}
if(this._x>550) {
this._x=550;
}
if(this._x<0) {
this._x=0;
}
if(this.hitTest(_root.wall)) {
speed=0;
}
}
------------------------------------------
---------------
Hope that helped everyone! I hope I didnt make any errors. Im too lazy to read over my post. Just for the record, the walls arent perfect. In my code, once you touch them, you cant move at all. Hopefully, thats fine with some of you.