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

2014/3/21

Don't stop animation if the key is not pressed.

Than4tos Than4tos

2014/3/21

#
    public void ataca()
    {
        if(Greenfoot.isKeyDown("space") && direção == 1)
        {
            indice = indice + 1;
            if(indice >= atacaDown.length )
            {
                indice = 0;
            }
            setImage(atacaDown[indice]);
        }
    }
So that's what i got but te animation only continues if im pressing the space bar how can the animation star and end with 1 press of the space bar? Would really apreciate the help another thing im using an array for the images if some1 needs to know.
lordhershey lordhershey

2014/3/21

#
set a flag to turn it on or off
boolean isOn = false;

   public void ataca()
    {
        if(Greenfoot.isKeyDown("space"))
        {
          isOn = !isOn;
        }
        if(isOn)
        {   
            indice = (indice + 1)%atacaDown.length;
            
            setImage(atacaDown[indice]);
        }
    }
this will toggle it, but if you hold the spacebar down it will stop and start the animation over and over again. If you tap the spacebar it will start. You can add a flag to prevent the key test from happening over and over again. like this:
boolean isOn = false;
boolean trap = false;

   public void ataca()
    {
        if(Greenfoot.isKeyDown("space") && !trap)
        {
          trap = true;
          isOn = !isOn;
        }
        else
        {
          trap = false;
        }

        if(isOn)
        {   
            indice = (indice + 1)%atacaDown.length;
            
            setImage(atacaDown[indice]);
        }
    }
or something like that.
Than4tos Than4tos

2014/3/21

#
The image goes into a loop still trying too find a way arround.
lordhershey lordhershey

2014/3/21

#
maybe I mis-understood, do you want it to stop after it hits the last image?
Than4tos Than4tos

2014/3/21

#
Yes, sorry abou that bit.
lordhershey lordhershey

2014/3/21

#
then maybe
boolean isOn = false;

   public void ataca()
    {
        if(Greenfoot.isKeyDown("space") && !isOn)
        {
            isOn = true;
            indice = 0;
            //show the first image, if the first image is already showing then you can comment out the next 2 lines
            setImage(atacaDown[indice]);
            return;
        }

        if(isOn)
        {   
            indice++;
            if (indice >= atacaDown.length)
            {
                //this stops the animation and sets it 
                //back to the first picture
                isOn = false;
                indicie = 0;
            }
            setImage(atacaDown[indice]);
        }
    }
you hit the spacebar once, the animation will cycle once and leave you on the first frame when it is done.
Than4tos Than4tos

2014/3/21

#
Yep thx, i was actually thinking about something similiar on the bus on my way home.
You need to login to post a reply.