Hmm, an easier way to sidescroll.
Create a new layer called vCam, now get vCam and place it on the stage. Give it these properties:
W: Width of your Flash
H: Height of your Flash
X: 0.0
Y: 0.0
Give the vCam an instance name of vCam. Now lock the vCam layer. Create a new layer called player and on there, draw your player standing still facing right. Convert it to a movieclip, what you actually call the movieclip is up to you, but give the player an instance name of player. On the movieclip's frame 2, convert it to a movieclip and animate it walking/running right. Now for the actions. On the player's movieclip frame 1, click the frame and press F9, now enter stop(); or drag in the stop action from the actions library. Close the actions window or press F9 again and go to the main scene (also known as _root)
and on there select your player and give him this code:
onClipEvent (load) {
var speed = 5;
var mm;
_root.mm = "still";
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.LEFT)) {
this._x -= speed;
_root.vCam._x -= speed;
gotoAndStop(2);
this._xscale = -100;
_root.mm = "left";
}
if (Key.isDown(Key.RIGHT)) {
this._x += speed;
_root.vCam._x += speed;
gotoAndStop(2);
this._xscale = 100;
_root.mm = "right";
}
if (!Key.isDown(Key.RIGHT) && !Key.isDown(Key.LEFT)) {
if (_root.mm == "left") {
gotoAndStop(1);
this._xscale = -100;
_root.mm = "still";
} else if (_root.mm == "right") {
gotoAndStop(1)
this._xscale = 100;
_root.mm = "still";
}
}
}
That also shows you how useful variables are. Without variables, that script wouldnt work properly.
THE CODE REVEALED
Time to reveal the code.
Variables:
speed - Change this as much as you like, this defines the speed of the character.
mm - This is the movement variable, this is set to left/right/still depending on how you are moving.
_root.mm = "still" - I know, I know, I could just put var mm = "still" but that didnt seem to work. Because you start off standing still, the mm variable is set to still.
If Tests:
if (Key.isDown(Key.LEFT)) {
this._x -= speed;
_root.vCam._x -= speed;
gotoAndStop(2);
this._xscale = -100;
_root.mm = "left";
}
If the left arrow key is down, the player's _x decreases by speed's value (default being 5) and it goes to its frame 2. Then it sets its xscale to -100. This flips the character horizontally, and if the animation is of it walking right then it flips it to walk left. Then it sets our movement variable to left, this is just to identify your direction. I'll just give you a brief description of the other key.
Instead of subtracting the x by speed it adds it, instead of the xscale setting to -100 it sets to 10, instead of the movement variable being left it is right.
if (!Key.isDown(Key.RIGHT) && !Key.isDown(Key.LEFT)) {
if (_root.mm == "left") {
gotoAndStop(1);
this._xscale = -100;
_root.mm = "still";
} else if (_root.mm == "right") {
gotoAndStop(1)
this._xscale = 100;
_root.mm = "still";
}
}
This checks if the right and left arrow keys are both up and if they are then it runs another if test to check if the movement variable is left and if so it goes to its frame 1 and its xscale is set to -100 the movement variable is set to still but if not then it does the same but the xscale is 100. Put your mind to the test and try to figure out the crouching script for yourself. I'll post a crouching script in a sec if you cant figure it out.