Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > General > Gaming and Game Development

Reply
 
Thread Tools Rate Thread Display Modes
Old 06-01-2012, 05:14 AM   #1
Mr.Cheez
Registered User
 
Join Date: Jun 2012
Location: Bay Area, CA
Posts: 3
Default [AS3] Scrolling BG when player walks

Hi everyone,

I'm very new to AS 3.0 (only been working w/ it for a week), but very well versed in animating using Flash. I'm interested in getting into creating games using AS 3.0, and so far I've been finding it a lot of fun.

Many apologies if a solution to my issue has been brought up before, but this is what I'm attempting to do:

- I have a character that stands in the center of the screen.

- I have a background graphic of mountains that is as wide as the screen is.


- When the user presses the right or left arrow keys, the character should go into a walk animation, and the background graphic should move along the x axis accordingly. So far, so good. The key bindings work correctly, and the background graphic indeed moves accordingly.


PROBLEM:

I've read some online tutorials that detailed how you can create two instances of the same background graphic, and put them side-by-side using actionscript. I used that code, but it's not working; they are actually spawning *on top of one another*.

this is the code I used:

ActionScript Code:
var bg1:BG1; var bg2:BG1; bg1 = new BG1(); bg2 = new BG1(); bg1.x = 0; bg2.x = bg1.width;

Is there something wrong with how I wrote that? It seems like .width is not working the way I imagined it to. What I would like to do is have the code put instances of the BG graphics beside itself on the stage, to give the illusion of one continuously scrolling background.

this is the code for that scrolling:
ActionScript Code:
public function tileBG(e:Event):void                     {                                                    bg1.x += xScrollSpeed;                         bg2.x += xScrollSpeed;                                                 if ( bg1.x < -bg1.width )                             {                                 bg1.x = bg1.width;                             }                         else if ( bg2.x < -bg2.width )                             {                                 bg2.x = bg2.width;                             }                     }

Any ideas on what I'm doing wrong? It's had me stumped for over a week!

Last edited by Mr.Cheez; 06-08-2012 at 07:31 PM.
Mr.Cheez is offline   Reply With Quote
Old 06-02-2012, 09:51 PM   #2
Mr.Cheez
Registered User
 
Join Date: Jun 2012
Location: Bay Area, CA
Posts: 3
Default

*deleted previous post*---learning new things, this post is no longer relevant. Still would appreciate any insights! Not looking for code, just any pointers to get me on the right track. Much appreciated...

Last edited by Mr.Cheez; 06-08-2012 at 07:34 PM.
Mr.Cheez is offline   Reply With Quote
Old 06-09-2012, 07:16 AM   #3
Scearezt
Satisfied User
 
Scearezt's Avatar
 
Join Date: Jun 2010
Location: C:\\System32\Hungary
Posts: 426
Lightbulb

Well, if the character is moving, then the position of the backgrounds will change.
ActionScript Code:
bg1.x = 0; bg2.x = bg1.width;
This should be something like this:
ActionScript Code:
bg1.x = player.x; bg2.x = player.x+bg1.width;
And ofcourse:
ActionScript Code:
if ( bg1.x < player.x-bg1.width )                             {                                 bg1.x = player.x+bg1.width;                             }                         else if ( bg2.x < player.x-bg2.width )                             {                                 bg2.x = player.x+bg2.width;                             }
Because we need to place them in front of the PLAYER.
Scearezt is offline   Reply With Quote
Old 06-11-2012, 06:07 AM   #4
Mr.Cheez
Registered User
 
Join Date: Jun 2012
Location: Bay Area, CA
Posts: 3
Default

Hi Scearezt, thanks for the tip!

Actually, since the player doesn't really "move" in the literal sense, I'm not sure if they bg need to follow the player at all? It just needs to move in a way that creates the *illusion* that the character is moving. Do you see what I mean?
Mr.Cheez is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 09:55 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.