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

2016/11/5

Switch to the hitting animation and back to normal

theotherroom theotherroom

2016/11/5

#
I'm trying to make my actor hit, pretty much. Here is the relevant code for him:
 boolean hasSpatula = false;

String [] forwards = new String [3];
String [] back = new String [3];
String [] right = new String [3];
String [] left = new String [3];
int direction = 0;
int timer = 0;

public void act() 
    {
        isMoving();
        
        setArmed();
        
        if ( Greenfoot.isKeyDown("space") & hasSpatula == true )
        {
            if (direction == 1)
            {
                if (timer < 15)
                {
                    setImage("walt_back0hit.png");
                }
                
                else if (timer > 15 & timer < 30)
                {
                    setImage("walt_back0S.png");
                }
                else if (timer > 30)
                {
                    timer = 0;
                }
            }

        }
        
        counter++;
        timer++;
    }   

He switches to the first image (walt_back0hit.png) but doesn't switch back. The timer does not reset to 0, either. It just keeps counting. Why could it potentially be ignoring the second and third if statements? Thank you for any help whatsoever!
danpost danpost

2016/11/5

#
theotherroom wrote...
I'm trying to make my actor hit, pretty much. Here is the relevant code for him: < Code Omitted > He switches to the first image (walt_back0hit.png) but doesn't switch back. The timer does not reset to 0, either. It just keeps counting. Why could it potentially be ignoring the second and third if statements?
I believe the problem is that you are incrementing the timer unconditionally; but only checking its value under specific conditions. If you cut line 38 and paste (insert) it at the beginning of line 33, you might get better results.
theotherroom theotherroom

2016/11/5

#
Thank you, it worked!
You need to login to post a reply.