Forum Topic: AS: Creating Boundaries

(6,583 views • 41 replies)

This topic is 2 pages long. [ 1 | 2 ]

<< < > >>
None

SpamBurger

Reply To Post Reply & Quote

Posted at: 7/22/05 09:13 AM

SpamBurger NEUTRAL LEVEL 15

Sign-Up: 07/12/05

Posts: 4,747

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.

"However, the game received only two orders, one of which Molyneux speculated was from his mother." -Peter Molyneux's first game The Entrepreneur


None

Inglor

Reply To Post Reply & Quote

Posted at: 7/22/05 09:23 AM

Inglor NEUTRAL LEVEL 17

Sign-Up: 01/26/03

Posts: 10,948

Post about it in AS: Main ... nice tutorial, been done before but differently... and you didn't do shapeFlag...then again shapeFlag has been done before


None

SpamBurger

Reply To Post Reply & Quote

Posted at: 7/22/05 09:25 AM

SpamBurger NEUTRAL LEVEL 15

Sign-Up: 07/12/05

Posts: 4,747

Hmmm... sorry of its been done before. And, what exactly is shapeFlag?

"However, the game received only two orders, one of which Molyneux speculated was from his mother." -Peter Molyneux's first game The Entrepreneur


None

gorman2001

Reply To Post Reply & Quote

Posted at: 7/22/05 09:27 AM

gorman2001 NEUTRAL LEVEL 14

Sign-Up: 08/18/02

Posts: 1,935

if i can suggest something...

maybe put limiting conditions with key.isDown conditions... takes less coding to do the same thing...


None

Inglor

Reply To Post Reply & Quote

Posted at: 7/22/05 09:28 AM

Inglor NEUTRAL LEVEL 17

Sign-Up: 01/26/03

Posts: 10,948

it hasn't been done before AS IS... which makes this thread pretty useful..

shapeFlag is the allows you to hitTest curved objects rather then squares alone, there is a tutorial about it if I'm not mistaken in AS MAIN, lemme check


None

Inglor

Reply To Post Reply & Quote

Posted at: 7/22/05 09:30 AM

Inglor NEUTRAL LEVEL 17

Sign-Up: 01/26/03

Posts: 10,948


None

SpamBurger

Reply To Post Reply & Quote

Posted at: 7/22/05 09:31 AM

SpamBurger NEUTRAL LEVEL 15

Sign-Up: 07/12/05

Posts: 4,747

At 7/22/05 09:30 AM, Inglor wrote: here: http://www.newground../topic.php?id=293660


Thanks. That was helpful.

"However, the game received only two orders, one of which Molyneux speculated was from his mother." -Peter Molyneux's first game The Entrepreneur


None

SpamBurger

Reply To Post Reply & Quote

Posted at: 7/22/05 10:11 AM

SpamBurger NEUTRAL LEVEL 15

Sign-Up: 07/12/05

Posts: 4,747

At 7/22/05 09:27 AM, gorman2001 wrote: if i can suggest something...

maybe put limiting conditions with key.isDown conditions... takes less coding to do the same thing...

Maybe, I mean, the users dont care. All they do is copy the code! lol

"However, the game received only two orders, one of which Molyneux speculated was from his mother." -Peter Molyneux's first game The Entrepreneur


None

Inglor

Reply To Post Reply & Quote

Posted at: 7/22/05 10:23 AM

Inglor NEUTRAL LEVEL 17

Sign-Up: 01/26/03

Posts: 10,948

I always put little errors in my code of tutorials so people will have to actually read them rather then copy the code :P

the point of tutorials is to teach... if they fail at that... they aren't good tutorials...


None

SpamBurger

Reply To Post Reply & Quote

Posted at: 7/22/05 11:20 AM

SpamBurger NEUTRAL LEVEL 15

Sign-Up: 07/12/05

Posts: 4,747

At 7/22/05 10:23 AM, Inglor wrote: I always put little errors in my code of tutorials so people will have to actually read them rather then copy the code :P

the point of tutorials is to teach... if they fail at that... they aren't good tutorials...

Yea, your right. I think I did a good job teaching though. If you put errors in the code, then people start complaining.

"However, the game received only two orders, one of which Molyneux speculated was from his mother." -Peter Molyneux's first game The Entrepreneur


None

Inglor

Reply To Post Reply & Quote

Posted at: 7/22/05 11:24 AM

Inglor NEUTRAL LEVEL 17

Sign-Up: 01/26/03

Posts: 10,948

or actually reading the instructions rather then just copying the code... :P

the errors are not related to the subject itself...


None

SpamBurger

Reply To Post Reply & Quote

Posted at: 7/22/05 11:41 AM

SpamBurger NEUTRAL LEVEL 15

Sign-Up: 07/12/05

Posts: 4,747

At 7/22/05 11:24 AM, Inglor wrote: the errors are not related to the subject itself...

Oh, ok.

"However, the game received only two orders, one of which Molyneux speculated was from his mother." -Peter Molyneux's first game The Entrepreneur


Angry

liaaaam

Reply To Post Reply & Quote

Posted at: 7/22/05 12:13 PM

liaaaam NEUTRAL LEVEL 22

Sign-Up: 12/11/04

Posts: 14,534

>: (

The way you did the walls is wrong, your way makes it so that if you hit a wall, you can't move in any direction, so you can't move away from the wall.

Clicky.


None

SpamBurger

Reply To Post Reply & Quote

Posted at: 7/22/05 12:15 PM

SpamBurger NEUTRAL LEVEL 15

Sign-Up: 07/12/05

Posts: 4,747

Yea, I know. Remember I said that? Do you know of a way I could fix it? I might try getBounds() but thats complicated.

"However, the game received only two orders, one of which Molyneux speculated was from his mother." -Peter Molyneux's first game The Entrepreneur


None

SpamBurger

Reply To Post Reply & Quote

Posted at: 7/22/05 12:19 PM

SpamBurger NEUTRAL LEVEL 15

Sign-Up: 07/12/05

Posts: 4,747

At 7/22/05 12:15 PM, SpamBurger wrote: Yea, I know. Remember I said that? Do you know of a way I could fix it? I might try getBounds() but thats complicated.

Ooops! Never mind! I saw the code in the demo thing.

"However, the game received only two orders, one of which Molyneux speculated was from his mother." -Peter Molyneux's first game The Entrepreneur


None

liaaaam

Reply To Post Reply & Quote

Posted at: 7/22/05 12:19 PM

liaaaam NEUTRAL LEVEL 22

Sign-Up: 12/11/04

Posts: 14,534

At 7/22/05 12:15 PM, SpamBurger wrote: Yea, I know. Remember I said that? Do you know of a way I could fix it? I might try getBounds() but thats complicated.

Click on my click thingie, I put a working code in there.. just copy and paste from the textbox.


None

SpamBurger

Reply To Post Reply & Quote

Posted at: 7/25/05 11:02 AM

SpamBurger NEUTRAL LEVEL 15

Sign-Up: 07/12/05

Posts: 4,747

Ok, I made a better wall code. I created it right here on the BBS so hopefully it works. Here is what to do.

First, create your character and give it this code:

onClipEvent (load) {
_name="player";
up=10;
down=10;
left=10;
right=10;
}
onClipEvent (enterFrame) {
if(Key.isDown(Key.UP)) {
_y-=up;
}
if(Key.isDown(Key.DOWN)) {
_y+=down;
}
if(Key.isDown(Key.LEFT)) {
_x-=left;
}
if(Key.isDown(Key.RIGHT)) {
_x+=right;
}
}

Then, create your wall, then give it the instance name of wall. Double click the wall and split the wall into four MC's. Convert the top-half of the wall to a MC and give it the instance name of wallUp. Then, convert the bottom-half to a MC and give it the instance name of wallDown. Then, convert the left half to a MC and give it the instance name of wallLeft. Then, create the right half of the wall to a MC and give it the instance name of wallRight. (Wow, that was long). Then, click the back button and give the wall this code:

onClipEvent (enterFrame) {
if(_root.player.hitTest(_root.wall.wallUp)
) {
_root.player.up=0;
}
if(_root.player.hitTest(_root.wall.wallDow
n)) {
_root.player.down=0;
}
if(_root.player.hitTest(_root.wall.wallLef
t)) {
_root.player.left=0;
}
if(_root.player.hitTest(_root.wall.wallRig
ht)) {
_root.player.right=0;
}
}

Hopefully that works. Im too lazy to explain it so have fun with it. Im hoping, the new auto line breaker thingy doesnt mess it up.

"However, the game received only two orders, one of which Molyneux speculated was from his mother." -Peter Molyneux's first game The Entrepreneur


None

SpamBurger

Reply To Post Reply & Quote

Posted at: 7/25/05 02:56 PM

SpamBurger NEUTRAL LEVEL 15

Sign-Up: 07/12/05

Posts: 4,747

Ooops! The other code doesnt work properly. (Next time I wont create my code on the bbs). Follow all the other instructions but replace the wall code with this:

onClipEvent (enterFrame) {
if(_root.player.hitTest(_root.wall.wallUp)
) {
_root.player.down=0;
}else{
_root.player.down=10;
}
if(_root.player.hitTest(_root.wall.wallDow
n)) {
_root.player.up=0;
}else{
_root.player.up=10;
}
if(_root.player.hitTest(_root.wall.wallLef
t)) {
_root.player.right=0;
}else{
_root.player.right=10
if(_root.player.hitTest(_root.wall.wallRig
ht)) {
_root.player.left=0;
}else{
_root.player.left=10;
}
}
}

Thats it!

"However, the game received only two orders, one of which Molyneux speculated was from his mother." -Peter Molyneux's first game The Entrepreneur


None

Inglor

Reply To Post Reply & Quote

Posted at: 7/25/05 02:59 PM

Inglor NEUTRAL LEVEL 17

Sign-Up: 01/26/03

Posts: 10,948

that code lost it's point...

anyone can do hitTests, boundaries are more then just hitTests


None

SpamBurger

Reply To Post Reply & Quote

Posted at: 7/25/05 03:04 PM

SpamBurger NEUTRAL LEVEL 15

Sign-Up: 07/12/05

Posts: 4,747

At 7/25/05 02:59 PM, Inglor wrote: that code lost it's point...

anyone can do hitTests, boundaries are more then just hitTests

I know, but the code still works.

"However, the game received only two orders, one of which Molyneux speculated was from his mother." -Peter Molyneux's first game The Entrepreneur


None

Toast

Reply To Post Reply & Quote

Posted at: 7/30/05 03:39 AM

Toast DARK LEVEL 09

Sign-Up: 04/02/05

Posts: 8,917

It's pretty much the same thing in:
AS: Collisions detections, AS: Basic Physics & Gravity,, and AS: Variables.

But since yours isn't a copy of any of those, it's okay :P


None

TheToon-1

Reply To Post Reply & Quote

Posted at: 9/2/05 05:17 AM

TheToon-1 LIGHT LEVEL 04

Sign-Up: 08/30/05

Posts: 80

Problem with changing the speed to 0 is that you get stuck and can't move again(well,I didn't read entire post so I don't know whether or not you added something to fix :D)

...good ol' scripted walls...although a lot different than what I do.


None

harman-dragon

Reply To Post Reply & Quote

Posted at: 11/30/05 08:19 PM

harman-dragon FAB LEVEL 13

Sign-Up: 02/18/03

Posts: 268

NJ, lol, keeep up the gud poosts


None

harman-dragon

Reply To Post Reply & Quote

Posted at: 2/11/06 05:34 PM

harman-dragon FAB LEVEL 13

Sign-Up: 02/18/03

Posts: 268

Very, Very Useful *Applause*


None

Evil-Entety

Reply To Post Reply & Quote

Posted at: 2/22/06 12:57 PM

Evil-Entety EVIL LEVEL 04

Sign-Up: 02/20/06

Posts: 113

did the script work for you because it never worked for me ---i might ov not followed correctly----


Happy

Obsidion

Reply To Post Reply & Quote

Posted at: 5/20/06 03:57 AM

Obsidion EVIL LEVEL 07

Sign-Up: 04/21/06

Posts: 23

Worked for me :) Very helpful althought I didint do EVERYTHING like you did. Just partial :)


None

Toast

Reply To Post Reply & Quote

Posted at: 5/20/06 05:03 AM

Toast DARK LEVEL 09

Sign-Up: 04/02/05

Posts: 8,917

At 7/22/05 10:23 AM, Inglor wrote: I always put little errors in my code of tutorials so people will have to actually read them rather then copy the code :P

You should hide secret codes/numbers in the instructions and at the end post a link to the .swf containing the code, except before you can see the code you need to have the password: the hidden numbers in the instructions. Lmao, that'd be funny. :P

Nice code Spam, but it uses a bit too much of cpu. I'd recommend having a spd variable and decrease / increase it when needed. Then when you hit a wall it gets a bit reversed or something. And at th end you'd need something like _x += spd;

I made it a bit shorter, I think it could use less cpu that way. 1 Line autoformatted. :p

_root.player.hitTest(_root.wall.wallUp) ? _root.player.down = 0:_root.player.down = 10,_root.player.hitTest(_root.wall.wallDow
n) ? _root.player.up = 0:_root.player.up = 10,_root.player.hitTest(_root.wall.wallLef
t) ? _root.player.right = 0:(_root.player.right = 10,_root.player.hitTest(_root.wall.wallRig
ht) ? _root.player.left = 0:_root.player.left = 10);

With binary it can be even shorter, let inglor read this reply and pwn me. :)


None

Paranoia

Reply To Post Reply & Quote

Posted at: 5/20/06 05:17 AM

Paranoia DARK LEVEL 34

Sign-Up: 04/22/05

Posts: 9,699

At 5/20/06 05:03 AM, -Toast- wrote: I made it a bit shorter, I think it could use less cpu that way. 1 Line autoformatted. :p

_root.player.hitTest(_root.wall.wallUp) ? _root.player.down = 0:_root.player.down = 10,_root.player.hitTest(_root.wall.wallDow
n) ? _root.player.up = 0:_root.player.up = 10,_root.player.hitTest(_root.wall.wallLef
t) ? _root.player.right = 0:(_root.player.right = 10,_root.player.hitTest(_root.wall.wallRig
ht) ? _root.player.left = 0:_root.player.left = 10);

With binary it can be even shorter, let inglor read this reply and pwn me. :)

Ahem...

_root.player.down = (!_root.player.hitTest(_root.wall.wallUp)) * 10;
_root.player.up = (!_root.player.hitTest(_root.wall.wallDown
)) * 10;
_root.player.left = (!_root.player.hitTest(_root.wall.wallLeft
)) * 10;
_root.player.right = (!_root.player.hitTest(_root.wall.wallRigh
t)) * 10;

I think that's what you were getting at - It's a lot shorter in characters and not all shoved into one untidy line :P

Now, who can do better?

BBS Signature

None

Toast

Reply To Post Reply & Quote

Posted at: 5/20/06 05:34 AM

Toast DARK LEVEL 09

Sign-Up: 04/02/05

Posts: 8,917

At 5/20/06 05:17 AM, _Paranoia_ wrote: Now, who can do better?

_root.boundary.hitTest(_x,_y,true) ? spd *= -1:0;

:P


None

newgroundbound

Reply To Post Reply & Quote

Posted at: 6/6/06 07:36 PM

newgroundbound EVIL LEVEL 07

Sign-Up: 05/20/05

Posts: 135

Pretty good tutorial!


All times are Eastern Standard Time (GMT -5) | Current Time: 02:57 PM

<< Back

This topic is 2 pages long. [ 1 | 2 ]

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!