我无法在Java游戏android中的两个对象之间进行碰撞

时间:2014-01-28 06:14:34

标签: java android collision

我想为Android制作2D游戏。但是我不能让我的物体碰撞。我想让熊和长颈鹿在相互作用时发生碰撞,但它不起作用。

这是熊类:

package gamefoundation;

import java.awt.Rectangle;

public class Bear {
//In Java, Class Variables should be private so that only its methods can change them.
    private int centerX = 50;
    private int centerY = 430;

    private int speedX = 0;
    private int speedY = 0;

    public static Rectangle rect = new Rectangle(0,0,0,0);

    private boolean movingLeft = false;
    private boolean movingRight = false;
    private boolean movingUp = false;
    private boolean movingDown = false;

    private Giraffe giraffe;

    public void update() {

        // Moves Character or Scrolls Background accordingly.
        if (speedX < 0) {
            centerX += speedX;
        } else {
            if (centerX <= 763) /*batas maks kanan*/{
                centerX += speedX;
            }
        }

        if (speedY < 0) {
            centerY += speedY;
        } else {
            if (centerY <= 430) /*batas maks bawah*/ {
                centerY += speedY;
            } 
        }

        //biar beruangnya gak nembus sebelah kiri
        if (centerX + speedX <= 60) {
            centerX = 61;
        }
        //biar beruangnya gak nembus ke atas
        if (centerY + speedY <= 60) {
            centerY = 61;
        }

        //collision
        rect.setRect(centerX-34, centerY-63, 68, 103);

    }

    public void moveRight() {
        speedX = 6;
    }

    public void moveLeft() {
        speedX = -6;
    }

    public void moveUp() {
        speedY = -6;
    }

    public void moveDown() {
        speedY = 6;
    }

    public void stop() {
        speedX = 0;
        speedY = 0;
    }

    public int getCenterX() {
        return centerX;
    }

    public int getCenterY() {
        return centerY;
    }

    public int getSpeedX() {
        return speedX;
    }

    public int getSpeedY() {
        return speedY;
    }

    public void setCenterX(int centerX) {
        this.centerX = centerX;
    }

    public void setCenterY(int centerY) {
        this.centerY = centerY;
    }

    public void setSpeedX(int speedX) {
        this.speedX = speedX;
    }

    public void setSpeedY(int speedY) {
        this.speedY = speedY;
    }

    public boolean isMovingLeft() {
        return movingLeft;
    }

    public boolean isMovingRight() {
        return movingRight;
    }

    public void setMovingLeft(boolean movingLeft) {
        this.movingLeft = movingLeft;
    }

    public void setMovingRight(boolean movingRight) {
        this.movingRight = movingRight;
    }

    public boolean isMovingUp() {
        return movingUp;
    }

    public boolean isMovingDown() {
        return movingDown;
    }

    public void setMovingUp(boolean movingUp) {
        this.movingUp = movingUp;
    }

    public void setMovingDown(boolean movingDown) {
        this.movingDown = movingDown;
    }

}

长颈鹿班:

package gamefoundation;

import java.awt.Rectangle;

public class Giraffe extends Animal {
private Rectangle r;

public Giraffe(int centerX, int centerY) {
    setCenterX(centerX);
    setCenterY(centerY);

    r = new Rectangle();
}

public void checkVerticalCollision(Rectangle rbody){
    if (rbody.intersects(r)){
        System.out.println("collision w/ giraffe");
    }
}

private void update() {
    r.setBounds(getCenterX(), getCenterY(), 20, 20);
}

}

这是StartingClass:

package gamefoundation;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.net.URL;

public class StartingClass extends Applet implements Runnable, KeyListener {

private Bear bear;
private Image image,beruang,map,burung,buaya,jerapah,monyet,rusa;
private Graphics second;
private URL base;
private static Map mp1,mp2;
private Bird bird;
private Crocodile crocodile;
private Deer deer;
private Giraffe giraffe;
private Monkey monkey;

@Override
public void init() {
    setSize(800,480);
    setBackground(Color.BLACK);
    setFocusable(true);
    addKeyListener(this);
    Frame frame = (Frame) this.getParent().getParent();
    frame.setTitle("Petualangan Bubu");
    try{
        base=getDocumentBase();
    }catch(Exception e){
        //TODO: handle exception
    }

    //image setups
    beruang = getImage(base,"data/beruangsmall.png");
    burung = getImage(base,"data/birdsmall.png");
    buaya = getImage(base,"data/crocodilesmall.png");
    rusa = getImage(base,"data/deersmall.png");
    jerapah = getImage(base,"data/giraffesmall.png");
    monyet = getImage(base,"data/monkeysmall.png");
    map = getImage(base,"data/map.jpg");
}

@Override
public void start() {
    mp1 = new Map(0,0);
    bear = new Bear();
    bird = new Bird(430,20);
    crocodile = new Crocodile(410,120);
    deer = new Deer(205,30);
    giraffe = new Giraffe(580,86);
    monkey = new Monkey(600,260);

    Thread thread = new Thread(this);
    thread.start();
}

@Override
public void stop() {

}

@Override
public void destroy() {

}

@Override
public void run() {
    while(true) {
        bear.update();

        if(bear.getCenterX() == giraffe.getCenterX()) {
            giraffe.checkVerticalCollision(bear.rect);
        }

        repaint();
        try{
            Thread.sleep(17);
        }catch(InterruptedException e) {
            e.printStackTrace();
        }
    }
}

@Override
public void keyPressed(KeyEvent e) {
    // TODO Auto-generated method stub
    switch (e.getKeyCode()) {
       case KeyEvent.VK_UP:
           bear.moveUp();
           bear.setMovingUp(true);
           break;

       case KeyEvent.VK_DOWN:
           bear.moveDown();
           bear.setMovingDown(true);
           break;

       case KeyEvent.VK_LEFT:
           bear.moveLeft();
           bear.setMovingLeft(true);
           break;

       case KeyEvent.VK_RIGHT:
           bear.moveRight();
           bear.setMovingRight(true);
           break;

       case KeyEvent.VK_SPACE:
           System.out.println("Jump");
           break;
       }
}

@Override
public void keyReleased(KeyEvent e) {
    // TODO Auto-generated method stub
     switch (e.getKeyCode()) {
       case KeyEvent.VK_UP:
           bear.stop();
           break;

       case KeyEvent.VK_DOWN:
           bear.stop();
           break;

       case KeyEvent.VK_LEFT:
           bear.stop();
           break;

       case KeyEvent.VK_RIGHT:
           bear.stop();
           break;

       case KeyEvent.VK_SPACE:
           System.out.println("Stop jumping");
           break;
       }
}

@Override
public void keyTyped(KeyEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void update(Graphics g) {
    if(image==null) {
        image = createImage(this.getWidth(), this.getHeight());
        second = image.getGraphics();
    }

    second.setColor(getBackground());
    second.fillRect(0, 0, getWidth(), getHeight());
    second.setColor(getForeground());
    paint(second);

    g.drawImage(image, 0, 0, this);
}

@Override
public void paint(Graphics g) {
    g.drawImage(map, mp1.getMapX(), mp1.getMapY(), this);
    g.drawImage(beruang, bear.getCenterX()-61, bear.getCenterY()-63, this);
    g.drawImage(burung, bird.getCenterX(), bird.getCenterY(), this);
    g.drawImage(buaya, crocodile.getCenterX(), crocodile.getCenterY(), this);
    g.drawImage(rusa, deer.getCenterX(), deer.getCenterY(), this);
    g.drawImage(jerapah, giraffe.getCenterX(), giraffe.getCenterY(), this);
    g.drawImage(monyet, monkey.getCenterX(), monkey.getCenterY(), this);
    g.drawRect((int)bear.rect.getX(), (int)bear.rect.getY(), (int)bear.rect.getWidth(), (int)bear.rect.getHeight());
}
}

1 个答案:

答案 0 :(得分:0)

正如评论中所说,当熊和长颈鹿的中心X相等时,你只能检查碰撞。我建议简单地删除它,如果检查它可能不会给你带来明显的性能。