开发者

Collision system not working

I am making a small game, with a friend. Now, I have a camera-system, I am walking by basicly just changing x and y values of environment objects. Example:

    public void move(){
    if(player.up){
        enemyposy += player.speedY;
        hostageposy += player.speedY;
        grassy += player.speedY;
        ammoboxy += player.speedY;
        healthkity += player.speedY;
        helipady += player.speedY;
        bushy += player.speedY;
    }

This will move all those objects, down when you press the up button, so it creates an illusion that the player is moving, which it isn't. Now, I tried and create a collision system by doing this:

    public void checkCollision(){
    if(player.getBounds().intersects(enemy.getBounds())){
        System.out.println("Colliding with Enemy");
        healthdown.start();
    }else{
        healthdown.stop();
    }

But, when I go to the enemy, and touch it, it won't react like it's touching, it's not saying "Colliding with Enemy.", however I AM in the player. Can somebody perhaps tell me why it's not responding, and give me a solution on how to solve it?

getBounds method:

   开发者_如何学运维 public Rectangle getBounds(){
    return new Rectangle(getX(),getY(), 20, 20);
}

player getBounds is exactly the same.

intersects is an already implemented method.

boolean java.awt.Rectangle.intersects(Rectangle r)

Determines whether or not this Rectangle and the specified Rectangle intersect. Two rectangles intersect if their intersection is nonempty.

Parameters: r the specified Rectangle Returns: true if the specified Rectangle and this Rectangle intersect; false otherwise.


In the code you show you don't update player.x so player.getX won't change. The same for enemy :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜