2D游戏设计 - 角色穿过墙壁

时间:2013-01-23 07:12:02

标签: java 2d

所以我想用2D编写游戏代码。我有一个班级为我的角色。但是,当我玩游戏时一切都很好,直到我试着走到墙上。玩家可以走进墙(左侧[使用'd'键]),应用程序将玩家推到墙的另一侧。我不确定问题是什么,因为截至目前代码中没有错误..

package net.ferrell.wrathoftuemdaym;

import java.awt.*;

public class Character extends DoubleRectangle {
    public double fallingSpeed = 1.3;
    public double movingSpeed = 0.65;

    public Character(int width, int height) {
        setBounds((Component.pixel.width / 2) - (width / 2),(Component.pixel.height / 2) - (height / 2), width, height);

    }

    public void tick() {
        if(!isCollidingWithBlock(new Point((int) x, (int) (y +  height)), new Point((int) (x + width), (int) (y + height)))) {
            y += fallingSpeed;
            Component.sY += fallingSpeed;
        }
        if(Component.isMoving) {
            boolean canMove = false;

            if(Component.dir == movingSpeed) {


            } else if(Component.dir == -movingSpeed) {
                canMove = isCollidingWithBlock(new Point((int) x - 1, (int) y), new Point((int) x - 1, (int) (y + (height - 2))));

            }

            if(!canMove) {
                x += Component.dir;
                Component.sX += Component.dir;
            }

            x += Component.dir;
            Component.sX += Component.dir;
        }
    }

    public boolean isCollidingWithBlock(Point pt1, Point pt2) {
        boolean isColliding = false;

        for(int x = (int) (this.x / Tile.tileSize);x < (int) (this.x / Tile.tileSize) + 3; x++) {
            for(int y = (int) (this.y / Tile.tileSize);y < (int) (this.y / Tile.tileSize) + 3; y++) {
                if(x >= 0 && y >= 0 && x < Component.Level.block.length && y < Component.Level.block[0].length)
                if(Component.Level.block[x][y].id != Tile.air) {
                if(Component.Level.block[x][y].contains(pt1) || Component.Level.block[x][y].contains(pt2)) {
                    return true;

                    }
                }
            }
        }
        return false;
    }

    public void render(Graphics g) {
        g.drawImage(Tile.tileset_terrain, (int) x - (int) Component.sX, (int) y - (int) Component.sY, (int) (x + width) - (int) Component.sX, (int) (y + height) - (int) Component.sY, Tile.character[0] * Tile.tileSize, Tile.character[1] * Tile.tileSize, Tile.character[0] * Tile.tileSize + (int) width, Tile.character[1] * Tile.tileSize + (int) height, null);

}
}

我认为错误与我的tick方法有关。 请记住,代码是一个WIP(正在进行中),所以我错过了像移动速度但是'+'movingspeed的位,但这是正确的一面,我现在在左边工作。

如果你需要我其他课程的代码,请说明,我会提供。

0 个答案:

没有答案
相关问题