2

请帮忙。我是android游戏开发和java的新手。我希望我的鹿在触摸屏幕边框时再次循环或从 0,0 开始,但我不知道如何编码。

包 com.cmanres.bunnyjourney;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;



public class Spriteleft {
       private static final int BMP_COLUMNS = 3;
   private static final int BMP_ROWS = 2;
   private int x = 0; 
   private int y=0;
       private int xSpeed = 3;
       private Levelunogame gameView;
       private Bitmap deer1;
   private int width;
   private int height;
   private int currentFrame=0;

 public Spriteleft(Levelunogame gameView, Bitmap deer1) {
         this.gameView=gameView;
         this.deer1=deer1;
         this.width = deer1.getWidth() / BMP_COLUMNS;
         this.height = deer1.getHeight() / BMP_ROWS;
   }

   private void update() {
         if (x > gameView.getWidth() - width - xSpeed) {
                xSpeed = -3;
         }
         if (x + xSpeed< 0) {
                xSpeed = 3;
         }
         x = x + xSpeed;
         currentFrame = ++currentFrame % BMP_COLUMNS;
   }


   public void onDraw(Canvas canvas) {
         update();
         int srcX = currentFrame * width;
         int srcY = 1 * height;
         Rect src = new Rect(srcX, srcY, srcX + width, srcY + height);
         Rect dst = new Rect(x, y, x + width, y + height);
         canvas.drawBitmap(deer1, src , dst, null);
       }
 }  

这是我在 android 手机游戏教程中遵循的代码。谁能告诉我怎么做?或提供任何教程链接?我会非常感激。

4

1 回答 1

0

假设您的精灵位于 x, y 与 sprite_width, sprite_height

// 检查你的精灵位置是否在屏幕边界之外

if( x < 0 || x > gameView.getWidth() - sprite_width 
 || y < 0 || y > gameView.getHeight() - sprite_height ) {
    // Set x, y to 0,0
    x = 0;
    y = 0;
}
于 2012-12-14T10:55:14.263 回答