【问题标题】:Rect.intersects not workingRect.intersects 不工作
【发布时间】:2015-09-01 01:48:17
【问题描述】:

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

我正在尝试制作一个飞扬的小鸟风格的游戏来尝试 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()){

        }
    }
}

【问题讨论】:

    标签: java android eclipse rect


    【解决方案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());
    

    不应使用 getWidth() 或 getHeight(),但应使用 x + width 和 y + height。

    公共矩形(int left,int top,int right,int bottom)

    在 API 级别 1 中添加 创建一个具有指定的新矩形 坐标。注意:不执行范围检查,所以调用者必须 确保左

    参数

    left 矩形左侧的X坐标

    top 矩形顶部的 Y 坐标

    正确的 X 矩形右侧坐标

    bottom Y坐标 矩形的底部

    这是来自documentation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-10
      • 1970-01-01
      • 2019-01-19
      • 1970-01-01
      • 2018-12-22
      • 2016-04-19
      • 1970-01-01
      相关资源
      最近更新 更多