碰撞()无法正常工作

时间:2014-11-28 15:25:03

标签: java collision-detection

所以当我碰到getBoundsBlock()时,它会像玩家一样停止玩家,但它不会让我上升,只能左右移动。但是,当我碰到getBoundsBlock2()时,主要问题就出现了。当我碰到那个时,我可以扔掉它的左右两侧。如果我碰到它的底部或顶部,我的播放器会卡住,只能向左或向右移动。

player.java

package com.questkings.game;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;

public class Player{

int x = 1; // Location of player
int y = 314; // location of player
int xa = 0; // Representation of where the player goes
int ya = 0; // Representation of where the player goes
private int speed = 2;
int[] playerPos = {x, y};
private static final int WIDTH = 30;
private static final int HEIGHT = 30;

private Game game;

public Player(Game game){
    this.game=game;
}

public void move(){
    if(x + xa < 0) // Left Bounds
        xa = 0;
    else if (x + xa > game.getWidth() - WIDTH) // Right Bounds
        xa = 0;
    else if (y + ya < 0) // Top Bounds
        ya = 0;
    else if(y + ya > game.getHeight() - WIDTH)
        ya = 0;
    else if (collision()) // Tile bounds
        ya = 0;
    else if (collision2())
        ya = 0;

    x = x + xa;
    y = y + ya;
}

// Method to find where player is located
public int[] Playerposition(){
    return playerPos;
}

public void paint(Graphics2D g2d){
    //Draws player to screen
    g2d.drawImage(getPlayerImg(), x, y, null);
}

public Image getPlayerImg(){
    ImageIcon ic = new ImageIcon("C:/Users/AncientPandas/Desktop/KingsQuest/Misc/Images/Sprites/player.png");
    return ic.getImage();
}

public void keyReleased(KeyEvent e){
    xa = 0;
    ya = 0;
}

public void keyPressed(KeyEvent e){
    if (e.getKeyCode() == KeyEvent.VK_S)
        xa = -speed;
    if (e.getKeyCode() == KeyEvent.VK_F)
        xa = speed;
    if (e.getKeyCode() == KeyEvent.VK_E)
        ya = -speed;
    if (e.getKeyCode() == KeyEvent.VK_D)
        ya = speed;
}

public Rectangle getBoundsPlayer(){
    return new Rectangle(x, y, WIDTH, HEIGHT);
}

private boolean collision(){
    return game.maplayout.getBoundsBlock().intersects(getBoundsPlayer());
}

private boolean collision2(){
    return game.maplayout.getBoundsBlock2().intersects(getBoundsPlayer());
}


}

MapLayout.java

package com.questkings.game;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;

import javax.swing.ImageIcon;

public class MapLayout {

int[] blockPlacementX = {0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300,
        330, 360, 390, 420, 450, 480, 510, 540, 570, 600, 630, 660, 690, 720,
        750, 780, 810};

int[] blockPlacementY = {344, 254};

@SuppressWarnings("unused")
private Game game;

public MapLayout(Game game){
    this.game=game;
}

//Map size 810(x), 420(y)

public void paint(Graphics2D g2d){
    for(int i = 0; i < blockPlacementX.length; i++){
        g2d.drawImage(getBlockIMG(), blockPlacementX[i], blockPlacementY[0], null);
    }
    for(int i = 13; i < blockPlacementX.length - 10; i++)
    {
        g2d.drawImage(getBlockIMG(), blockPlacementX[i], blockPlacementY[1], null);
    }
}

public Image getBlockIMG(){
    ImageIcon ic = new ImageIcon("C:/Users/AncientPandas/Desktop/KingsQuest/Misc/Images/Sprites/grassWall.png");
    return ic.getImage();
}

// Bug: Bounds are making it so I can not go back up if I hit block 344
public Rectangle getBoundsBlock(){
    return new Rectangle(0, 344, 810, 30);
}

public Rectangle getBoundsBlock2(){
    return new Rectangle(390, 254, 150, 30);
}
}

1 个答案:

答案 0 :(得分:0)

碰撞时,您将delta y设置为ya=0,这意味着发生碰撞时垂直移动(向上或向下)没有变化。你可能想在这里重新思考你想要的东西。