libGDX - 试图实现2精灵之间的碰撞 [英] libGDX - Trying to implement collision between 2 sprites

查看:172
本文介绍了libGDX - 试图实现2精灵之间的碰撞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么标题说基本上

有关当前游戏背景位:球员总是从左至右,如果屏幕被窃听他跳,如果它拖着他飞奔向前迈进。我加了瓦(平台对象),我现在试图让我的球员停止,如果他打为之感动。

Bit of background about the current game: Player is always moving from left to right, if the screen is tapped he jumps if it's dragged he dashes forward. I added a tile (Platform object) and am now trying to make my Player stop moving if he hits it.

播放器开始在屏幕的左下方,并且平台是在右上角。什么情况是,玩家当他到达屏幕的中间,而不是实际的平台停止。任何人都可以看看我的code和看看他们是否能找出原因?将是非常美联社preciated。

The Player starts on the bottom left of the screen, and the Platform is on the top right. What happens is that the Player stops when he reaches the middle of the screen, rather than the actual Platform. Can anyone have a look at my code and see if they can figure out why? Would be highly appreciated.

public class Platform extends GameObject {
private Rectangle playerRect;
private Rectangle platformRect;
private Player player;
private boolean isOverlapping;

public Platform(Sprite spr) {
    super(spr);
    player = Player.getInstance(null); // Initialises the Player class (a Singleton)
// Set position of sprite
    setxPos(getWidth() - 400);
    setyPos(getHeight() / 2 - 100);
    spr.setX(getxPos()); 
    spr.setY(getyPos()); 

}

public void update() {
// Rectangle of Player
    playerRect = new Rectangle(player.getxPos(), player.getyPos(), player
            .getSprite().getWidth() + player.getxPos(), player.getSprite()
            .getHeight() + player.getyPos());
// Rectangle of Platform
    platformRect = new Rectangle(getxPos(), getyPos(), getSprite().getWidth()
            + getxPos(), getSprite().getHeight() + getxPos());

// Make Player stop moving if the two rectangles collide
    isOverlapping = playerRect.overlaps(platformRect);
    if (isOverlapping) {
        player.setxSpeed(0);

    }
}
    }

一直停留在这一段时间,在此先感谢任何输入

Have been stuck on this for a while, thanks in advance for any input

推荐答案

是你可以看到如果矩形的地方,你想,如果玩家通过碰撞或以其他方式尝试一些车站,我把它放在更新,但你可以把其他地方借鉴等。

is what you can see if the rectangles are where you think and if the player stops by colliding or otherwise try something, I put it in update but you can put anywhere else draw ect.

我希望你能理解。

变量类。

   private ShapeRenderer sRDebugRectangelPlayer = new ShapeRenderer();
   private ShapeRenderer sRDebugRectangelPlatform = new ShapeRenderer();

   private Rectangle playerRect = new Rectangle();
   private Rectangle platformRect = new Rectangle();

您的方法

public Platform(Sprite spr) {
super(spr);
player = Player.getInstance(null); // Initialises the Player class (a Singleton)
// Set position of sprite
setxPos(getWidth() - 400);
setyPos(getHeight() / 2 - 100);
spr.setX(getxPos()); 
spr.setY(getyPos()); 

//add

// Rectangle of Player
playerRect = new Rectangle(player.getxPos(), player.getyPos(),
                           player.getSprite().getWidth(),
                           player.getSprite().getHeight());

// Rectangle of Platform
platformRect = new Rectangle(getxPos(), getyPos(), 
                             getSprite().getWidth(), 
                             getSprite().getHeight());
}

您的方法

public void update() {

playerRect.setPosition(player.getxPos(), player.getyPos());
platformRect.setPosition(getxPos(), getyPos());



// Make Player stop moving if the two rectangles collide

isOverlapping = playerRect.overlaps(platformRect);

if (isOverlapping) {
        player.setxSpeed(0);
}
}

//更新中增加对测试或吸取

//add for test in update or draw

sRDebugRectangelPlayer.begin(ShapeType.Filled);
sRDebugRectangelPlayer.identity();

sRDebugRectangelPlayer.rect(player.getxPos(), player.getyPos(),
                           player.getSprite().getWidth(),
                           player.getSprite().getHeight());

sRDebugRectangelPlayer.end();

//添加对测试更新或绘制

//add for test update or draw

sRDebugRectangelPlatform.begin(ShapeType.Filled);
sRDebugRectangelPlatform.identity();

sRDebugRectangelPlatform.rect(getxPos(), getyPos(), 
                             getSprite().getWidth(), 
                             getSprite().getHeight());

sRDebugRectangelPlatform.end();

这篇关于libGDX - 试图实现2精灵之间的碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆