How to render a sprite that spawns at the top of the screen, moves to the bottom, then restarts from the top?

I know this is an oddly specific request, but I’m making a top down “shooter” game focused around speed, and I want to render some “road lines” to make it look like the player is speeding up.

These lines would be horizontal and would move down the screen, then reappear at the top. Is there an easy way to do this?

Any help? Thanks!

Hey @Chubbulus , I can’t think of an easy way to do this, as in some pre-existing code that you could use. You’ll likely have to build something that does what you want. I’d suggest a separate class(es) for it, with update() and draw() functions that can be called in ofApp.

You could use ofDrawLine() or ofDrawRectangle() to render the road lines. Calling ofFill() will draw shapes as filled, while ofNoFill() will just draw outlines. Use ofSetColor() to set a color before drawing something. Rendering the line could also involve a texture (an ofImage, ofTexture, etc), which can be drawn at a determined location and size.

Multiple road lines can be managed with a std::vector. In this case, you might want to add 2 total new classes. One class (class RoadLine) would contain all of the things for a single line (position, velocity, color, width, height, etc), and likely update(), draw(), reset(), and functions related to each line. Another class (class RoadLines) would have a std::vector along with update(), draw(), and any functions related to the whole group of lines.

Moving a line from the bottom to the top of the screen is just a matter of resetting its position back to the top of the screen whenever its current (or next) position exceeds some boundary condition.

I ended up re-using code I had written for lives that I decided not to implement. I made a sprite and made 2 instances of the lines, then had them scroll down the screen and once reaching the bottom, they would go back to the top.