LibGdx What is the proper way to copy an Actor?

偶尔善良 提交于 2019-12-13 04:07:02

问题


I have a custom implementation of Image that animates. I want to make multiple copies of an instance of this class using the minimum memory and processor time for rendering and copying. How do I go about it? Is it just easiest to recreate it whenever I need another one or is there some proper or suggested method for copying Image instances. I ask this because I can't find a copy constructor and I don't know if .clone() is implemented in Image.


回答1:


If you really want to take care about Memory you can give all actors the same Image. But if you change the image in some way and at one of the Actors it does change at all actors with it(I did it with a sprite and changed the TextureRegion. All my Monsters looked at the same direction). But if you simply just have the same Image the whole time you can create your Objects and give all the same reference to one Image.
For example like this:

public ArrayList<Actor> generateImageActor(){
        ArrayList<Actor> temp = new ArrayList<Actor>();
        Image img = new Image(____);
        for(int i = 0; i <10; i++){
            MyActor act = new MyActor(img);
            temp.add(act);
        }
        return temp;
    }


The rendertime does not get any effect if you refare to one image or to always a new image. I simply does take the picture and its offsett and draws it. So if you refare to 1 image at 10 actors it has the same rendertime else if you would have 10 actors with a copy of the image.
Hope this helps.



来源:https://stackoverflow.com/questions/16119522/libgdx-what-is-the-proper-way-to-copy-an-actor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!