2014-11-28 11 views
0

Итак, когда я сталкиваюсь с 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); 
} 
} 
+0

Вы меняете только «ya», когда происходит одно из столкновений. Как это могло повлиять на горизонтальное движение? – fabian

ответ

0

В столкновении, вы настраиваете дельту y в ya=0, который не означает, что никаких изменений в вертикальное движение (вверх или вниз), когда происходит столкновение. Вы, вероятно, захотите переосмыслить то, что вы хотите здесь.

Смежные вопросы