Best viewed in Firefox

Awesty Productions

Making a Scrolling Background

April 15th, 2007 by awesty

This has been requested by a few people so I thought I will write it. In this tutorial you will learn how to make a scrolling background. I am using Macromedia/Adobe Flash 8 but Flash MX 2004 will work as well. It would help if you knew some basic actionscript since I am not going to go as in depth with the code as I usually might since this is alot like a few of the other tutorials.

Click here to see an example of what we will be making. Arrow Keys to move.

First of all open flash and draw the ground that you want to scroll and the character that will be walking. Make them each MC’s (MovieClips). Give the ground an instance name of ‘ground’ and the character an instance name of ‘man’.

Making the Movie Clips

Make sure the mans registration point is in the top left corner. Now paste this code on the frame.

onEnterFrame = function () {
    if (!_root.ground.hitTest(_root.man._x, _root.man._y+(man._height/2), true)) {
        if (Key.isDown(Key.LEFT)) {
            _root.ground._x += 3;
        }
    }
    if (!_root.ground.hitTest(_root.man._x+man._width, _root.man._y+(man._height/2), true)) {
        if (Key.isDown(Key.RIGHT)) {
            _root.ground._x -= 3;
        }
    }
};

The first line means ‘When this frame is entered’. The second line is an if statement, checking if the left side of the square is not hitting the ground, if the statement is false flash will do nothing. If it is true, flash will make the ground move right, so it appears that the man is moving left. The 3rd line is checking if the Left arrow key is down, and the 4th is making the ground move left (like I just said) if the 3rd line is true.
Lines 7-9 are exactly the same as the previous ones except checking if the right side is hitting the ground and making the ground move left.

Now if you test your movie you should have a scrolling background when you move left or right. But if your MC hits a wall in the background it will stop scrolling.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • blogmarks
  • del.icio.us
  • digg
  • Furl
  • MyShare
  • NewsVine
  • Netscape
  • Reddit
  • Simpy
  • Slashdot
  • StumbleUpon
  • Technorati
  • YahooMyWeb

RSS feed | Trackback URI

36 Comments »

Comment by KIM
2007-04-16 13:42:05

kinda explains alot in the problems i had… thnx

 
Comment by KIM
2007-04-16 13:47:33

wheres the tutorial request thingy? i cant find it… well i can but i can type anything on it theres no text box!..

i wanr to request a tutorial…

category: flash

i want a tutorial about graphics in flash or if not, teach us about filter

(like: bevel, gradient bevel, glow, gradient gloe, drop shadow and others)

thnx alot

Comment by lonstile Subscribed to comments via email
2007-04-23 13:32:23

great request! I want it too!

 
Comment by awesty
2007-04-23 17:34:47

I have no idea what happened to the tutorial request thing.

With filters and such there is not really anything to teach. Flash gives you a few settings for each and each one is very self explanatory. If you wanted filters through actionscript however that would get a bit more advanced.

Comment by luke Subscribed to comments via email
2007-09-27 11:30:50

please tell me if theres a way i can do this without the registration point in the top left corner, it wont let me put it anywhere but the middle.

So when i push left or right it doesn’t do anything

 
 
 
 
Comment by alex Subscribed to comments via email
2007-04-24 04:18:38

It doesnt work for me for some reason. i copied everything exactly as it is, and it doesnt work.

 
Comment by alex Subscribed to comments via email
2007-04-24 04:19:41

nevermind, i got it to work

Comment by johnpinkertin Subscribed to comments via email
2007-04-26 09:05:46

realy? i cant get mine to work, how did you do iy?

 
 
Comment by fredrich
2007-04-24 04:30:21

How do i make it so the man can go up or down? ( i dont mean jump ). I already tried copying the code thats already there and changing it so it increases or decreases the y position, not x. This works, but it does not stop moving when i hit a block. Please help.

Comment by awesty
2007-04-24 20:01:02

Well you would also need up and down hitTests.

 
 
Comment by Kurt Subscribed to comments via email
2007-04-28 23:40:50

if it hits a wall for me it would go trough i will fix that bug and do you know what you would do to jump???

Comment by awesty
2007-04-30 16:38:35
 
 
Comment by Johnpinkertin Subscribed to comments via email
2007-04-29 11:30:44

wow great tutorial, but i have a ? could you give me the code that lets me move the backround from a birds eye view?

Comment by awesty
2007-04-30 16:31:54

You would use the same code as you did for the left and right but change the keys. Check out this tutorial:

http://www.awestyproductions.c.....-tutorial/

 
 
Comment by luccaped Subscribed to comments via email
2007-04-29 22:19:57

I’d like to request a tutorial for flash.

how do you do a scrolling background but the ‘man’ MC can jump and if ‘man’ doesn’t touch the ‘ground’ MC he falls like if he had fallen in a pit? thanks

Comment by awesty
 
 
Comment by austin Subscribed to comments via email
2007-05-03 08:01:29

i am making a tank game…and like…the walls on the left side don’t block my tank, it goes through it a ways…and then stops. i made sure all of my stuff was correct and it just doesn’t work. here is the swf: http://www.swfup.com/uploads/swf-9413.swf, too lazy to upload it to my own site i am :). see how it messes up on the left side? anyway…i need help…

Comment by Kurt Subscribed to comments via email
2007-05-04 11:06:13

Well Austin I don’t know how to fix it but I know the problem the registration point turns around like the image so the point goes from left to right so when you press the left key the point turns around so the whole thing would have to go through the wall until it stops I will try to fit code together like Awesty to make it so the point doesn’t turn around.

Comment by austin Subscribed to comments via email
2007-05-04 11:19:05

thanks, i might just go find a different code or make my own i guess since this code is very limited. oh well. i just don’t understand why the registration point has to be in the top left…i’m sure it is possible to modify it to work while it is in the middle

Comment by awesty
2007-05-04 13:38:14

You put the registration point in the right spot, but since the tank turns around the registration point switches sides. You need to put the registration point in the middle, but then just change some of the hitTests.

Comment by austin Subscribed to comments via email
2007-05-04 22:46:27

thats what i was totally thinking. what would i change the hittests to? i have been reading over the tutorial and i just can’t find the logic in what you already have. could you explain it?

(Comments wont nest below this level)
 
 
 
 
 
Comment by Mike
2007-05-03 12:59:31

Buddy you just made my week! thanks a lot!

 
Comment by Tanzil Ahmed Subscribed to comments via email
2007-05-03 22:10:34

Hii frnd This is Tanzil frm Mumbai.I like making animation but also like to work in flash.now i didn’t start making game but i reading yur site getting ideas.soon i start making game.

 
Comment by Calum Subscribed to comments via email
2007-06-26 05:44:48

Hi Awesty,

I am trying to make a game like this one http://www.addictinggames.com/stickrpg.html And what I need is the code on how to do the birds-eye view, also to make the man sop exactly on the wall without going through it or anythin.

Thanks and plz plz plz reply

Calum :)

P.S. I am most likely to forget about this post so please email the code or instructions or whatever to me. thaks alot:) Also ur tutorials ROCK! lol

 
Comment by Calum Subscribed to comments via email
2007-06-28 04:00:04

Awesty??? Why havn’t you replied???

 
Comment by SaroVati Subscribed to comments via email
2007-07-02 01:15:43

I’m pretty sure awesty is taking a break of some sort. If you look at other tutorial comments, his last post was on the 15th. Anyways, for ur question, type this code on your man:

onClipEvent(load) {
moveSpeed = 8;
}
onClipEvent(enterFrame) {
if (_root.wall.hitTest(_x+(_width/2), _y, true)) {
this._x -= moveSpeed;
}
if (_root.wall.hitTest(_x-(_width/2), _y, true)) {
this._x += moveSpeed;
}
if (_root.wall.hitTest(_x, _y+(_height/2), true)) {
this._y -= moveSpeed;
}
if (_root.wall.hitTest(_x, _y-(_height/2), true)) {
this._y += moveSpeed;
}
}

Put the instance name of your wall to ‘wall’ without the quotation marks.

Comment by awesty
2007-07-03 18:49:27

I AM BACK! :D

And yea, that should work.

 
 
Comment by Calum Subscribed to comments via email
2007-07-03 21:45:12

That code makes it worse :(

Ok here is what I have done…

I have added the code to the frame that Awesty has displayed in his tutorial.

BUT I want to make my game a birds-eye view so i need the walls to move up, down, left, right, diagonal etc.

Also when the man hits the walls I want the maze to stop instead of it still moving and pushing the man away.

Also if you need an idea of what I am trying to make go to http://www.addictinggames.com/stickrpg.html .

Thanks and please help I am dying to know what to do.

Also if you want to, you can email me instructions to calum[@]prositedesigner[.]co[.]uk just remove the brackets “[ and ]”.

Thanks :)

 
Comment by Bobby Subscribed to comments via email
2007-07-15 02:38:16

Awesty, I am making a game an your tutorial was VERY helpful to what I’m trying to do. But is there a way that you can show me to make it scroll not only left and right, but up and down?

Thanks

 
Comment by Bobby Subscribed to comments via email
2007-07-15 07:12:55

Also, could you explain how to keep some other items (example: enemies, coins, ect.) looking stationary rather than appearing to run away?

 
Comment by Calum Subscribed to comments via email
2007-07-31 03:18:02

Awesty… where are you? on another holiday???

 
Comment by Dyan Subscribed to comments via email
2007-12-18 16:43:34

this is good but how to make the ground move in top view

 
Comment by Rhys
2008-03-23 03:45:56

Hi Thanks for this tutorial it was brilliant. I have been on loads of tutirals about moving the background but could never understand them your one worked the first time and i understansd it! so thanks and keep it up :)

 
Comment by antonette Subscribed to comments via email
2008-05-30 13:00:55

hope u cn help me about dis flash thing i need to study this 4 my tesis its about network adventure gaming., cn u add me hirs my add antonette_mine03 thnx po

 
Comment by Courtney Subscribed to comments via email
2008-06-19 10:11:47

So I’ve got a couple weird issues… and its very possible I’m just being a noob

First(and kinda off topic) when I create a symbol I dont have he option to pick the registration point… is it just a weird verson I have or do you have to tick some sort of box?

Second… I did everything (except the registration, since I cant find it) but when I ctrl-enter I get a blank window, no errors…

Help?

 
Name (required)
E-mail (required - never shown publicly)
URI
Subscribe to comments via email
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong> in your comment.