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

2020/2/28

Trying to create a blinking image

DaRafster DaRafster

2020/2/28

#
I've created a delay timer for my actor to create a blinking "start game" button, but when I run the game it doesn't blink. Here is my code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class StartGameButton here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class StartGameButton extends Actor
{   
    private int delay = 0;
    /**
     * Act - do whatever the StartGameButton wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        startingGame();
        blinkingImage();
    }
    
    public void setDelay(int d)
    {
        delay = d;
    }
    
    public void blinkingImage()
    {
       
       GreenfootImage button = getImage();
       int t = button.getTransparency();
       button.setTransparency(0);
       setDelay(100);
       if (delay>0)
        {
            delay--;
            return;  
        }
       button.setTransparency(t);
 
    }
    
    public void startingGame()
    {
        if(Greenfoot.mouseClicked(this))
        {
            Greenfoot.setWorld(new GuessThatPokemon());
        }
    }
}
danpost danpost

2020/2/28

#
Use 200 for max delay value, delay%100 == 0 for condition to change image and 255*(delay/100) to set the transparency.
DaRafster DaRafster

2020/2/28

#
danpost wrote...
Use 200 for max delay value, delay%100 == 0 for condition to change image and 255*(delay/100) to set the transparency.
I don't quite understand what you mean. Can you point out the line numbers that need changes and what to change within those lines? Sorry, got beginner knowledge
danpost danpost

2020/2/28

#
Basically:
/** with field */
private int delay;

/** in code */
delay = (delay+1)%200; // run "timer"
if (delay%100 == 0) // if time to change image transparency
{
    getImage().setTransparency(255*(delay/100)); // change image transparency
}
You need to login to post a reply.