简体   繁体   English

如何在 Java 中将链接列表与我的处理程序 class 一起使用?

[英]How can I use a Linked list with my Handler class in Java?

I am making a simple game which has an array of objects called beats which move along the screen towards a stationary player like a basic rhythm game, and I have decided that using a linked list is the best way to track the nearest beat to the player我正在制作一个简单的游戏,它有一组称为节拍的对象,它们沿着屏幕向静止的玩家移动,就像基本的节奏游戏一样,我决定使用链表是跟踪离玩家最近的节拍的最佳方式

Currently I am trying to add to the linked list, and have this as a starting point:目前我正在尝试添加到链表中,并以此为起点:

     public static LinkedList<Beat> beatLinkedList = new LinkedList<Beat>();

The linked list is public and static as it needs to be accessed in a different class.链表是公共的,static 因为它需要在不同的 class 中访问。

        int startingPoint = 800;
        //For loop 51 times
        for(int i=0;i<=50;i++){
            //modifier to start position to create differing gaps between beats
            int startModifier = random.nextInt(50);


            beatLinkedList.add(new Beat(startingPoint,300,ID.Beat));
            System.out.println(beatLinkedList.get(0));

            //redefines the starting point for each beat
            startingPoint = startingPoint+50+startModifier;

        }
    }

When I want to render the objects however I need to use the handler class, to add the objects to a linked list of gameobjects which has the render method called on them which I would like to keep the same ideally meaning I need to use the handler classes addObject method:但是,当我想渲染对象时,我需要使用处理程序 class 将对象添加到游戏对象的链接列表中,该链接列表中调用了渲染方法,我希望保持相同的理想状态,这意味着我需要使用处理程序类addObject方法:

 public void addObject(GameObject object){
        //linked list built in method to add an object
        this.object.add(object);
    }

For using an array this solution worked对于使用数组,此解决方案有效

But something similar for the linked list does not但是对于链表来说类似的东西并没有

 handler.addObject(beatLinkedList.add(new Beat(startingPoint,300,ID.Beat)));

I should also add that when I get the element at position 0 it outputs all 51 objects which is also a problem.我还应该补充一点,当我在 position 0 处获得元素时,它会输出所有 51 个对象,这也是一个问题。

With trying to use the handler like this I am given an exception saying "addoObject in Handler cannot be applied to boolean" on line 50:尝试像这样使用处理程序时,我在第 50 行遇到了一个异常,说"addoObject in Handler cannot be applied to boolean"

 handler.addObject(beatLinkedList.add(new Beat(startingPoint,300,ID.Beat)));

I'm not sure about this but it seems that I need to give a pointer to a location when using the add method and so I tried this:我不确定这一点,但似乎我需要在使用 add 方法时提供一个指向位置的指针,所以我尝试了这个:

beatLinkedList.addLast(new Beat(startingPoint,300,ID.Beat));

And this method still has the problem of adding all 51 objects to the same point in the list, which I imagine is something to do with being in a for loop still.而且这种方法仍然存在将所有 51 个对象添加到列表中的同一点的问题,我想这与仍然处于 for 循环中有关。

    handler.addObject(beatLinkedList.addLast(new Beat(startingPoint,300,ID.Beat)));

Trying to utilise this with my handler class results in a different error from before saying that the 'void' type is not allowed as a parameter.尝试将其与我的处理程序 class 一起使用会导致与之前说“void”类型不允许作为参数不同的错误。 I'm really not sure where to go from here.我真的不知道 go 从这里到哪里。

    beatLinkedList.addLast(new Beat(startingPoint,300,ID.Beat));  
handler.addObject(beatLinkedList.getLast());

Solved thanks to Abra's comment, I decided to add the object to 2 seperate linked lists, one of just the beats, and one containing all the game objects, which allows them to be rendered properly, while also allowing beats to be identified and removed when necessary.由于 Abra 的评论解决了,我决定将 object 添加到 2 个单独的链表中,其中一个是节拍,一个包含所有游戏对象,这样可以正确渲染它们,同时还可以在以下情况下识别和删除节拍必要的。

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

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