Flash AS2 Movment and Collision tutorial (October 20th 2024)

Hi, hello! Today I'm gonna show you guys how to make something move and have it not phase through shit in Adobe flash using AS2, especially since I can't find good tutorials about it.
(Please keep in mind I'm not great at coding, this is just what I've learnt from forum posts and other people I've talked to)

First you're gonna have to have a movie clip and in the actions tab you put in
onClipEvent(load) {
speed = 10;
}
speed is gonna help give the movieclip the speed it needs, that's it.
next is to put in more code.

For the sake of the tutorial it will just be making the cube go to the right but to make it go left, up and down, the number in the parenthesis gets changed. Here's the key codes for moving all around (WASD)
left = 65
right = 68
up = 87
down = 83

onClipEvent(enterFrame) {
if(Key.isDown(68))
{
_root.PlayerRight = 1;
}
else
{
_root.PlayerRight = 0;
}

(To add more direction movement just copy and paste the whole Key is Down segment and change the keycode and PlayerDirection)
if you need to use other keys here's a Newgrounds forum topic to help you out.
I like to make the direction a variable, especially since it would help when making it work with collissions

Add in this bit of code under the whole Key.isDown segments
if (_root.PlayerRight == 1)
{
this._x += +speed;
}

(When making the player go up and down change the x to a y and to make it go the opposite direction change the + next to the speed to a -)
Now we go to the canvas and add a Box, make it a movieclip and in its properties, make the instance name Bounds (Can be a different name but for the sake of this tutorial it will be that)
After we do that, go back to the player code and in the PlayerRight code underneat the this._x += +speed code add in this:

if (_root.Bounds.hitTest(this._x,this._y,true))
{
this._x += -speed;
}

(Dont change or remove anything the hittest statement or the collission won't work)
once we run the flash file and we move to the right we can see that the player will now stop moving when it hits the wall.
Thanks for stopping by and have a good day :)

Full Code (I reccomend to just type it all down on your own)
onClipEvent(load) {
speed = 10;
}

onClipEvent(enterFrame) {
if(Key.isDown(68))
{
_root.PlayerRight = 1;
}
else
{
_root.PlayerRight = 0;
}

if(Key.isDown(65))
{
_root.PlayerLeft = 1;
}
else
{
_root.PlayerLeft = 0;
}

if(Key.isDown(87))
{
_root.PlayerUp = 1;
}
else
{
_root.PlayerUp = 0;
}

if(Key.isDown(83))
{
_root.PlayerDown = 1;
}
else
{
_root.PlayerDown = 0;
}

if (_root.PlayerRight == 1)
{
this._x += +speed;
if (_root.Bounds.hitTest(this._x,this._y,true))
{
this._x += -speed;
}
}

if (_root.PlayerLeft == 1)
{
this._x += -speed;
if (_root.Bounds.hitTest(this._x,this._y,true))
{
this._x += +speed;
}
}

if (_root.PlayerUp == 1)
{
this._y += -speed;
if (_root.Bounds.hitTest(this._x,this._y,true))
{
this._y += +speed;
}
}

if (_root.PlayerDown == 1)
{
this._y += +speed;
if (_root.Bounds.hitTest(this._x,this._y,true))
{
this._y += -speed;
}
}
}