This site requires JavaScript, please enable it in your browser!
Greenfoot back
ErasedBlade
ErasedBlade wrote ...

2019/5/9

creation of text images causing lag

ErasedBlade ErasedBlade

2019/5/9

#
i just added in custom text to display the players remaining health, how many times the enemy can respawn, and the ufo's health (enemy), however since using this i have been experiencing slower gameplay. does anyone know why this could be? the code that updates the images is what i believe is causing it. the method is being called in a subclass of this level
    /**
     * returns remaining enemy hp value
     */
    public int getHpE(int amt)
    {
        int section1 = getObjects(Section1.class).size();
        hpE = hpE- amt;
        if (hpE != 0 || section1 == 0)
        {
            myText.updateImage(hpE);
            removeObject(myText);
        }
        return hpE;
    }
Can I see the code called by updateImage? It looks like the problem is there.
Super_Hippo Super_Hippo

2019/5/10

#
Why do you update its image and then remove myText immediately afterwards? And why is size=0 a condition to execute it?
ErasedBlade ErasedBlade

2019/5/10

#
i have it with size 0, only because the section1 class has a respawn in some levels. I just realized that since im calling it in the respawn method, i dont need that. and second off im updating it and removing it imeadiatly afterwards, because prior to that it would still consecutively creating text images in any of the modification methods. the updateImage methods code is as fallows.
    public void updateImage(int var)
    {
        this.var = var;
        int x=1000, y=100;
        GreenfootImage img = new GreenfootImage(x, y);
        img.setColor(Color.CYAN);
        img.setFont(new Font("OptimusPrinceps", style, size));
        img.drawString(pre + var, x/2, y/4);
        setImage(img);
    }
Super_Hippo Super_Hippo

2019/5/10

#
If you instantly remove it, you won't see the effect though.
ErasedBlade ErasedBlade

2019/5/10

#
before it would actually flicker off and on due to the removing, updating, then readding. I would of worked on that later.,but to solve it I tried removing the methods that interact with the text from the act method which did speed it.
Super_Hippo Super_Hippo

2019/5/10

#
I mean if you constantly remove the myText object, you need to readd it somewhere... you should probably just add it once.
Zamoht Zamoht

2019/5/11

#
As mentioned by others you should avoid removing and adding objects. However I believe the slowdown happens because you create a new GreenfootImage that is quite large. Two ways you can improve this, if possible make the image smaller than 1000x100, and instead of creating a new image every time, you could store the image in an instance field and update it as needed.
You need to login to post a reply.