00:00
00:00
Newgrounds Background Image Theme

ShogunAfterDark just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

AS: Movement w/ Boundaries

1,710 Views | 26 Replies
New Topic Respond to this Topic

AS: Movement w/ Boundaries 2005-12-16 19:48:47


AS: Main

Movement w/ Boundaries

This is a pretty straightforward tutorial. It covers simple movement and boundaries, which are commonly used in overhead-style games. (think GTA or GTA2). I'm not very good with actionscript, so forgive me if there's a better way to do it. I pretty much learned everything I know about boundries from Rystic's Platformer Tutorial. In fact, this tutorial is pretty much a modified version of his. All credit should go to him. So let's begin.

Here are a sample file:
SAMPLE (arrow keys - move)

Tutorial:
Draw a square and make it a movie clip. No need to give it an instance name, the code will do it for you. (another thing I learned from Rystic's tutorial). Open up the actions menu, and put in this code:


onClipEvent (load) {
_name = "guy";
speed = 5;
}
onClipEvent (enterFrame) {
xmin = getBounds(_root).xMin;
xmax = getBounds(_root).xMax;
ymin = getBounds(_root).yMin;
ymax = getBounds(_root).yMax;
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.LEFT)) {
_x -= speed;
}
if (Key.isDown(Key.RIGHT)) {
_x += speed;
}
if (Key.isDown(Key.UP)) {
_y -= speed;
}
if (Key.isDown(Key.DOWN)) {
_y += speed;
}
}

The speed variable can be changed to whatever you want. So can the keys. The movement is pretty straightforward. UP makes the Y value decrease, DOWN makes it increase, same for the X value with LEFT and RIGHT. The xmin = getBounds(_root).xMin; lines get the boundaries of the object and turn them into easy-to-use variables. Hooray variables.

Alrighty. Now make another square. This will be your wall. Put this code in it:


onClipEvent (enterFrame) {
xmin = getBounds(_root).xMin;
xmax = getBounds(_root).xMax;
ymin = getBounds(_root).yMin;
ymax = getBounds(_root).yMax;

if (hitTest(_root.guy) && _root.guy.xmax<xmin) {
_root.guy._x -= _root.guy.speed;
}
if (hitTest(_root.guy) && _root.guy.xmin>xmax) {
_root.guy._x += _root.guy.speed;
}
if (hitTest(_root.guy) && _root.guy.ymin<ymax) {
_root.guy._y -= _root.guy.speed;
}
if (hitTest(_root.guy) && _root.guy.ymin>ymax) {
_root.guy._y += _root.guy.speed;
}
}

There's the code for the walls and whatnot. They don't need instance names, so go nuts. But what does this code do? Well, I'll try my best to explain. The first part is the same as before. It converts the boundaries into easy-to-use variables. The second part tells the guy to stop when he hits a wall.

if (hitTest(_root.guy) && _root.guy.ymin>ymax) {
_root.guy._y += _root.guy.speed;
}

When the guy hits the wall, the wall takes the guy's speed in that direction and adds negative to it. Because (something) + (negative something) = 0, it effectively stops it.

There you have it. I think.

AS: Maine

Speaking of Maine, how bout that Rhode Island? Rhode Island is neither a road, nor an island. Discuss.

I used to be relevant.

Response to AS: Movement w/ Boundaries 2005-12-16 19:55:24


sorry, but I'm pretty sure this is already covered in here
http://www.newground../topic.php?id=311358

maybe denvish could add this thread as an extra reference

Response to AS: Movement w/ Boundaries 2005-12-16 19:58:01


At 12/16/05 07:55 PM, Inglor wrote: sorry, but I'm pretty sure this is already covered in here
http://www.newground../topic.php?id=311358

Yeah, I saw that one. I didn't like it (no offense). And this one uses getBounds.

IT'S DIFFERENT I SAY!


I used to be relevant.

Response to AS: Movement w/ Boundaries 2005-12-16 19:59:31


the min and max do absolutly noting worthwile here is a much simplified version of the code:

onClipEvent (load) {
_name = "guy";
speed = 5;
hit = false;
}
onClipEvent (enterFrame) {
}
onClipEvent (enterFrame) {
if (!hit) {
if (Key.isDown(Key.LEFT)) {
_rotation = 270;
walk = true;
} else {
if (Key.isDown(Key.RIGHT)) {
_rotation = 90;
walk = true;
} else {
if (Key.isDown(Key.UP)) {
_rotation = 0;
walk = true;
} else {
if (Key.isDown(Key.DOWN)) {
_rotation = 180;
walk = true;
} else {
walk = false;
}
}
}
}
}
if (walk) {
xSpeed = speed*Math.sin(this._rotation*(Math.PI/180
));
ySpeed = speed*Math.cos(this._rotation*(Math.PI/180
));
_x += xSpeed;
_y -= ySpeed;
}
if (_root.wall.hitTest(this)) {
hit = true;
} else {
hit = false;
}
}

Response to AS: Movement w/ Boundaries 2005-12-16 19:59:45


Mwhahahaha bob's back

Why did you just repeat Rystic's great work? All you did was explain it (the movement part). You arn't fite to write an AS: !!! I bet you couldn't even explain the jumping part (not saying that i could).

Response to AS: Movement w/ Boundaries 2005-12-16 20:02:16


At 12/16/05 07:59 PM, BobRicci wrote: Why did you just repeat Rystic's great work? All you did was explain it (the movement part). You arn't fite to write an AS: !!! I bet you couldn't even explain the jumping part (not saying that i could).

I can't explain the jumping part. I couldn't explain the jumping part to save my life. This is just using getBounds. Without the jumping part. It's what I've learned from his tutorial. That's why I've credited him several times.


I used to be relevant.

Response to AS: Movement w/ Boundaries 2005-12-16 20:02:32


sorry not simplified, better

Response to AS: Movement w/ Boundaries 2005-12-16 20:04:23


At 12/16/05 08:02 PM, Disarray_yarrasiD wrote: sorry not simplified, better

But that has absolutely nothing to do with the code I posted. Where are the boundaries, good sir? It's just moving with rotation!

The whole point of this thing is moving and boundaries.


I used to be relevant.

Response to AS: Movement w/ Boundaries 2005-12-16 20:06:28


i forgot something:

replace:

if (walk) {
xSpeed = speed*Math.sin(this._rotation*(Math.PI/180
));
ySpeed = speed*Math.cos(this._rotation*(Math.PI/180
));
_x += xSpeed;
_y -= ySpeed;
}
if (_root.wall.hitTest(this)) {
hit = true;
} else {
hit = false;
}
with:

xSpeed = speed*Math.sin(this._rotation*(Math.PI/180
));
ySpeed = speed*Math.cos(this._rotation*(Math.PI/180
));

if (walk) {
_x += xSpeed;
_y -= ySpeed;
}

if (_root.wall.hitTest(this)) {
hit = true;
_x-=xSpeed
_y+=ySpeed
} else {
hit = false;
}

Response to AS: Movement w/ Boundaries 2005-12-16 20:09:32


At 12/16/05 07:48 PM, scottmale24 wrote: AS: Main

huzzah for as main

It covers simple movement and boundaries, which are commonly used in overhead-style games. (think GTA or GTA2).

not really, GTA2 uses overhead, true, but it uses trigonometric movement, the player moves in the direction it is rotated towerds, it uses Math.sin (for _y) and Math.cos (for _x).

I'm not very good with actionscript, so forgive me if there's a better way to do it.

that's ok, even though there is (you checked it in every single mc, you could have just used an onEnterFrame in _root or the player itself) (you used normal hitTest where shapeFlag against the whole stage would have been more effective) another perspective at a topic is always good.

I pretty much learned everything I know about boundries from Rystic's Platformer Tutorial. In fact, this tutorial is pretty much a modified version of his. All credit should go to him. So let's begin.

ok, go for it :)

onClipEvent (load) {
_name = "guy";

//I don't think that messing with _name is proper, why can't you just instance name it?

speed = 5;

//I have to disagree with that, please use var speed:Number=5;

}
onClipEvent (enterFrame) {
xmin = getBounds(_root).xMin;

//could have used _x

xmax = getBounds(_root).xMax;

//could have used _width + _x

ymin = getBounds(_root).yMin;

//could have used _y

ymax = getBounds(_root).yMax;

//could have used _y + _height

}
onClipEvent (enterFrame) {
if (Key.isDown(Key.LEFT)) {
_x -= speed;
}

//could have used _x -=speed * Key.isDown(Key.LEFT)):

if (Key.isDown(Key.RIGHT)) {
_x += speed;

//same comment as above about the binary assigment

}
if (Key.isDown(Key.UP)) {
_y -= speed;

//same comment as above about the binary assigment

}
if (Key.isDown(Key.DOWN)) {
_y += speed;

//same comment as above about the binary assigment

}
}



The speed variable can be changed to whatever you want. So can the keys.

//only some keys need ASCII values and not just .KEYNAME

The movement is pretty straightforward. UP makes the Y value decrease, DOWN makes it increase, same for the X value with LEFT and RIGHT.

it's a bit more complex than that but ok :)

The xmin = getBounds(_root).xMin; lines get the boundaries of the object and turn them into easy-to-use variables. Hooray variables.

//the _x _width _y and _height are there for you, calling a method takes up more resources

Alrighty. Now make another square. This will be your wall. Put this code in it:

//square walls are no fun >:( circle walls are funner, than again this code doesn't work with anything but squares so I'll shuddap


onClipEvent (enterFrame) {
xmin = getBounds(_root).xMin;
xmax = getBounds(_root).xMax;
ymin = getBounds(_root).yMin;
ymax = getBounds(_root).yMax;

//same getBounds optimization tip as above

if (hitTest(_root.guy) && _root.guy.xmax<xmin) {
_root.guy._x -= _root.guy.speed;
}
if (hitTest(_root.guy) && _root.guy.xmin>xmax) {
_root.guy._x += _root.guy.speed;
}
if (hitTest(_root.guy) && _root.guy.ymin<ymax) {
_root.guy._y -= _root.guy.speed;
}
if (hitTest(_root.guy) && _root.guy.ymin>ymax) {
_root.guy._y += _root.guy.speed;
}
}

//same optimization tip about the IFs as above, aslo, any reason you're checking both a hitTest and a getBounds? you can just check one.



There's the code for the walls and whatnot. They don't need instance names, so go nuts. But what does this code do? Well, I'll try my best to explain. The first part is the same as before. It converts the boundaries into easy-to-use variables. The second part tells the guy to stop when he hits a wall.

if (hitTest(_root.guy) && _root.guy.ymin>ymax) {
_root.guy._y += _root.guy.speed;
}
When the guy hits the wall, the wall takes the guy's speed in that direction and adds negative to it. Because (something) + (negative something) = 0, it effectively stops it.

There you have it. I think.
AS: Maine

it's better than buttsekcs.

Speaking of Maine, how about that Rhode Island? Rhode Island is neither a road, nor an island. Discuss.

The name of the state is NOT Rhode Island! If you look in
the US Constitution, you will see that the name of
the state is "The State of Rhode Island and Providence Plantations."

And one of those islands *is* named Rhode Island (a settlement on which
is what the state's official name refers to, as dr topper states.)
This island, called Aquidneck by its Indian inhabitants, was purchased
from them by a group from Boston fleeing religious persecution. According
to the Encyclopedia Britannica, these settlers chose that name because
they mistakenly believed that this was the name used by the 16th century
Italian explorer Giovanni da Varrazzano. In fact, Varrazzano was referring
to the island now called Block Island, and the basis for this was that
he found this island to be comparable in size to the Mediterranean island
of Rhodes. Quite a convoluted tale!

Response to AS: Movement w/ Boundaries 2005-12-16 20:16:13


At 12/16/05 08:02 PM, scottmale24 wrote: It's what I've learned from his tutorial.

Then it shouldn't be an AS:

Then I might as well make an AS: saying:

I learned this from this tut:

stop();

it stops the movie OMFG_BBQ!!!

play();

it plays the movie HOLYS**T!!!

gotoAndS**t (#);

it shits sometwheer JEZUZ WOWWOWWOW!!!!!

Response to AS: Movement w/ Boundaries 2005-12-16 20:18:52


At 12/16/05 08:16 PM, BobRicci wrote: Then I might as well make an AS: saying:
<noob post>

No thats just stupid o.O

Dang. I really think we need a ''under judgement'' zone for these AS threads, they are exploding everywhere. ;-) And usually, people say its the same thing as something else, hah.


"Actually, the server timed out trying to remove all your posts..."

-TomFulp

Response to AS: Movement w/ Boundaries 2005-12-16 20:18:55


At 12/16/05 08:09 PM, Inglor wrote: //I don't think that messing with _name is proper, why can't you just instance name it?

It makes it easier for people who want it done fast.

//I have to disagree with that, please use var speed:Number=5;

Does not work in flash5. Which is what I use.

The speed variable can be changed to whatever you want. So can the keys.
//only some keys need ASCII values and not just .KEYNAME

I know. I know. I learned that beforehand.
Also, what is this shapeflag thing? I've never heard of it before. Does it exist in flash5?

And one of those islands *is* named Rhode Island (a settlement on which
is what the state's official name refers to, as dr topper states.)
This island, called Aquidneck by its Indian inhabitants, was purchased
from them by a group from Boston fleeing religious persecution. According
to the Encyclopedia Britannica, these settlers chose that name because
they mistakenly believed that this was the name used by the 16th century
Italian explorer Giovanni da Varrazzano. In fact, Varrazzano was referring
to the island now called Block Island, and the basis for this was that
he found this island to be comparable in size to the Mediterranean island
of Rhodes. Quite a convoluted tale!

I did not know that. Hooray for learning!


I used to be relevant.

Response to AS: Movement w/ Boundaries 2005-12-16 20:25:25


At 12/16/05 08:18 PM, scottmale24 wrote: Also, what is this shapeflag thing? I've never heard of it before. Does it exist in flash5?

ynaw, flash mx 2004 costs next to nothing nowdays... flash 5 is old and outdated. however yes, it does support shapeflag

try reading http://www.newground../topic.php?id=293660

Response to AS: Movement w/ Boundaries 2005-12-16 20:29:24


At 12/16/05 08:25 PM, Inglor wrote: ynaw, flash mx 2004 costs next to nothing nowdays... flash 5 is old and outdated. however yes, it does support shapeflag

try reading http://www.newground../topic.php?id=293660

Sounds interesting. And by "interesting" I mean incredibly useful.


I used to be relevant.

Response to AS: Movement w/ Boundaries 2005-12-16 20:38:33


At 12/16/05 08:25 PM, Inglor wrote: flash 5 is old and outdated

HEY I USE FLASH 5!!! (don't take that as an insult 'cause it was supposed to be sarcastic although it's harder imprint sarcasim through just typed words through the net and although it was from the Greek word sarkasmos which was again from the Greek word sarkazein meaning "to bite the lips in rage" I absoulutley do not imply to do that although I have thought about, and how ironic, from being banned 7 times for typing in caps.)

In all, it was basicaly a lighly flavored caustic remark.

Response to AS: Movement w/ Boundaries 2005-12-16 20:47:08


HEY I USE FLASH 5!!! (don't take that as an insult 'cause it was supposed to be sarcastic although it's harder imprint sarcasim through just typed words through the net and although it was from the Greek word sarkasmos which was again from the Greek word sarkazein meaning "to bite the lips in rage" I absoulutley do not imply to do that although I have thought about, and how ironic, from being banned 7 times for typing in caps.)

In all, it was basicaly a lighly flavored caustic remark.

You use Flash 5?!?!?!Get with the dates man,Flash 8 is the new King of the World!

Everything you posted after "HEY I USE FLASH 5!!!" was spam.

Now to make my post not spam,I'm gonna post a thought.

I really wish I knew getBounds better,but it's not explained anywhere in AS:Main...


wat

Response to AS: Movement w/ Boundaries 2005-12-16 21:03:18


At 12/16/05 08:47 PM, -Snow- wrote: Get with the dates man,Flash 8 is the new King of the World!

*mumurs quitetly* f**k you rich boy... (some evidence suggests that "f**k" actually meant to strike back in the 17th century which i mean not to do for the main reason being i do not no where you live but even if it was so i still would not wish to harm you. plus... some evidence that "f**k" actually could have been offensive in the 16th century but we can never be certain. As for the etymology of the word "f**k" no one is quite certain of where it started but some evidence clearly suggests that it had an Anglo-Saxon origon. In conclusion "f**k" is a very mysterious word)

Response to AS: Movement w/ Boundaries 2005-12-16 21:08:36


Stop spamming please.

kthxbi


wat

Response to AS: Movement w/ Boundaries 2005-12-16 21:10:15


At 12/16/05 09:08 PM, -Snow- wrote: Stop spamming please.

kthxbi

how is this spam?


aquaticmole.

BBS Signature

Response to AS: Movement w/ Boundaries 2005-12-16 21:21:50


At 12/16/05 09:08 PM, -Snow- wrote: Stop

The word stop has several possible meanings in the English language.

The term stop, when used by itself, can refer to:

One or more ranks of pipes used together to make a particular sound on a pipe organ, or the control used to select them, see organ stop

An error in Windows NT or NT-based operating systems, see blue screen of death.

The point on an animal's head at which the muzzle meets the forehead.

The punctuation mark more correctly known as a full stop (British) or a period (US): "."

A limiting aperture in an optical system, such as an f-stop

"Stop" is also a part of the name of:
bus stop and tram stop: places where the bus or tram stops for people to get on and off

door stop: a wedge to stop a door from closing when you want it open

stop sign: a traffic sign informing drivers to make a temporary stop.

In financial markets, a stop loss order is executed if the value of a position has declined below a certain limit.

Terry stop: in U.S. law, a stop-and-frisk law enforcement procedure less intrusive than an arrest, so probable cause is not required

truck stop: an eating establishment on a major trucking route with a large carpark for truck drivers to stop and refresh themselves, and often with other amenities available.

Stop - a song by Pink Floyd, from the album, The Wall

Stop: a 2004 album by Franco De Vita that featured the hits "Tu De Que Vas" and "Si La Ves".

"Stop": a 1998 release by Spice Girls, from their second studio album, Spiceworld, featuring the hits, "Spice Up Your Life" and "Too Much".

Response to AS: Movement w/ Boundaries 2005-12-16 21:23:44


At 12/16/05 09:10 PM, aquaticmole wrote:
At 12/16/05 09:08 PM, -Snow- wrote: Stop spamming please.

kthxbi
how is this spam?

BobRicci is spamming,and he won't stop >:(


wat

Response to AS: Movement w/ Boundaries 2005-12-16 21:25:44


At 12/16/05 09:03 PM, BobRicci wrote: f**k, not swearing but an incredible simulation!!!!

nothing looks/ sounds more retarded then someone cencoring himself. if you cant say fuck then just replace fuck or take fuck out because fuck usually isnt nesicarry(sp). dont think your some kind of genious who slipped past the censors.

and i dont like the moveMent code in this tut. you should really avoid multiple walls. and try to keep most of the code on a single Mc. heres the simplest code i have

onClipEvent (load) {
function val(n) {
return (n > 0 ? 1 : -1);
}
x = y = 0;
w = _width / 2;
h = _height / 2;
}
onClipEvent (enterFrame) {
x += Key.isDown(68) - Key.isDown(65);
y += Key.isDown(83) - Key.isDown(87);
_x += x * !_root.wall.hitTest(_x + x + (val(x) * w), _y, true);
_y += y * !_root.wall.hitTest(_x, _y + y + (val(y) * h), true);
_root.wall.hitTest(_x + x + (val(x) * w), _y, true) ? x *= 0 : x *= .9;
_root.wall.hitTest(_x, _y + y + (val(y) * h), true) ? y *= 0 : y *= .9;
}

its wasd because im too lazy to change it. i pulled it off an old game.

Response to AS: Movement w/ Boundaries 2005-12-16 21:30:12


At 12/16/05 09:23 PM, -Snow- wrote:
At 12/16/05 09:10 PM, aquaticmole wrote:
At 12/16/05 09:08 PM, -Snow- wrote: Stop spamming please.

kthxbi
how is this spam?
BobRicci is spamming,and he won't stop >:(

oh okay i thot u ment this whole tutorial was spam and i was like WTF??? but then again it is bobricci who shood win the bbs awards 4 attention whore and worst poster ever


aquaticmole.

BBS Signature

Response to AS: Movement w/ Boundaries 2005-12-16 21:33:42


At 12/16/05 09:21 PM, BobRicci wrote: Terry stop: in U.S. law, a stop-and-frisk law enforcement procedure less intrusive than an arrest, so probable cause is not required

thats what we meant

Response to AS: Movement w/ Boundaries 2005-12-17 15:32:49


At 12/16/05 09:30 PM, aquaticmole wrote: attention whore

I've only got 1 vote for that... =-(

You know "whore" on the 16th century... fine... i'll stop...

Response to AS: Movement w/ Boundaries 2005-12-17 15:46:17


At 12/17/05 03:32 PM, BobRicci wrote: I've only got 1 vote for that... =-(

You know "whore" on the 16th century... fine... i'll stop...

no... you probly wont... but that why we love you.

just sayin what we're all thinkin