如何在我的游戏中两个Rect相交时正确渲染图像?

时间:2015-12-01 21:21:51

标签: android

所以游戏基本上都是这样的,每当火焰与冰淇淋接触时,我都希望在屏幕上画出不同的冰淇淋。较小的一个类似于“融化”的外观。但是,每当冰淇淋和火焰发生碰撞时,游戏就会在没有结束时结束。

这是渲染iceCream的代码

  private void renderIceCream(Painter g) {

        if (iceCream3) {
            g.drawImage(Assets.iceCream3, (int) iceCream.getX(), (int) iceCream.getY(), ICECREAM_WIDTH, IC3_HEIGHT);

            if (Rect.intersects(iceCream.getRect(), fire.getRect())) {
                iceCream3 = false;
                iceCream2 = true;


            }
        }

        if (iceCream2) {
            g.drawImage(Assets.iceCream2, (int) iceCream.getX(), (int) iceCream.getY() + 25, ICECREAM_WIDTH, IC2_HEIGHT);
//
            if (Rect.intersects(iceCream.getRect(), fire.getRect())) {
                iceCream2 = false;
                iceCream1 = true;

            }
        }

        if (iceCream1) {

            g.drawImage(Assets.iceCream1, (int) iceCream.getX(), (int) iceCream.getY() + 50, ICECREAM_WIDTH, IC1_HEIGHT);
            if (Rect.intersects(iceCream.getRect(), fire.getRect())) {
                iceCream1 = false;
                iceCream.melted();

            }
        }
    }

这是全班

package rect.draw.gametest.model.state;

import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.view.GestureDetector;
import android.view.MotionEvent;



import java.util.ArrayList;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.view.MotionEvent;
import android.widget.Toast;

import rect.draw.gametest.Assets;
import rect.draw.gametest.GameMainActivity;
import rect.draw.gametest.model.Fire;
import rect.draw.gametest.model.IceCream;
import rect.draw.gametest.model.util.Painter;


public class PlayState extends State {
    private IceCream iceCream;
    private Fire fire;
    private int fireX = 400;
    private int fireY = 0;

    private int cloudX = 0;
    private int cloudY = 100;

    private float duckDuration = .6f;

    private int playerY;

    private int playerScore = 0;
    private static final int BLOCK_HEIGHT = 50;
    private static final int BLOCK_WIDTH = 20;
    private int fireSpeed = -200;
    private static int ICECREAM_WIDTH = 38;
    private static int IC1_HEIGHT = 77;
    private static int IC2_HEIGHT= 102;
    private static int IC3_HEIGHT = 127;

    private static int FIRE_WIDTH = 75;
    private static int FIRE_HEIGHT = 75;

    public boolean iceCream1,iceCream2,iceCream3,durationEnded;


    private float recentTouchY;

    @Override
    public void init() {
        iceCream = new IceCream(160, GameMainActivity.GAME_HEIGHT - 45
                - IC3_HEIGHT, ICECREAM_WIDTH, IC3_HEIGHT);
        fire = new Fire(10, 10, FIRE_WIDTH, FIRE_HEIGHT);
        //cloud = new Cloud(100, 100);
        // cloud2 = new Cloud(500, 50);
        iceCream2 = true;
       // iceCream2 = false;
       // iceCream1 = false;
    }


    @Override
    public void update(float delta) {
        fire.update(delta, fireSpeed);
        iceCream.update(delta);

        if (!iceCream.isFrozen()) {
            setCurrentState(new GameOverState(playerScore / 100));
        }
    }

    @Override
    public void render(Painter g) {
        g.setColor(Color.rgb(208, 244, 247));
        g.fillRect(0, 0, GameMainActivity.GAME_WIDTH,
                GameMainActivity.GAME_HEIGHT);

        renderSun(g);
        renderClouds(g);
        g.drawImage(Assets.grass, 0, 405);
        renderIceCream(g);
        renderFire(g);
        renderScore(g);
    }

    private void renderScore(Painter g) {
        g.setFont(Typeface.SANS_SERIF, 25);
        g.setColor(Color.GRAY);
        g.drawString("" + playerScore / 100, 20, 30);
    }



    private void renderFire(Painter g) {
        g.drawImage(Assets.gameTestFire, (int) fire.getX(), (int) fire.getY(), FIRE_WIDTH, FIRE_HEIGHT);
        if(Rect.intersects(iceCream.getRect(),fire.getRect())){
            fire.reset();
        }
    }

    private void renderIceCream(Painter g) {

        if (iceCream3) {
            g.drawImage(Assets.iceCream3, (int) iceCream.getX(), (int) iceCream.getY(), ICECREAM_WIDTH, IC3_HEIGHT);

            if (Rect.intersects(iceCream.getRect(), fire.getRect())) {
                iceCream3 = false;
                iceCream2 = true;


            }
        }

        if (iceCream2) {
            g.drawImage(Assets.iceCream2, (int) iceCream.getX(), (int) iceCream.getY() + 25, ICECREAM_WIDTH, IC2_HEIGHT);
//
            if (Rect.intersects(iceCream.getRect(), fire.getRect())) {
                iceCream2 = false;
                iceCream1 = true;

            }
        }

        if (iceCream1) {

            g.drawImage(Assets.iceCream1, (int) iceCream.getX(), (int) iceCream.getY() + 50, ICECREAM_WIDTH, IC1_HEIGHT);
            if (Rect.intersects(iceCream.getRect(), fire.getRect())) {
                iceCream1 = false;
                iceCream.melted();

            }
        }
    }

    private void renderSun(Painter g) {
        g.setColor(Color.rgb(255, 165, 0));
        g.fillOval(715, -85, 170, 170);
        g.setColor(Color.YELLOW);
        g.fillOval(725, -75, 150, 150);
    }

    private void renderClouds(Painter g) {
        g.drawImage(Assets.cloud1, cloudX, cloudY, 100, 60);
        cloudX += 4;

        if (cloudX > 800) {
            cloudX = 0;
        }

    }


    @Override
    public boolean onTouch(MotionEvent e, int scaledX, int scaledY) {
        if (e.getAction() == MotionEvent.ACTION_DOWN) {
            recentTouchY = scaledY;
        } else if (e.getAction() == MotionEvent.ACTION_UP) {
            if (scaledY - recentTouchY < -50) {
                iceCream.jump();
            } else if (scaledY - recentTouchY > 50) {

            }
        }
        return true;
    }
}

这是IceCream类

package rect.draw.gametest.model;

import android.graphics.Rect;




public class IceCream {

    private static final float ACCEL_GRAVITY = 1800 ;
    private static final int JUMP_VELOCITY =-600 ;
    private float x,y;
    private int width,height;
    private Rect rect,ground;
    public boolean isFrozen,durationEnded,isGrounded;

    private int velY;

    public IceCream(float x, float y, int width, int height){
        this.x=x;
        this.y=y;
        this.width=width;
        this.height=height;
        rect = new Rect((int) x, (int) y, (int) x + width, (int) y + height);

        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        ground = new Rect(0, 405, 0 + 800, 405 + 45);
        rect = new Rect();

        isFrozen = true;
    }

    public void update(float delta) {



        if (!isGrounded()) {
            velY += ACCEL_GRAVITY * delta;
        } else {
            y = 406 - height;
            velY = 0;
        }
        y += velY * delta;
        updateRect();
    }



   public void jump(){

       y -= 10;
       velY = JUMP_VELOCITY;
       updateRect();

   }
    public boolean isFrozen(){

        return isFrozen;
    }
    public void melted(){
    isFrozen = false;
    }



    public float getX() {
        return x;
    }

    public float getY() {
        return y;
    }

    public void updateRect() {
        rect.set((int) x, (int) y, (int) x + width, (int) y + height);

    }
    public Rect getRect(){
        return rect;
    }


    public boolean isGrounded() {
        return Rect.intersects(rect, ground);
    }
}

1 个答案:

答案 0 :(得分:0)

我认为可以通过添加这两个else语句来解决问题:

private void renderIceCream(Painter g) {

    if (iceCream3) {
        ...
    }

    else if (iceCream2) {
        ...
    }

    else if (iceCream1) {
        ...
    }
}

如果没有else语句,当iceCream3为真且存在冲突时,iceCream2变为true,Java进入下一个if语句,其中iceCream1变为true。使用else语句,这不会发生。如果您之后通过调用fire.reset()移开了火,那么您的代码现在应该可以正常工作。