Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当精灵触及边界时如何循环(android游戏开发)_Java_Android_Eclipse - Fatal编程技术网

Java 当精灵触及边界时如何循环(android游戏开发)

Java 当精灵触及边界时如何循环(android游戏开发),java,android,eclipse,Java,Android,Eclipse,请帮忙。我不熟悉android游戏开发和java。我希望我的鹿在触摸屏幕边界时再次循环或从0,0开始,但我不知道如何编写代码 package com.cmanres.bunnyTravely import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Rect; public class Spriteleft { private static final int

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

package com.cmanres.bunnyTravely

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.graphics.Bitmap;
导入android.graphics.Canvas;
导入android.graphics.Rect;
公共级Spriteft{
私有静态final int BMP_COLUMNS=3;
私有静态final int BMP_ROWS=2;
私有整数x=0;
私有整数y=0;
私有int xSpeed=3;
私人游戏视图;
私家侦探1;
私有整数宽度;
私人内部高度;
私有int currentFrame=0;
公共Spriteleft(LevelunoGameView,位图deer1){
this.gameView=gameView;
这个。deer1=deer1;
this.width=deer1.getWidth()/BMP\u列;
this.height=deer1.getHeight()/BMP\u行;
}
私有void更新(){
if(x>gameView.getWidth()-width-xSpeed){
xSpeed=-3;
}
如果(x+x速度<0){
xSpeed=3;
}
x=x+x速度;
currentFrame=++currentFrame%BMP_列;
}
公共空白onDraw(画布){
更新();
int srcX=当前帧*宽度;
int srcY=1*高度;
Rect src=新的Rect(srcX,srcY,srcX+宽度,srcY+高度);
Rect dst=新的Rect(x,y,x+宽度,y+高度);
drawBitmap(deer1、src、dst、null);
}
}  

这是我在android手机游戏教程中遵循的代码。有人能告诉我怎么做吗?或者提供教程的链接?非常感谢。

假设您的精灵位于x,y位置,精灵宽度,精灵高度

//检查您的精灵位置是否超出屏幕边界

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;
}
if(x<0 | | x>gameView.getWidth()-sprite|u width
||y<0 | | y>gameView.getHeight()-sprite|u height){
//将x,y设置为0,0
x=0;
y=0;
}

适当地重写
更新
代码。请注意,让对象跳入视图看起来并不好。