Java 矩形相交不工作

Java 矩形相交不工作,java,android,eclipse,rect,Java,Android,Eclipse,Rect,我知道这在技术上是一个重复的问题,但所有类似的问题都包括我不懂的代码,所以我决定用我懂的代码提问 我正在尝试制作一款flappy bird风格的游戏来尝试android编程,但当玩家与物体(云)碰撞时,我无法使用Rect.intersects来更改玩家的分数 提前谢谢 视图类: package com.gregsapps.fallingbird; import java.util.ArrayList; import android.content.Context; import androi

我知道这在技术上是一个重复的问题,但所有类似的问题都包括我不懂的代码,所以我决定用我懂的代码提问

我正在尝试制作一款flappy bird风格的游戏来尝试android编程,但当玩家与物体(云)碰撞时,我无法使用Rect.intersects来更改玩家的分数

提前谢谢

视图类:

package com.gregsapps.fallingbird;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;

public class GameView extends View{

    private Bird bird;
    private boolean runOnce = false;
    private Context context;
    private Paint red;
    ArrayList<Cloud> clouds = new ArrayList<Cloud>();   
    private int cloudDelay;
    private Collision collision;
    //private Paint textPaint;


    public GameView(Context context) {
        super(context);
        this.context = context;
        this.setDrawingCacheEnabled(true);
        red = new Paint();
        red.setColor(Color.RED);
        red.setTextSize(100f);
        collision = new Collision();
        //textPaint.setColor(Color.BLACK);
        //textPaint.setTextAlign(Align.RIGHT);
        // TODO add setup code
    }

    protected void onDraw(Canvas canvas){
        update(canvas);
        System.out.println(bird.score);
        //TODO add drawing code
        this.buildDrawingCache();
        //bird.canvasImage = this.getDrawingCache(true);
        canvas.drawColor(Color.rgb(10, 255, 255));
        canvas.drawRect(bird.birdRect, red);
        canvas.drawBitmap(bird.image, bird.x, bird.y, null);


        for(int i = 0; i < clouds.size(); i++){
            System.out.println("Drawing cloud");
            Cloud cloud = (Cloud) clouds.get(i);
            cloud.move(5);
            System.out.println(cloud.leftX + "/t" + cloud.rightX);
            canvas.drawRect(cloud.rightCloud, red);
            canvas.drawRect(cloud.leftCloud, red);
            canvas.drawBitmap(cloud.image, cloud.leftX, cloud.leftY, null);
            canvas.drawBitmap(cloud.image, cloud.rightX, cloud.rightY, null);
            if(cloud.leftY <= -144)clouds.remove(cloud);
            if(bird.y > cloud.leftY + bird.height) bird.score++;
            if(Rect.intersects(bird.birdRect, cloud.leftCloud)){
                bird.score = 0;
            }
            else if(Rect.intersects(bird.birdRect,  cloud.rightCloud)){
                bird.score = 0;
            }
        }


        canvas.drawLine(canvas.getWidth()/2 - 1, 0, canvas.getWidth()/2 - 1, canvas.getHeight(), red);
        cloudDelay --;

        if(cloudDelay <= 0){
            System.out.println("new cloud");
            Cloud cloud = new Cloud(com.gregsapps.fallingbird.R.drawable.cloud, context);
            System.out.println("added");
            clouds.add(cloud);
            cloudDelay = 175;
        }

        canvas.drawText(Integer.toString(bird.score/12), 50, 100, red);

        invalidate();

    }

    private void update(Canvas canvas){
        //TODO add code to update stuff
        if(runOnce == false){
            bird = new Bird(canvas, com.gregsapps.fallingbird.R.drawable.bird, context);
            runOnce = true;
            StaticVarHandler.canvasHeight = canvas.getHeight();
            StaticVarHandler.canvasWidth = canvas.getWidth();
        }
        bird.move();
    }


}
鸟类种类:

package com.gregsapps.fallingbird;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;

public class Bird {
    public int x;
    public int y;
    private int speed;
    public Bitmap image;
    Context context;
    private int gravity;
    public int width;
    public int height;
    private int canvasWidth;
    private int canvasHeight;
    public Bitmap canvasImage;
    public boolean touch;
    public int touchX;
    public int touchY;
    private int gravDelay = 0;
    private int jump = 0;
    public int score;
    public Rect birdRect;

    Bird(Canvas canvas, int image, Context context){
        this.context = context;
        this.image = BitmapFactory.decodeResource(this.context.getResources(), image);
        x = canvas.getWidth()/2 - this.image.getWidth()/2;
        y = 10;
        speed = this.image.getWidth()/10;

        //setup gravity, speed, width and height attributes
        speed = canvas.getWidth()/25;
        gravity = speed/10;
        StaticVarHandler.birdWidth = width = this.image.getWidth();
        height = this.image.getHeight();
        canvasWidth = canvas.getWidth();
        canvasHeight = canvas.getHeight();
        System.out.println(canvasWidth);
        System.out.println(canvas.getWidth());
        birdRect = new Rect(x, y, this.image.getWidth(), this.image.getHeight());
    }

    public void move(){
        gravDelay --;
        jump --;
        if(StaticVarHandler.touch) jump = 3;
        if(jump >= 0){
            if(StaticVarHandler.touchX < canvasWidth/2){
                x -= speed/3;
            }
            if(StaticVarHandler.touchX > canvasWidth/2){
                x += speed/3;
            }
            StaticVarHandler.touch = false;

            if(jump == 0) gravDelay = 7;
        }
        else if(gravDelay <= 0){
            System.out.println("GRAVITY");
            if(x+width/2 < canvasWidth/2){
                x += gravity;
                //code to move bird via gravity
            }
            if(x+width/2 > canvasWidth/2){
                x -= gravity;
                //same as above but other side
            }
            gravDelay = 1;
        }
        if(x <= 0){
            score = 0;
            x = 0;
        }
        else if(x+width >= canvasWidth){
            score = 0;
            x = canvasWidth - width;
        }
        birdRect.offsetTo(x-1, y-1);
    }


    private void collisionCheck(){
        if(longEquation()){

        }
    }
}
package com.gregsapps.fallingbird;
导入android.content.Context;
导入android.graphics.Bitmap;
导入android.graphics.BitmapFactory;
导入android.graphics.Canvas;
导入android.graphics.Rect;
公鸡{
公共int x;
公共智力;
私人整数速度;
公共位图图像;
语境;
重力;
公共整数宽度;
公众内部高度;
私人内部画布宽度;
私人内部画布高度;
公共位图画布图像;
公众接触;
公共int touchX;
公众敏感;
专用int延迟=0;
私有整数跳跃=0;
公众智力得分;
公共直选;
鸟(画布、整型图像、上下文){
this.context=上下文;
this.image=BitmapFactory.decodeResource(this.context.getResources(),image);
x=canvas.getWidth()/2-this.image.getWidth()/2;
y=10;
速度=this.image.getWidth()/10;
//设置重力、速度、宽度和高度属性
速度=canvas.getWidth()/25;
重力=速度/10;
StaticVarHandler.birdWidth=width=this.image.getWidth();
高度=this.image.getHeight();
canvasWidth=canvas.getWidth();
canvasHeight=canvas.getHeight();
System.out.println(画布宽度);
System.out.println(canvas.getWidth());
birdRect=newrect(x,y,this.image.getWidth(),this.image.getHeight());
}
公开作废动议(){
延迟--;
跳——;
如果(StaticVarHandler.touch)跳转=3;
如果(跳转>=0){
if(StaticVarHandler.touchXcanvasWidth/2){
x+=速度/3;
}
StaticVarHandler.touch=false;
如果(跳跃=0)延迟=7;
}
否则如果(延迟画布宽度/2){
x-=重力;
//同上,但另一侧
}
延迟=1;
}
如果(x=画布宽度){
得分=0;
x=画布宽度-宽度;
}
伯德雷特·奥菲托(x-1,y-1);
}
私有void冲突检查(){
if(longEquation()){
}
}
}

我想我发现了你的问题

birdRect = new Rect(x, y, this.image.getWidth(), this.image.getHeight());

不应使用getWidth()或getHeight(),但应使用x+宽度和y+高度

公共矩形(左整数、上整数、右整数、下整数)

在API级别1中添加创建具有指定 协调。注意:不执行范围检查,因此调用者必须 确保左
birdRect = new Rect(x, y, this.image.getWidth(), this.image.getHeight());
leftCloud = new Rect(leftX, leftY, this.image.getWidth(), this.image.getHeight());
    rightCloud = new Rect(rightX, rightY, this.image.getWidth(), this.image.getHeight());