滚动背景,如月球着陆器或忍者跳跃游戏-Android

滚动背景,如月球着陆器或忍者跳跃游戏-Android,android,game-physics,2d-games,Android,Game Physics,2d Games,我正在使用Canvas/Surfaceview开发一个2d游戏,在滚动背景图像时遇到问题 看看这个游戏- 在忍者跳跃游戏中,角色忍者只是在X坐标中跳跃,背景图像以非常高的速度滚动,使忍者看起来像是在奔跑 我已经创建了基本设置,创建了忍者,添加了跳跃功能,添加了背景现在我想一遍又一遍地重复相同的背景。我如何才能做到这一点? 下面是我的源文件-主要活动类 package com.abc.apps; import android.app.Activity; import android.os

我正在使用Canvas/Surfaceview开发一个2d游戏,在滚动背景图像时遇到问题

看看这个游戏-

在忍者跳跃游戏中,角色忍者只是在X坐标中跳跃,背景图像以非常高的速度滚动,使忍者看起来像是在奔跑

我已经创建了基本设置,创建了忍者,添加了跳跃功能,添加了背景现在我想一遍又一遍地重复相同的背景。我如何才能做到这一点?

下面是我的源文件-主要活动类

    package com.abc.apps;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

public class LadderActivity extends Activity {

    private static final String TAG = LadderActivity.class.getSimpleName();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // requesting to turn the title OFF
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        // making it full screen
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        // set our MainGamePanel as the View
        setContentView(new MainGameBoard(this));
        Log.d(TAG, "View added"); 
    }

 @Override
 protected void onDestroy() {
     Log.d(TAG, "Destroying...");
     super.onDestroy();
 }

 @Override
 protected void onStop() {
     Log.d(TAG, "Stopping...");
     super.onStop();
 }
}
游戏板扩展SurfaceView

    package com.abc.apps;

import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MainGameBoard extends SurfaceView implements SurfaceHolder.Callback{

    private MainGameLoop thread;
    private Monkey monkey;
    private static final String TAG = MainGameLoop.class.getSimpleName();

    int currentX, currentY;


    public MainGameBoard(Context context) {
        super(context);
        // TODO Auto-generated constructor stub

        // adding the callback (this) to the surface holder to intercept events
        //This line sets the current class (MainGamePanel) as the handler for the events happening on the actual surface
        getHolder().addCallback(this);

        // create monkey and load bitmap INITIALIZE AT LEFT
        monkey = new Monkey(BitmapFactory.decodeResource(getResources(), R.drawable.actor),60, 340); 

        // create the game loop thread
        thread = new MainGameLoop(getHolder(), this);


        // make the GamePanel focusable so it can handle events.
        setFocusable(true);
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        // TODO Auto-generated method stub

    }

    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub

        thread.setRunning(true);
        thread.start();

    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        // tell the thread to shut down and wait for it to finish
        // this is a clean shutdown
        boolean retry = true;
        while (retry) {
            try {
                thread.join();
                retry = false;
               } 
            catch (InterruptedException e) {
                // try again shutting down the thread
               }
              }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
         if (event.getAction() == MotionEvent.ACTION_DOWN) {
             //For jumping Left
             if (event.getX() < (getWidth()/2 - 32)) {
                // Log.d(TAG, "Coords: x=" + event.getX() + ",y=" + event.getY());
                 //Log.d(TAG, "Jump Left");
                // Sleep so that the main thread doesn't get flooded with UI events.
                 try {
                     Thread.sleep(32);
                     monkey.setX((getWidth()/2 - 60));
                     monkey.setY(monkey.getY()-70);

                 } catch (InterruptedException e) {
                     // No big deal if this sleep is interrupted.
                 }


             }

             //For Jumping Right
             if (event.getX() > (getWidth()/2 + 32)) {
                 //Log.d(TAG, "Coords: x=" + event.getX() + ",y=" + event.getY());
                 //Log.d(TAG, "Jump Right");
                // Sleep so that the main thread doesn't get flooded with UI events.
                 try {
                     Thread.sleep(32);

                     monkey.setX((getWidth()/2 + 60));
                     monkey.setY(monkey.getY()-70); 

                 } catch (InterruptedException e) {
                     // No big deal if this sleep is interrupted.
                 }


             }

/*           //Middle Portion
             if (event.getX() > (getWidth()/2 - 32) && event.getX() < (getWidth()/2 +32)) {
                 //thread.setRunning(false);
                 //((Activity)getContext()).finish();
             }*/


         }
         return true;
        }



    public void render(Canvas canvas) {
        canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.monkey_sc), 0, 0,null);
        monkey.draw(canvas);
    }

/*  @Override
    protected void onDraw(Canvas canvas){
        canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.monkey_scene), 0, 0,null);
        monkey.draw(canvas);
    }*/



}
猴子类

package com.abc.apps;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.MotionEvent;

public class Monkey {

 private Bitmap bitmap; // the actual bitmap
 private int x;   // the X coordinate
 private int y;   // the Y coordinate
 private boolean touched; // if monkey is touched


 public Monkey(Bitmap bitmap, int x, int y) {
  this.bitmap = bitmap;
  this.x = x;
  this.y = y;

 }

 public Bitmap getBitmap() {
  return bitmap;
 } 
 public void setBitmap(Bitmap bitmap) {
  this.bitmap = bitmap;
 }
 public int getX() {
  return x;
 }
 public void setX(int x) {
  this.x = x;
 }
 public int getY() {
  return y;
 }
 public void setY(int y) {
  this.y = y;
 }

 public boolean isTouched() {
  return touched;
 }

 public void setTouched(boolean touched) {
  this.touched = touched;
 }


 public void draw(Canvas canvas) {
     Paint paint = new Paint();
     paint.setAntiAlias(true);
     paint.setFilterBitmap(true);
     paint.setDither(true);

  canvas.drawBitmap(bitmap, x - (bitmap.getWidth() / 2), y, paint);
 }



}

看起来您正在render方法中的MainGameBoard类中绘制背景

public void render(Canvas canvas) {

canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.monkey_sc), 0, 0,null);

monkey.draw(canvas);
}
您应该只需要2个drawBitmap调用,而不是1个

canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.monkey_sc), 0, y_offset1,null);
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.monkey_sc), 0, y_offset2,null);
我假设每个背景图像具有相同的高度或大于屏幕高度;如果小于屏幕高度,则需要2个以上的实例

然后在y_offset1=0处开始一个图像,另一个在y_offset2=-image_高度处开始

每次提款时,您将增加y_抵销1和y_抵销2相同的金额。然后,您需要检查两个偏移量,以查看其中一个偏移量是否大于屏幕高度。如果确实如此,则现在“屏幕下方”的y_偏移应重置为另一个y_偏移减去图像高度。这将创建一个无限循环的滚动图像


当使用这种技术时,重要的是要考虑你的图像边缘;图像应设计为无缝平铺,否则在循环点边缘有明显的视觉伪影。

能否请您详细解释我应该在哪里编码。。。我是安卓系统的新手。我也相信FPS因素必须被考虑进去,我没有,但是我已经在原始文章中添加了所有的源代码。对于背景图像,我使用的是简单的白色背景。我真的厌倦了,如果有人有时间,那么任何人都可以制作一个工作教程吗
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.monkey_sc), 0, y_offset1,null);
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.monkey_sc), 0, y_offset2,null);