阻止精灵通过另一个精灵重影 [英] Stop sprite from ghosting through another sprite

查看:107
本文介绍了阻止精灵通过另一个精灵重影的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好,所以我才刚刚开始学习Java(我通常在Objective-C中编程).我的第一款游戏是类似于《神奇宝贝》的游戏,但是显然它要简化得多...

Ok so I've just started learning java (I usually program in Objective-C). My first game is a game similar to Pokémon, however, its a lot more simplified obviously...

我遇到的麻烦是我无法找到一种方法来阻止2个精灵互相鬼影".在屏幕上,我设置了边框(边界),一个播放器精灵和一个敌人精灵.

The trouble I'm having is I can't find a way to stop 2 sprites from 'ghosting' through each other. On screen I have borders set up (boundaries), A player sprite, and an Enemy sprite.

public void playerUpdate(GameContainer gc, int delta) throws SlickException
{
    Input input = gc.getInput();

    // Right Key Pressed
    if (input.isKeyDown(Input.KEY_RIGHT) && (leftKeyPressed == false)
            && (upKeyPressed == false) && (downKeyPressed == false))
    {
        player = walkRight;
        playerX += speed * delta;
        rightKeyPressed = true;
        if (playerX >= Main.getWindowWidth() - pImageWidth)
        {
            playerX -= speed * delta;
        }
    } else if (rightKeyPressed == true) 
    {
        player = standRight; 
        rightKeyPressed = false;
    } 

^^这是我需要实现碰撞检测的地方. 我为每个图像添加了矩形以进行碰撞检测,但是,我并没有想办法使图像消失.我需要一种阻止一个精灵穿过另一个精灵的方法.

^^ this is where I need to implement the collision detection. I have added rectangles to each image for collision detection, however, I'm not after a way to make one disappear. I need a way to stop one sprite from walking through another.

有什么想法吗?

我尝试使用

if (this.playerBoundingBox.intersects(Enemy.getEnemyBoundingBox())
{
    playerX += speed * delta;
}

但是,当我实现此功能时,播放器会卡住,无法释放.

However, when i implement this the player gets stuck and can not be freed.

谢谢大家

推荐答案

冲突检测是一个广泛而深入的主题,并且有很多方法可以实现它.

Collision detection is a broad and deep topic, and there are many ways to implement it.

我强烈建议您阅读 The实施2D平台程序指南,它应该为您提供一些很好的建议.我已经使用《声波复古物理学指南》 实现了2D平台引擎,这真的很有用.

I'd strongly recommend reading The Guide To Implementing 2D Platformers, which should give you some great advice. I've implemented a 2D platform engine using the Sonic Retro Physics Guide, which was really useful.

在我的游戏 Clover:Curious Tale 中,我(不必要!)实现了每个像素碰撞与边界框的更复杂的混合.该方法是找出所需的移动路径,然后逐个像素检查以查看是否有任何障碍-如果存在,则仅移动至该像素减去一.

In my game Clover: A Curious Tale I (needlessly!) implemented a more complicated hybrid of per-pixel collision with bounding boxes. The approach was to work out the desired movement path, and then check pixel-by-pixel to see if anything was in the way - if it was, only move as far as that pixel-minus-one.

创建在所有情况下均无瑕疵的2D引擎是一项艰巨的任务,而不是您应该尝试的事情.对演员的身材等事物有一些限制,任何事物都可以在一个瞬间移动的最大速度,将使您的生活更轻松.省略可以推动玩家的事情会容易得多,因为您只需一次且仅在一个方向"(即,玩家移动时,而不是其他所有角色移动时)进行碰撞检测.

Creating a 2D engine that is flawless in all circumstances is a very tall order, and not something you should attempt. Having some limits on things like size of an actor, and the maximum speed anything can travel in a single tick will make your life easier. Omitting things that can push the player will be much easier, as then you only have to do the collision detection once and in one 'direction' (ie when the player moves, rather than when all the other actors move).

这篇关于阻止精灵通过另一个精灵重影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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