【问题标题】:scrolling background in game android studio.Background moves towards right with screen blinking how to solve it在游戏android studio中滚动背景。背景向右移动,屏幕闪烁如何解决
【发布时间】:2020-05-11 11:18:52
【问题描述】:

我想在我的 android studio 游戏中实现滚动背景

我的想法是有两个位图,它们彼此相邻绘制,如果不再是屏幕,则重置位图的位置

  1. 背景移动向右
  2. 屏幕闪烁

我该如何解决我的问题?

我的SurfaceView 班级

public class GameView extends SurfaceView implements Runnable {

  private Thread thread; // draw + update thread

  int screenx, screeny; // screen size
  Background background1 , background2;

  float screenratiox, screenratioy;

  private boolean isplaying;

  Paint paint;

  public GameView(Context context, int screenx, int screeny) {
    super(context);

    this.screenx = screenx;
    this.screeny = screeny;

    this.screenratiox = 1920f / screenx;
    this.screenratioy = 1080f / screeny;

    background1 = new Background(screenx, screeny, getResources());
    background2 = new Background(screenx, screeny, getResources());

    background2.x = screenx;

    paint = new Paint();
  }

  @Override
  public void run() {
    while (isplaying) {
      update();
      sleep();
      draw();
    }
  }

  public void sleep() {
    try {
      Thread.sleep(17);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }

  public void resume() {
    isplaying = true ;
    thread = new Thread(this) ;
    thread.start();
  }

  public void pause() {
    try {
      isplaying = false ;
      thread.join();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }

  private void update() { //should move the background and reset it, if beyond the screen
    background1.x += 10 * screenratiox; // move
    background2.x += 10 * screenratioy; // move

    if (background1.x + background1.background.getWidth() < 0) {
      background1.x = screenx; //reset x
    } 

    if (background2.x + background2.background.getWidth() < 0) {
      background2.x = screenx; //reset x
    } 
  }

  private void draw () {
    if (getHolder().getSurface().isValid()) {

      Canvas canvas = getHolder().lockCanvas();

      canvas.drawBitmap(background1.background, background1.x, background1.y, paint);
      canvas.drawBitmap(background2.background, background2.x, background2.y, paint);

      getHolder().unlockCanvasAndPost(canvas);
    }
  }
}

我的背景类

public class Background {
  int x = 0, y = 0; // position
  Bitmap background; // image

  Background(int screenx , int screeny , Resources res) {
    background = BitmapFactory.decodeResource(res, R.drawable.background);
    background = Bitmap.createScaledBitmap(background, screenx, screeny, false);
  }
}

【问题讨论】:

  • 我觉得你看的教程很烂。
  • 请发布您的完整代码(睡眠、恢复+暂停方法)
  • 我已经用睡眠、恢复和暂停方法编辑了我上面的代码...请检查...当我使用 -= 10 背景准确地向左移动但我想将它移向右侧......background1.x -= 10 * screenratiox; // 移动 background2.x -= 10 * screenratioy; // 移动

标签: java android 2d-games


【解决方案1】:

background1.x += 10 * screenratiox; // 移动

background2.x += 10 * screenratioy; // 移动

字面意思是:将x 向右移动。

所以

background1.x -= 10 * screenratiox; // move 
background2.x -= 10 * screenratioy; // move

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-30
    • 2010-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-21
    相关资源
    最近更新 更多