00:00
00:00
Newgrounds Background Image Theme

JustAndrew 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: Basic Sidescrolling and moving!

1,589 Views | 19 Replies
New Topic Respond to this Topic

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.

Response to AS: Basic Sidescrolling and moving! 2006-06-03 16:33:19


I hope you like it, it took me alot of time to figure it out and alot of time to write it too. =P

Response to AS: Basic Sidescrolling and moving! 2006-06-03 16:35:24


nice code =)

Response to AS: Basic Sidescrolling and moving! 2006-06-03 16:37:16


At 6/3/06 04:35 PM, xXmastermindXx wrote: nice code =)

Thanks, I'm not gonna post again coz of the max 4 posts per topic and I wouldnt be able to post that crouching script I said I'd post.

Response to AS: Basic Sidescrolling and moving! 2006-06-03 17:26:50


Already been covered, also no link to either AS: Main or the VCam download page.


- - Flash - Music - Images - -

BBS Signature

Response to AS: Basic Sidescrolling and moving! 2006-06-03 17:58:07


Change your current code to this for crouching and create a keyframe on frame 3 of the player crouching right.
onClipEvent (load) {
//create variables
var speed = 5;
var mm;
//set variables
_root.mm = "still";
}
//start on handler
onClipEvent (enterFrame) {
//begin the left movement test
if (Key.isDown(Key.LEFT) && !Key.isDown(Key.DOWN)) {
//decrease the _x and go left
this._x -= speed;
//decrease the vCam's _x and make it go left too
_root.vCam._x -= speed;
//go to the walking frame
gotoAndStop(2);
//flip the player horizontally
this._xscale = -100;
//change our movement variable to left just to identify direction
_root.mm = "left";
}
//begin the right movement test
if (Key.isDown(Key.RIGHT) && !Key.isDown(Key.DOWN)) {
//increase the _x and go right
this._x += speed;
//increase the vCam's _x and make it go right too
_root.vCam._x += speed;
//go to the walking frame
gotoAndStop(2);
//flips player horizontally
this._xscale = 100;
//change our movement variable to right just to identify direction
_root.mm = "right";
}
//begin our standing still test
if (!Key.isDown(Key.RIGHT) && !Key.isDown(Key.LEFT) && !Key.isDown(Key.DOWN)) {
//begin our standing left test
if (_root.mm == "left") {
//make our player go to its first frame
gotoAndStop(1);
//flip our player horizontally
this._xscale = -100;
//change variable to still.left
_root.mm = "still.left";
//begin the else if test
} else if (_root.mm == "right") {
//goes to frame 1
gotoAndStop(1);
//flip our player horizontally
this._xscale = 100;
//change variable to still.right
_root.mm = "still.right";
}
}
//begin crouching script
if (Key.isDown(Key.DOWN) && !Key.isDown(Key.LEFT) && !Key.isDown(Key.RIGHT)) {
//check our movement variable
if (_root.mm == "still.left") {
//goes to our crouching frame
gotoAndStop(3);
//flips player
this._xscale = -100;
//set variable to crouching.left
_root.mm = "crouching.left";
//begin else if test
} else if (_root.mm == "still.right") {
//go to crouch frame
gotoAndStop(3);
//flips player
this._xscale = 100;
//set variable to crouching.right
_root.mm = "crouching.right";
}
}
//begin new if test
if (Key.isDown(Key.DOWN) && Key.isDown(Key.RIGHT)) {
gotoAndStop(1);
this._xscale = 100;
_root.mm = "still.right";
}
if (Key.isDown(Key.DOWN) && Key.isDown(Key.LEFT)) {
gotoAndStop(1);
this._xscale = -100;
_root.mm = "still.left";
}
//begin stand script
if (Key.isDown(Key.UP)) {
//begin new if test
if (_root.mm == "crouching.right") {
//go to first frame
gotoAndStop(1);
//flip player
this._xscale = 100;
//set variable
_root.mm = "still.right";
//begin else if test
} else if (_root.mm == "crouching.left") {
//go to first frame
gotoAndStop(1);
//flip player
this._xscale = -100;
//set variable
_root.mm = "still.left";
}
//begin else if test
}
}

Links:
Results:
View result
Source files:
Flash MX 6
Flash 5

Response to AS: Basic Sidescrolling and moving! 2006-06-03 18:00:13


At 6/3/06 05:26 PM, Denvish wrote: Already been covered, also no link to either AS: Main or the VCam download page.

I forgot the link to AS Main, the vCam is in the source files and so what if its been covered, this is a different way of doing it.

Response to AS: Basic Sidescrolling and moving! 2006-06-03 18:01:46


Also, the '//begin else if test' at the bottom of the code is just because I removed an else if test from the code and I didnt remove that on accident, so ignore that.

Response to AS: Basic Sidescrolling and moving! 2006-06-03 18:03:44


Response to AS: Basic Sidescrolling and moving! 2006-06-03 18:35:04


Oh a new bug was found, hold left and right at the same time and it walks on the spot. So what do we do? Just make a new keyframe on frame 4 of the movieclip after your crouching frame and draw your character facing the screen and put this code on him:
if (Key.isDown(Key.LEFT) && Key.isDown(Key.RIGHT)) {
gotoAndStop(4);
this._xscale = 100;
}

Response to AS: Basic Sidescrolling and moving! 2006-06-03 20:44:48


At 6/3/06 07:09 PM, pyro111 wrote: You really should take all the bugs out before posting your code.

Yah, and this is actually a bit embarassing to people who called him a newb.

Response to AS: Basic Sidescrolling and moving! 2006-06-03 21:21:23


Make a movieclip and put these keyframes on it (and always draw it facing right else it wont work):
KEYFRAME ONE - Player standing still.
KEYFRAME TWO - Player walking.
KEYFRAME THREE - Player crouching.
KEYFRAME FOUR - Player looking at the screen. This is an exception to facing right.
Now go to the main scene, and change Layer 1 to player. Add a new layer ontop called vCam;
download the vCam from the links and drag it onto the stage. Set its width and height to that of the movie, the default is 550 x 400 so you would make its width 550 and its height 400. Set both its X and its Y to 0.0. Give it an instance name of vCam, then lock and hide the vCam layer so it doesnt get in your way. Drag the player from the libary onto the stage; put it on the player layer. Give it an instance name of player and put these actions on him:

onClipEvent (load) {
//create variables
var speed = 5;
var mm;
//set variables
_root.mm = "still";
}
//start on handler
onClipEvent (enterFrame) {
//begin the left movement test
if (Key.isDown(Key.LEFT) && !Key.isDown(Key.DOWN)) {
//decrease the _x and go left
this._x -= speed;
//decrease the vCam's _x and make it go left too
_root.vCam._x -= speed;
//go to the walking frame
gotoAndStop(2);
//flip the player horizontally
this._xscale = -100;
//change our movement variable to left just to identify direction
_root.mm = "left";
}
//begin the right movement test
if (Key.isDown(Key.RIGHT) && !Key.isDown(Key.DOWN)) {
//increase the _x and go right
this._x += speed;
//increase the vCam's _x and make it go right too
_root.vCam._x += speed;
//go to the walking frame
gotoAndStop(2);
//flips player horizontally
this._xscale = 100;
//change our movement variable to right just to identify direction
_root.mm = "right";
}
//begin our standing still test
if (!Key.isDown(Key.RIGHT) && !Key.isDown(Key.LEFT) && !Key.isDown(Key.DOWN)) {
//begin our standing left test
if (_root.mm == "left") {
//make our player go to its first frame
gotoAndStop(1);
//flip our player horizontally
this._xscale = -100;
//change variable to still.left
_root.mm = "still.left";
//begin the else if test
} else if (_root.mm == "right") {
//goes to frame 1
gotoAndStop(1);
//flip our player horizontally
this._xscale = 100;
//change variable to still.right
_root.mm = "still.right";
}
}
//begin crouching script
if (Key.isDown(Key.DOWN) && !Key.isDown(Key.LEFT) && !Key.isDown(Key.RIGHT)) {
//check our movement variable
if (_root.mm == "still.left") {
//goes to our crouching frame
gotoAndStop(3);
//flips player
this._xscale = -100;
//set variable to crouching.left
_root.mm = "crouching.left";
//begin else if test
} else if (_root.mm == "still.right") {
//go to crouch frame
gotoAndStop(3);
//flips player
this._xscale = 100;
//set variable to crouching.right
_root.mm = "crouching.right";
}
}
//begin new if test
if (Key.isDown(Key.DOWN) && Key.isDown(Key.RIGHT)) {
gotoAndStop(1);
this._xscale = 100;
_root.mm = "still.right";
}
if (Key.isDown(Key.DOWN) && Key.isDown(Key.LEFT)) {
gotoAndStop(1);
this._xscale = -100;
_root.mm = "still.left";
}
//begin stand script
if (Key.isDown(Key.UP)) {
//begin new if test
if (_root.mm == "crouching.right") {
//go to first frame
gotoAndStop(1);
//flip player
this._xscale = 100;
//set variable
_root.mm = "still.right";
//begin else if test
} else if (_root.mm == "crouching.left") {
//go to first frame
gotoAndStop(1);
//flip player
this._xscale = -100;
//set variable
_root.mm = "still.left";
}
}
if (Key.isDown(Key.LEFT) && Key.isDown(Key.RIGHT)) {
gotoAndStop(4);
this._xscale = 100;
}

This script has been tested and I havent found a bug yet. Just combine this with some collision detection, gameplay and a background (put backgrounds on the bottom) and you could get a pretty neat side-scroller.

Response to AS: Basic Sidescrolling and moving! 2006-06-03 21:25:09


(put backgrounds on the bottom)

I mean make a layer underneath the player called background with the background things on it.

Response to AS: Basic Sidescrolling and moving! 2006-06-11 08:21:56


Pretty much basic... And you should have searched for buggs and added the extra codes in the main post before even makeing this thread.. And you forgot to link to AS: Main.. And this has been covered before but better with a jumping script to..

Response to AS: Basic Sidescrolling and moving! 2006-06-11 08:27:29


Am i the only one who has a problem with him posting 5 times in a row.
seriously, the script is buggy and crap. There's already a better tut on this. I could name 5 better ways of doing this. Stop posting in this topic, leave it to die.


========|| WWWWWWWW>[-[Blog] - [Audio] - [Userpage] - [Flash] - [Last.fm]-]<WWWWWWWW ||========

BBS Signature

Response to AS: Basic Sidescrolling and moving! 2006-06-11 08:41:31


already a better tut

Where is that tut?

Response to AS: Basic Sidescrolling and moving! 2006-06-11 08:49:26


I would never use a v-cam for scrolling. BAD IDEA.

Response to AS: Basic Sidescrolling and moving! 2006-07-07 19:02:46


At 6/11/06 08:49 AM, RyanPridgeon wrote: I would never use a v-cam for scrolling. BAD IDEA.

Shut up. Look, its a GOOD IDEA coz you can MESS AROUND with the VCAM okay? And its EASIER! >:(

Response to AS: Basic Sidescrolling and moving! 2006-07-07 19:04:45


Its also good because you dont have to move the whole background, that would mean you would have to convert it to a symbol. And what if you didnt want to?

Response to AS: Basic Sidescrolling and moving! 2006-07-07 19:06:26


I am not saying its the best in the word method, but its pretty similar to other scrolling tuts.

PS Sorry for Triple Post