简体   繁体   English

Java Space Invaders游戏快速触发作弊触发游戏获胜太早

[英]Java Space Invaders Game Rapid Fire Cheat Triggering Game Win too Early

So I am new the very new programmer modifying/adding things to a already made space invaders game to get more familiar with how games are programmed. 所以我是新来的非常新的程序员,它在已经制作的太空入侵者游戏中修改/添加了东西,以更加熟悉游戏的编程方式。 I am trying to add cheats to the game and the rapid fire cheat is giving me some trouble. 我正尝试在游戏中添加作弊功能,快速的作弊行为给我带来了麻烦。 When you choose rapid fire and go through the level, the game can sometimes trigger the notifyWin early with some aliens still left. 当您选择快速开火并经过关卡时,游戏有时可能会提前离开一些外星人来触发notifyWin。 This is because sometimes 2 shots hit the same enemy at the exact same time making the computer think that 2 different enemies are being killed, thus leaving a few aliens sometimes left over while the "You have won!" 这是因为有时在同一时间有2发子弹击中相同的敌人,使计算机认为有2个不同的敌人被杀死,因此在“您赢了!”时有时会留下一些外星人 screen pops up. 屏幕弹出。 The win is triggered when the alien count reaches 0 shooting the same enemy twice reduces the alien count by 2 instead of 1. I can't for the life of me seem to figure this out. 当外星人数量达到0时触发胜利,射击同一名敌人两次将外星人数量减少2点而不是1点。

// reduce the alient count, if there are none left, the player has won
    alienCount--;

if (alienCount == 0)
    {
        notifyWin ();
    }

Here is the code where the shot collides with the enemy 这是枪击与敌人碰撞的代码

/**
     * Notification that this shot has collided with another
     * entity
     * 
     * @parma other The other entity with which we've collided
     */
    public void collidedWith(Entity other) {
            // prevents double kills, if we've already hit something,
            // don't collide
            if (used) {
                    return;
            }

            // if we've hit an alien, kill it!
            if (other instanceof AlienEntity) {
                    // remove the affected entities
                    game.removeEntity(this);
                    game.removeEntity(other);

                    // notify the game that the alien has been killed
                    game.notifyAlienKilled();
                    used = true;
            }
    }

One possible solution is to add the enemies as objects to a list. 一种可能的解决方案是将敌人作为对象添加到列表中。 When they have been shot, remove them from the list, checking that they are still there before hand. 当他们被射击后,将它们从列表中删除,并检查它们是否仍在手。 Do a notifyWin when the list is empty. 当列表为空时,执行notifyWin。 This way, if the logic doing the collision detection on the enemies generates too many hits, it will still work correctly. 这样,如果对敌人执行碰撞检测的逻辑产生了太多的打击,它仍然可以正常工作。

The chances are good that this list exists already in order to render the enemies on the screen. 为了在屏幕上呈现敌人,此列表已经存在的可能性很大。

EDIT : now that I see the method, you could probably just do the following at the top of collidedWith: 编辑 :现在,我看到了该方法,您可能只需在collidedWith顶部执行以下操作:

if (used || !game.containsEntity(other)) {
    return;
}

Where the method containsEntity checks for the existence of the entity in the internal list or structures that hold the game actors (entities). 在方法containsEntity所在的位置,将检查内部列表中是否存在该实体,或该内部列表中是否包含游戏参与者(实体)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM