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

2020/7/6

actor's methods (changing images while moving) don't work anymore since i added this method

Lorenz123 Lorenz123

2020/7/6

#
Hi, my problem is named in the title. I hope someone can help me. Here is the method causing the problem:
private void sternBenutzen()
    {
        SuperStern superStern = (SuperStern) getOneIntersectingObject(SuperStern.class);
        Timer timer = new Timer();
        if(superStern != null && status == "figur")
        {
            statusAlt = status;
            status = "sternFigur";
            timer.timer = 1200;
            getWorld().addObject(timer, getWorld().getWidth()/2, getWorld().getHeight()/2);
            timer.setImage(new GreenfootImage("Zeit übrig : " + timer.timer/60, 20, Color.WHITE, new Color(0, 0, 0, 0 )));
            if(xRichtung == 1)
            {
                
                // setImage(sternRechtsBlickend);
            }

            if(xRichtung == -1)
            {
                
                // setImage(sternLinksBlickend);
            }
            superStern = null;
            getWorld().removeObject(superStern);
        }
        
        if(timer.timer == 0)
        {
            status = statusAlt;
            statusAlt = null;
        }
    }
If required: the actor "timer" just reduces the variable timer by 1 per act cycle if the value of the varible is over 0
danpost danpost

2020/7/6

#
Your code has a new Timer object created every time the method is called (see line 4). The variable "timer" always refers to a different Timer object on different act cycles. At minimum, line 4 needs to be cut and pasted back in outside the method (like, say, line 0).
You need to login to post a reply.