Forum Topic: AS: Scrolling Background

(9,455 views • 54 replies)

This topic is 2 pages long. [ 1 | 2 ]

<< < > >>
Happy

DrDeath2k3

Reply To Post Reply & Quote

Posted at: 6/28/05 03:13 PM

DrDeath2k3 EVIL LEVEL 28

Sign-Up: 09/04/03

Posts: 1,511

This is something I am currently using in a game I am making.
(If you can help with some AS, please respond with your e-mail)

Ok, this is quite easy.

First, draw the background you want to scroll. Make sure it is very long or high, depending on the type of game.

Make the background a movieclip. (duh!)

Highlight it and go to it's action panel. (F9)

Put in this actionscript!

onClipEvent (enterFrame) {
// When the right key is down, it starts scrolling left.
if(Key.isDown(Key.RIGHT)) {
this._x=_x-10;
}
}
onClipEvent (enterFrame) {
// When the left key is down, it starts scrolling right.
if(Key.isDown(Key.LEFT)) {
this._x=_x+10;
}
}

//You can change the speed (the 10) to whatever you want.

Ok, if you want it to scroll up / down, simply change the Left / Right to Up / Down and the x's to y's.

This can be used in many types of games.

If you have any questions, just respond! = D


None

Xentron

Reply To Post Reply & Quote

Posted at: 6/28/05 03:17 PM

Xentron NEUTRAL LEVEL 06

Sign-Up: 03/05/05

Posts: 277

If you need any help, my email is xentron@gmail.com


None

Inglor

Reply To Post Reply & Quote

Posted at: 6/28/05 03:23 PM

Inglor NEUTRAL LEVEL 17

Sign-Up: 01/26/03

Posts: 10,948

THIS IS NOT THE EASY WAY TO DO BACKGROUND SCROLLING

you'll have a shitty time hitTesting it(the player) against stuff inside the background, you need to put the player inside the background if you want hitTests to work right

the easy way (for me at least) is to use a Scripted camera, instead of moving the "background" you move the camera and the player, it's easy, and simple

if you insist on not doing so, move the palyer and the background, that would work too...

trust me, I have some exp on this, scrollng only the BG is a pain in the ass... sorry for being so sure of my way being right :P I just had some painful experiance :(

on the player

onClipEvent(enterFrame){
if(Key.isDown(Key.RIGHT)){
this._x+=5;
_parent._x-=5;
}else if(Key.isDown(Key.LEFT)){
this._x-=5;
_parent._x+=5;
}else if(Key.isDown(Key.UP)){
this._y-=5;
_parent._y+=5;
}else if(Key.isDown(Key.DOWN)){
this._y+=5;
_parent._y-=5;
}else if(Key.isDown(Key.RIGHT)){
this._x+=5;
_parent._x-=5;
}
}


None

DrDeath2k3

Reply To Post Reply & Quote

Posted at: 6/28/05 03:28 PM

DrDeath2k3 EVIL LEVEL 28

Sign-Up: 09/04/03

Posts: 1,511

At 6/28/05 03:23 PM, Inglor wrote: BLEH

If you have MSN, I added you. I can send you the file and you can see how awesomeness it is.

...And I could use some help on it.


None

Inglor

Reply To Post Reply & Quote

Posted at: 6/28/05 03:30 PM

Inglor NEUTRAL LEVEL 17

Sign-Up: 01/26/03

Posts: 10,948

my msn is benjamingr@msn.com ring me


None

Denvish

Reply To Post Reply & Quote

Posted at: 6/28/05 03:32 PM

Denvish DARK LEVEL 46

Sign-Up: 04/25/03

Posts: 16,236

At 6/28/05 03:23 PM, Inglor wrote: on the player

onClipEvent(enterFrame){
if(Key.isDown(Key.RIGHT)){
this._x+=5;
_parent._x-=5;
}else if(Key.isDown(Key.LEFT)){
this._x-=5;
_parent._x+=5;
}else if(Key.isDown(Key.UP)){
this._y-=5;
_parent._y+=5;
}else if(Key.isDown(Key.DOWN)){
this._y+=5;
_parent._y-=5;
}else if(Key.isDown(Key.RIGHT)){
this._x+=5;
_parent._x-=5;
}
}

or using binary:

onClipEvent(load){spd=5;}
onClipEvent(enterFrame) {
_x-=Key.isDown(37)*spd;
_x+=Key.isDown(39)*spd;
_y-=Key.isDown(38)*spd;
_y+=Key.isDown(40)*spd;
_parent._x+=Key.isDown(37)*spd;
_parent._x-=Key.isDown(39)*spd;
_parent._y+-=Key.isDown(38)*spd;
_parent._y-=Key.isDown(40)*spd;
}

- - Flash - Music - Images - -

BBS Signature

None

Inglor

Reply To Post Reply & Quote

Posted at: 6/28/05 03:36 PM

Inglor NEUTRAL LEVEL 17

Sign-Up: 01/26/03

Posts: 10,948

I appreciate you paying attention to my binary tutorial :P (I know you probebly knew that before)

but using binary isn't a good idea since you can't do 'else if' with binary

and if you wanna do it binary, at least do it

_x+=(Key.isDown(Key.RIGHT)*spd+(Key.isDown(Key.LEFT)*(-spd);

it's even shorter ;)


None

T-H

Reply To Post Reply & Quote

Posted at: 6/28/05 03:43 PM

T-H LIGHT LEVEL 39

Sign-Up: 01/07/04

Posts: 4,893

This is the second shitty attempt at an AS: reference topic today, inglor and Denvish had to come along and salvage it. Don't make tutorials about obvious stuff (in this case shifting the bg using _x- / _x+. (OMFG liek, that so clever!11!))


None

Denvish

Reply To Post Reply & Quote

Posted at: 6/28/05 03:44 PM

Denvish DARK LEVEL 46

Sign-Up: 04/25/03

Posts: 16,236

The thing is, with that code, you don't need any else if's. In fact, I think it's better since it allows diagonal movement as well, with no key clashes. I understand what you're saying, though.

- - Flash - Music - Images - -

BBS Signature

None

Inglor

Reply To Post Reply & Quote

Posted at: 6/28/05 03:46 PM

Inglor NEUTRAL LEVEL 17

Sign-Up: 01/26/03

Posts: 10,948

fine so I remove one 'else' and I don't have any key clashes either :P I still get 3/4 of the average runspeed ;)


None

Kipling

Reply To Post Reply & Quote

Posted at: 7/24/05 08:51 AM

Kipling NEUTRAL LEVEL 13

Sign-Up: 05/04/04

Posts: 2,641

At 6/28/05 03:17 PM, Xentron wrote: If you need any help, my email is xentron@gmail.com

I'll keep that in mind next time I want your help. lol


None

Dipshitprod

Reply To Post Reply & Quote

Posted at: 10/8/05 08:24 AM

Dipshitprod LIGHT LEVEL 07

Sign-Up: 03/13/05

Posts: 390

At 6/28/05 03:43 PM, T-H wrote: This is the second shitty attempt at an AS: reference topic today, inglor and Denvish had to come along and salvage it. Don't make tutorials about obvious stuff (in this case shifting the bg using _x- / _x+. (OMFG liek, that so clever!11!))

Well, there are people who don't know stuff (the easy or hard way), and that's why forums like these exist..... also, Some improvements or easier ways are supposed to be written here, if you meant the opposite, yesterday I fixed a flaw on the basic movement AS =).... so.... don't just say something is a shitty attempt if you only know half the reason for it =/


None

Devenger

Reply To Post Reply & Quote

Posted at: 10/8/05 09:11 AM

Devenger NEUTRAL LEVEL 09

Sign-Up: 12/24/04

Posts: 1,103

Still, it's not a good idea to write an AS: unless it's pretty advanced, because all the simple stuff has been donezor'd. Maybe I'm utterly wrong, and Inglor will have to flame me for another page though. *yawn*

Instant newb satisfaction: play with _root._x and _root._y. :D I'll just leave now.

I'm back! on a temporary basis. No-one can remember who I am! but I don't really mind.


None

alexhall

Reply To Post Reply & Quote

Posted at: 11/4/05 11:18 AM

alexhall LIGHT LEVEL 10

Sign-Up: 07/25/04

Posts: 21

how do i get it to stop scroling when it is at the end?


None

rmthegreat88

Reply To Post Reply & Quote

Posted at: 11/4/05 11:22 AM

rmthegreat88 LIGHT LEVEL 10

Sign-Up: 04/17/04

Posts: 1,314

At 11/4/05 11:18 AM, papperboy wrote: how do i get it to stop scroling when it is at the end?

put like an ending wall there and once the hero runs into it, go to a new frame.

kinda like a mario game


None

Claxor

Reply To Post Reply & Quote

Posted at: 11/4/05 11:42 AM

Claxor DARK LEVEL 10

Sign-Up: 10/21/05

Posts: 2,465

At 6/28/05 03:32 PM, Denvish wrote: binary stuff

Thats also a way to do it.

BBS Signature

None

neowolf22964

Reply To Post Reply & Quote

Posted at: 11/6/05 11:10 AM

neowolf22964 EVIL LEVEL 08

Sign-Up: 05/01/04

Posts: 190

Yep that is the right way to do it.
Could you tell me how to do that but at the same time Making the charecters legs move and Wot ever?


Happy

HPen

Reply To Post Reply & Quote

Posted at: 11/15/05 03:03 PM

HPen LIGHT LEVEL 02

Sign-Up: 11/15/05

Posts: 79

okay.... but if the map/world is moving how do you made that if the car hittest a bounce the maps speed = -20?


None

Vengeance

Reply To Post Reply & Quote

Posted at: 11/15/05 03:12 PM

Vengeance EVIL LEVEL 28

Sign-Up: 03/18/05

Posts: 5,035

wtf do you mean, use proper punctuation

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

BBS Signature

None

HPen

Reply To Post Reply & Quote

Posted at: 11/15/05 03:14 PM

HPen LIGHT LEVEL 02

Sign-Up: 11/15/05

Posts: 79

wtf do you mean with that proper punctuation


None

Vengeance

Reply To Post Reply & Quote

Posted at: 11/15/05 03:24 PM

Vengeance EVIL LEVEL 28

Sign-Up: 03/18/05

Posts: 5,035

At 11/15/05 03:03 PM, HPen wrote: okay.... but if the map/world is moving how do you made that if the car hittest a bounce the maps speed = -20?

well, i re-read it (im tired at the moment its 6:00AM and i just woke up) and sorry for the punctuation comment.
put this in the car MC.
onClipEvent(enterFrame){
if(this.hitTest(_root.bumpMC)){
_root.car.mapspeed-=20 //change _root.car.mapspeed to whatever variable its saved to and the source thats its saved to.
}}

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

BBS Signature

None

liaaaam

Reply To Post Reply & Quote

Posted at: 11/15/05 03:27 PM

liaaaam NEUTRAL LEVEL 22

Sign-Up: 12/11/04

Posts: 14,528

Something easay:

onMouseMove=function(){
xspd = (_xmouse-x1), yspd = (_ymouse-y1);
_x+=xspd, _y+=yspd;
x1=_xmouse,y1=_ymouse;
}


None

spawn28

Reply To Post Reply & Quote

Posted at: 11/15/05 05:17 PM

spawn28 EVIL LEVEL 14

Sign-Up: 03/15/04

Posts: 468

lol, this is not hard to do, what is hard is to get the bg to only move when your character is at the edge of the screen, and then the bg moves... and the bg stops at end of the screen..... so that the screen doesnt go blank and that the bg doesnt go off screen. i will upload my current file to uhhh image shack and show u what i mean.


None

spawn28

Reply To Post Reply & Quote

Posted at: 11/15/05 05:23 PM

spawn28 EVIL LEVEL 14

Sign-Up: 03/15/04

Posts: 468

okay heres a really complicated scrolling bg and moving character.
http://img490.images..ge=narutogame0mq.swf

ignore the thing saying read this AS, as it is just a note to self.
this consists of a charater (idle, and walking animations),
4 invisible boxes that are their to tell the script when to stop the bg and crap.
1 bg.
and 150+ lines of code just in that.


None

DemitriW

Reply To Post Reply & Quote

Posted at: 11/26/05 07:27 AM

DemitriW EVIL LEVEL 12

Sign-Up: 10/09/05

Posts: 70

this is good but i want scrolling background for a movie not a game


None

Chris

Reply To Post Reply & Quote

Posted at: 12/3/05 09:04 AM

Chris LIGHT LEVEL 18

Sign-Up: 09/17/04

Posts: 301

At 6/28/05 03:23 PM, Inglor wrote: on the player

onClipEvent(enterFrame){
if(Key.isDown(Key.RIGHT)){
this._x+=5;
_parent._x-=5;
}else if(Key.isDown(Key.LEFT)){
this._x-=5;
_parent._x+=5;
}else if(Key.isDown(Key.UP)){
this._y-=5;
_parent._y+=5;
}else if(Key.isDown(Key.DOWN)){
this._y+=5;
_parent._y-=5;
}else if(Key.isDown(Key.RIGHT)){
this._x+=5;
_parent._x-=5;
}
}

how would i reset the camera's position after this? you know so the camera doesn't stay over there when you goto a new frame.


None

kitsunez

Reply To Post Reply & Quote

Posted at: 12/20/05 05:29 PM

kitsunez NEUTRAL LEVEL 01

Sign-Up: 07/31/03

Posts: 17

I'm full of questions......but anyways.....as i'm lookingto do a final fight type game I want to know if it is possible to have a background taht scrolls only after you completed an objective like defeat all the enemies on the screen and it's lock in place till when another objective came up....it's all kinda overwhelming for me but i'm trying


None

icarus

Reply To Post Reply & Quote

Posted at: 12/29/05 05:02 AM

icarus EVIL LEVEL 12

Sign-Up: 01/10/05

Posts: 712

I made a game where i needed scrolling, but i wanted the player to be centered when it was scrolling so all i did was once the players _x was >= 1/2screen width every time they moved _root._x += 1 and player._x -= 1.
This is also alot bettre i believe, because it moves evrything besides the player, like enemys, not just the background.


None

TrueDarkness

Reply To Post Reply & Quote

Posted at: 12/29/05 11:04 AM

TrueDarkness EVIL LEVEL 27

Sign-Up: 08/31/04

Posts: 4,718

I think it would be easiest if you were to make the background follow the player and scroll as it does. It's simple.
Give your character the scripts for him to move left and right and give him an instance name such as "player"
Then on the background just add a script like...
onClipevent(enterFrame){
this._x=_root.player._x/5
}

You can change /5 to any number that suits you and the game :)


None

aquaticmole

Reply To Post Reply & Quote

Posted at: 2/5/06 11:35 AM

aquaticmole LIGHT LEVEL 28

Sign-Up: 02/10/05

Posts: 7,908

At 12/3/05 09:04 AM, IS_ChRiS wrote: how would i reset the camera's position after this? you know so the camera doesn't stay over there when you goto a new frame.

thats a question i thought about and tryed to solve but couldn't, could someone help us out?

Please?

aquaticmole.

BBS Signature

All times are Eastern Standard Time (GMT -5) | Current Time: 02:19 PM

<< Back

This topic is 2 pages long. [ 1 | 2 ]

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!