java - Rect.intersects 不工作

标签 java android eclipse rect

I understand that this is technically a repeat question, but all of the similar questions include code I do not understand, so I decided to ask the question using code I understand.

我正在尝试制作一个 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 java.util.Random;

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

public class Cloud {
    public int leftX;
    public int leftY;
    public int rightX;
    public int rightY;
    private Random random;
    public Bitmap image;
    private Context context;
    public Rect leftCloud;
    public Rect rightCloud;
    public int height;
    public int width;

    Cloud(int image, Context context){
        this.context = context;
        this.image = BitmapFactory.decodeResource(this.context.getResources(), R.drawable.cloud);
        random = new Random();
        leftX = random.nextInt(StaticVarHandler.canvasWidth-(StaticVarHandler.birdWidth*2));
        rightX = leftX + StaticVarHandler.birdWidth*2;
        rightY = leftY = StaticVarHandler.canvasHeight+this.image.getHeight();
        leftX -= this.image.getWidth();
        leftCloud = new Rect(leftX, leftY, this.image.getWidth(), this.image.getHeight());
        rightCloud = new Rect(rightX, rightY, this.image.getWidth(), this.image.getHeight());
    }

    public void move(int scrollSpeed){
        leftCloud.offset(0, -scrollSpeed);
        rightCloud.offset(0, -scrollSpeed);
        leftY-=scrollSpeed;
        rightY-=scrollSpeed;
    }
}

鸟类类:

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()){

        }
    }
}

最佳答案

我想我发现了你的问题。

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());

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

public Rect (int left, int top, int right, int bottom)

Added in API level 1 Create a new rectangle with the specified coordinates. Note: no range checking is performed, so the caller must ensure that left <= right and top <= bottom.

Parameters

left The X coordinate of the left side of the rectangle

top The Y coordinate of the top of the rectangle

right The X coordinate of the right side of the rectangle

bottom The Y coordinate of the bottom of the rectangle

这是来自 documentation .

关于java - Rect.intersects 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32322366/

相关文章:

java - 如何使用来自datastax的java api从语句中获取精确的cql

android - 使用 Google 助理 SDK

android - "If This Then That"如何实现对第三方用户帐户的访问(所谓的 "Channels")?

eclipse - IntelliJ 喜欢 'Extract Property' 将 Maven Artifact 版本提取到 Eclipse 中的属性

java - Liferay Tomcat 与另一个 CMS 系统运行在同一个 Linux 机器上

java - Eclipse 插件 m2e 在 Luna 4.4 中无法工作(2014 年 6 月 25 日发布)

java - 使用字符串时,应该对单个字符使用 "c"还是 'c'?

java - Android 服务在一段时间后停止广播进度

Java - Eclipse - 声明的包 "edu.uci.ics.crawler4j.examples.basic"与预期的包 ""不匹配

android - Webview 链接到 m.youtube.com 而不是 youtube 应用