如何管理背景渲染?

时间:2012-02-28 12:11:58

标签: android libgdx

我想移动背景与bob(Android游戏角色)移动,我创建一个动态对象名称背景

    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;
    }
 }

类似于世界级中的bob I amke对象,并将一个ArrayList添加Backgroung添加到该arraylist中,并且在绘制时从arraylist获取并绘制它...但是没有任何效果只显示屏幕显示并跨越范围和显示的红色屏幕...请任何人帮忙..

       /*---------------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();
   }

1 个答案:

答案 0 :(得分:1)

你想要用Bob(1)移动背景,或者你想要一个固定的背景(2):

(1):将背景的位置设置为Bob(可能有偏移量,你可以在世界的更新函数中执行),使用batch.draw(......, background.position.x, background.position.y)进行渲染。

(2):抛弃BackGround对象中复杂的更新方法,并像现在一样使用render方法。

但老实说,你的意图和解释并不十分明确,试着改进它!