Get the latest Education e-news
 

Go Back   Game Career Guide Forums > Programming
Forum Home Register Members List Mark Forums Read

Reply
 
Thread Tools Display Modes
Old 02-14-2011, 12:43 PM   #1
Killerapathy
Junior Member

Activity Longevity
0/20 14/20
Today Posts
0/11 sssssss23
Default Scrolling Background Question

Hey everyone,

I'm curious about how scrolling backgrounds work. I'd assume that the texture (background) rendering rectangle is modified to emulate scrolling but how do you make the player appear to be moving to the left while the background scrolls to the right.

I'm trying to implement the feature of the scrolling background in an XNA game I'm working on. When the player hits the left edge of the screen, they lose the game.

Example games: The Impossible Game, Robot Unicorn Attack
Killerapathy is offline   Reply With Quote
Old 02-14-2011, 01:07 PM   #2
Protector one
Moderator

Activity Longevity
2/20 17/20
Today Posts
1/11 ssssss692
Location: Netherlands
Default

When you have the scrolling background, I expect you just need to animate the player character.
__________________
My name is Louis. Hi!
Uh oh! The princess is in another signature!
Protector one is online now   Reply With Quote
Old 02-14-2011, 10:13 PM   #3
Killerapathy
Junior Member

Activity Longevity
0/20 14/20
Today Posts
0/11 sssssss23
Default

Here's some pseudo code, what do you think?
Code:
int nBGOffset = 0;
RECT drawRect
Texture texBG;

Update(float _dt)
{
     nBGOffset += 40*  _dt;
     if(nBGOffset > texBG.Width)
          nBGOffset = 0;

     Player.Position.X -= 40* _dt
}

Render()
{
      drawRect.x = nBGOffset;
      texBG.Draw(drawRect);
}

What i hope the code will
do is scroll the background and displace the player every frame towards the left. Im just not sure how you can wrap the image around when you scroll to the end.

Last edited by Killerapathy : 02-14-2011 at 11:08 PM.
Killerapathy is offline   Reply With Quote
Old 02-15-2011, 11:03 AM   #4
Protector one
Moderator

Activity Longevity
2/20 17/20
Today Posts
1/11 ssssss692
Location: Netherlands
Default

I don't have much experience with XNA, but that code could work just fine, sure.

I also find "wrapping the background around" to usually be an annoying issue. It (probably) requires creating a second instance of your background instance, when the first instance is fully visible to the player. Place it adjacent to your other instance, and have it scroll the same amount as your first instance.
When the leftmost instance scrolls outside of the viewing area, you can remove references to the object so you don't waste resources rendering an object the player can't see anyway.

Oh, and by the way, this is not really "pseudo code", but rather "code".
__________________
My name is Louis. Hi!
Uh oh! The princess is in another signature!
Protector one is online now   Reply With Quote
Reply


Thread Tools
Display Modes

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

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


Powered by vBulletin® Version 3.6.9
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
All times are GMT -8. The time now is 06:25 PM.






UBM Tech