Question

I want to move background with the bob(Android Game Character) moving for that I make a Dynamic object name Background

    public static int BACKGROUND_MOVE=0;
  public static int BACKGROUND_FALL=1;
 public static final float BACKGROUND_MOVE_VELOCITY =3.5f;
 public static float BOB_WIDTH =10.0f;
 public static float BOB_HEIGHT =15.0f;
 public static long startTime = 0;
 public int state;
 public float stateTime;
public BackGround(float x, float y)
 {
    super(x, y, BOB_WIDTH, BOB_HEIGHT);
    state = BACKGROUND_MOVE;
    stateTime = 0;
    accel.set(0,-2);
    velocity.y=3.5f;//55

 }  

public void update (float deltaTime)
 {
    //velocity.add(World.gravity.x, World.gravity.y * deltaTime);
    velocity.add(accel.x * deltaTime,accel.y*deltaTime);
            position.add(velocity.x * deltaTime, velocity.y * deltaTime);

    if (velocity.y > 0 && state==Bob.BOB_STATE_HIT)//BOB_STATE_HIT is bob running //condition
       {
            if (state != BACKGROUND_MOVE)
            {
                state = BACKGROUND_MOVE;
                stateTime = 0;
            }
        }

    if (velocity.y > 0 && state != Bob.BOB_STATE_HIT)
         {
        if (state != BACKGROUND_FALL)
         {
            state = BACKGROUND_FALL;
            stateTime = 0;
         }
       }
  //        if (velocity.y < 0 && state == BOB_STATE_HIT)
 //       {
  //            if (state != BOB_STATE_JUMP) {
 //             state = BOB_STATE_JUMP;
//              stateTime = 0;
//          }
    //}

    //if (position.y < 0) position.x = World.WORLD_WIDTH;
    //if (position.x > World.WORLD_WIDTH) position.x = 0;
    stateTime += deltaTime;
   }
 public void move()
    {
    if(state==BACKGROUND_MOVE)
    {
    startTime=System.nanoTime()/1000000000; 
  //        state = BACKGROUND_MOVE;
    velocity.y = BACKGROUND_MOVE_VELOCITY;
    stateTime = 0;
    }
 }

similar to bob I amke object in World class and make an ArrayList Add Backgroung into that arraylist and at time of drawing get from arraylist and draw it...but no any effect show simply screen show and cross the rangeand red screen shown...please anyone help..

       /*---------------background cam------*/

      this.bcam = new OrthographicCamera(FRUSTUM_WIDTH,FRUSTUM_HEIGHT);
       this.bcam.position.set(FRUSTUM_WIDTH,FRUSTUM_HEIGHT, 0);
       this.batch = batch;

          public void renderBackground()
          {
    GLCommon gl = Gdx.gl;
    gl.glClearColor(1, 0, 0, 1);
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    bcam.update();
    batch.setProjectionMatrix(bcam.combined);
    batch.disableBlending();
    batch.begin();
    if (world.objbackground.state ==BackGround.BACKGROUND_MOVE)
    batch.draw(Assets.mainbackgroundRegion, cam.position.x - FRUSTUM_WIDTH / 2,       cam.position.y - FRUSTUM_HEIGHT / 2, FRUSTUM_WIDTH, FRUSTUM_HEIGHT);
        //       else
        //       {
        //       batch.draw(Assets.touchbackgroundRegion, cam.position.x -
        //       FRUSTUM_WIDTH / 2, cam.position.y - FRUSTUM_HEIGHT / 2,
        //       FRUSTUM_WIDTH, FRUSTUM_HEIGHT);
        //       if (elapsed == 5)
        //              changebackground = 0;
        //       }
      batch.end();
   }
Was it helpful?

Solution

Either you want to move the background with Bob (1) or you want to have a fixed background (2):

(1): Set background's position to Bob's (probably with an offset, you can do that in the update function of the world), render using batch.draw(......, background.position.x, background.position.y).

(2): Throw away your complicated update methods in the BackGround object and use the render method as you do now.

But honestly, your intentions and explanations are not very clear, try to improve on that!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top