Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 391531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:04:14+00:00 2026-05-12T16:04:14+00:00

I have a game that uses layers as a way to organize depth. It

  • 0

I have a game that uses layers as a way to organize depth. It works fine but I have stumbled into a silly problem…and despite being simple I can’t get it to work EXACTLY as I need.
The idea is that once a graphic is completely out of the game area (as it its right side has completely traveled through the area’s left side, so it’s not visible anymore), it should be transported to the opposite side of the area.

This is the culprit code, checked every cycle to see if the object is within boundaries, and in the special case of an “infinite” layer, this code is executed:

if(world.layer[object[i].layer].infinite){
    sw = object[i].rect.w * 0.5;
    sh = object[i].rect.h * 0.5;
    if(object[i].rect.x + sw < world.fieldw[0] && world.layer[object[i].layer].speedX < 0){
        dif = world.fieldw[0] - (object[i].rect.x + sw);
        object[i].rect.x = ((world.fieldw[1]+sw) + dif);
    }
    else if(object[i].rect.x - sw > world.fieldw[1] && world.layer[object[i].layer].speedX > 0){
        dif = (object[i].rect.x - sw) - world.fieldw[1];
        object[i].rect.x = ((world.fieldw[0]-sw) - dif);
    }

    if(object[i].rect.y + sh < world.fieldh[0] && world.layer[object[i].layer].speedY < 0){
        dif = world.fieldh[0] - (object[i].rect.y + sh);
        object[i].rect.y = ((world.fieldh[1]+sh) + dif);
    }
    else if(object[i].rect.y - sh > world.fieldh[1] && world.layer[object[i].layer].speedY > 0){
        dif = (object[i].rect.y - sh) - world.fieldh[1];
        object[i].rect.y = ((world.fieldh[0]-sh) - dif);
    }

It works, but as the objects loop around, they lose their original formation, what distorts the backdrops. What am I doing wrong here?
I know it’s nowhere a complex issue, but I have tried and can’t find an exact result.
EDIT: fieldw[0] is the left boundary of the area, fieldw[1] is the right. Same for h[0/1] in vertical space. Layer speedX/Y are the constant speed used in the layer (to keep it scrolling, like in the Flintstones and their repeating backgrounds) .
Also, should I take speed in consideration when doing the move?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T16:04:15+00:00Added an answer on May 12, 2026 at 4:04 pm

    All objects should snap back at the same point out of the field of vision.

    So the way to do it is to determine the biggest object and take this width. And this is the point outside of the view where objects are snapped back to the right side of the screen.

    If you can’t determine the biggest object, you could also take the width of the screen (given that objects are never wider than the screen).

    so your code would look like this (pseudocodeish):

     int biggestObjectWidth = 200;
    
     loop:
          if(object.x < (layer.x-biggestObjectWidht)) 
          {
               object.x += biggestObjectWidth + layer.width;
          }
    

    so all objects are snapped back the same amount

    What you can also do is just Modulo the object positions.

     drawObject((object.x % (biggestObjectWidth+layer.width)) - biggestObjectWidth, object.y)
    

    this way you don’t need to snap objects back yourself… they’ll be done automatically.
    beware of negative object.x values though, since than the modulo trick will work against you ;^)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a game that uses GLUT, OpenGL and FMOD. The problem is
I have a game engine that uses OpenGL for display. I coded a small
I have a cocos2d powered game that uses UIKit menues, so I only use
I have a C# game program that i'm developing. it uses sound samples and
We have a game that is very close to being released, but we have
We have an existing PC game that uses C# and the XNA Framework. We
i have a java game app that uses sockets to communicate with each other.
I have a clojure project that uses the slick 2d game engine that I
I'm currently working on a game/engine that uses OpenGL for rendering, and recently have
I have a game that uses the accelerometer to move the player. For this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.