Solved

Java scrolling background

Posted on 2011-02-24
3
624 Views
Last Modified: 2012-05-11
I am creating Road Rash in a java midlet. I have come to the part where I have to create the track that scrolls towards the player. Does anyone have any idea about how to go about doing this? Could I just have the trees move towards the player to give the effect of the track and if so how would I put bends in the track.
0
Comment
Question by:tango2009
  • 2
3 Comments
 
LVL 26

Expert Comment

by:dpearson
ID: 34975984
Yes you're thinking along the right lines.  You're going to want to draw the trees closer and closer to the player (and getting larger) to give the sense of motion.

In order to draw the road you want to think in terms of scan lines.  Say the horizon is half way up the screen.  To draw the road you need to keep track of where the left and right side of the road is for each line from the midpoint of the screen down.  With each frame repaint of the game, you'd draw a line in the color of the road (or copied from a bitmap if you want more beauty at the cost of less speed) from left-to-right for that line of the screen.

On a straight road your left and right values would all be the same.  For a bend, the values would vary.

For example, say the lower half of the screen is 200 pixels and the screen is 400 pixels wide.  You'd have:

 
int[] left = new int[200] ;
int[] right = new int[200] ;

For a straight road:
for (int i =  0; i < 200 ; i++) { left[i] = 100 ; right[i] = 300 ; }

For a road with a bend:
for (int i =  0 ; i < 200 ; i++) { left[i] = i/2 ; right[i] = 200+i/2 ; }

Then to create the next frame you'd shift the road down one (or more) lines:
void nextFrame(int moveLines) {
    for (int i = 0 ; i < 200-moveLines ; i++) { left[i] = left[i+moveLines] ; right[i] = right[i+moveLines] ; }
    // Now fill in new values for left[200-movesLines] to left[200], these define the new road appearing
   // over the horizon.
}

void drawFrame() {
   for (int i = 0 ; i < 200 ; i++) {
       // Draw the road
       int y = 400-i ;
       drawLine(left[i],y, right[i],y) ;
   }
}

Open in new window


Hope that makes sense.

Doug
0
 

Author Comment

by:tango2009
ID: 34982984
What should drawLine be defined as? and should I just call the drawFrame method in the part of the code that draws the objects?
0
 
LVL 26

Accepted Solution

by:
dpearson earned 500 total points
ID: 34985813
That was just pseudo code.

If you want details on how to use the actual drawLine method in Java there's an example here: http://www.java2s.com/Code/Java/2D-Graphics-GUI/Drawline.htm

And yes you should call the drawFrame() method in the part of the code that draws the objects.  You should draw the background first and then draw the trees etc. afterwards, so they appear to be in front of the background.

Doug
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Currency Conversion? 1 71
control image tags in a string ? 12 110
Windows Service to Receive TCP Packets 4 121
t-sql sp_addlinkedserver possible risks 3 52
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
A short article about problems I had with the new location API and permissions in Marshmallow
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now