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

2016/6/26

Flashing text at startwindow

Undead Undead

2016/6/26

#
I try to create an one second delay between alternating the transparency levels of img2 but it doesn't even reach point 1
long timer = 0;
boolean visible= true;
public void alternating()
    {
        timer = System.currentTimeMillis();
        System.out.println("Point 0");
        if (visible == true){
            
            if (System.currentTimeMillis() - timer >= 1000)
            {
                img2.setTransparency(0);
                visible = false;
                timer = 0;
                System.out.println("Point 1");
                alternating();
            }
        } else {
            
            if (System.currentTimeMillis() - timer >= 1000)
            {
                img2.setTransparency(255);
                visible = true;
                timer = 0;
                System.out.println("Point 2");
                alternating();
            }
        }
    }
Class located in my main world. Any suggestions how to fix it ? Undead
danpost danpost

2016/6/26

#
Lines 13 and 23 are setting the 'timer' field to zero. Is that really what you want to do there? Another thing is that lines 15 and 25 are calling the method that is currently executing. I do not think that it is wise to call a method from within itself (unless you know exactly what it is doing to break out of what could be an infinite loop). In this case, since the real time is being used as a condition within the method, it does break out of the method (no infinite looping will occur); however, I do not see what calling the method inside itself will accomplish above and beyond what the method does without doing this. Maybe you are calling the method from the constructor of your world. Are you trying to have the text flash even before the scenario is started (before the 'Run' button is clicked, for example)? Maybe you should post the world class code for the start window so that we can break down the problem to its roots.
Undead Undead

2016/6/26

#
The loop is intended
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.awt.Font;


/**
 * Write a description of class Menü here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Menü extends World
{
    long timer = 0;
    boolean sichtbar= true;
    GreenfootImage img1 = new GreenfootImage("Menü1.gif");
    GreenfootImage img2 = new GreenfootImage("Press ENTER To Start",50, Color.WHITE, null); 
    /**
     * Constructor for objects of class Menü.
     * 
     */
    public Menü()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.

        super(1280, 720, 1);
        
       

        Menülayout menülayout1 = new Menülayout(img1);
        Menülayout menülayout2 = new Menülayout(img2);
        addObject(menülayout1, getWidth()/2, getHeight()/2);
        addObject(menülayout2, getWidth()/2, getHeight()/2+300);
        timer = System.currentTimeMillis();
         
        alternating();
    }

   
  
    public void act() 
    {
        if (Greenfoot.isKeyDown("enter"))
        {
            Greenfoot.setWorld(new Menü2());
        }
        
        
        
        //Greenfoot.playSound("soundFile.wav");
    }
     
    
    public void alternating()
    {
        timer = System.currentTimeMillis();
        if (sichtbar == true){
            System.out.println("Point 0");
            if (System.currentTimeMillis() - timer >= 1000)
            {
                img2.setTransparency(0);
                sichtbar = false;
                timer = 0;
                System.out.println("Point 1");
                alternating();
            } 
        } else {
            
            if (System.currentTimeMillis() - timer >= 1000)
            {
                img2.setTransparency(255);
                sichtbar = true;
                timer = 0;
                System.out.println("Point 2");
                alternating();
            } 
        }
    }
}
I hope the german parts don't confuse you too much
danpost danpost

2016/6/26

#
Copy line 36 and paste it at line 48; then change line 36 to this:
Greenfoot.start();
Remove the iterative calls to 'alternating' (lines 65 and 75). Also, change lines 63 and 73 to match line 34. This will have the act method, which is called continuously, control the flashing text while the scenario is running.
danpost danpost

2016/6/26

#
It should be possible to reduce the size of the 'alternating' method. Maybe something like this:
private void alternating()
{
    if (System.currentTimeMillis()-timer < 1000) return;
    timer = System.currentTimeMillis(); // reset start time
    im2.setTransparency((1-img2.getTransparency()/255)*255); // change image transparency
}
This eliminates the need for the 'sichbar' boolean field and makes the code easier to follow.
Undead Undead

2016/6/26

#
@danpost Thanks a lot !
danpost danpost

2016/6/26

#
Undead wrote...
@danpost Thanks a lot !
There was another issue with your original code which I just spotted. The first instruction in the 'alternating' method, line 56, sets the value of 'timer' to the system time . With that line, the conditions on lines 59 and 69 will never be 'true'.
You need to login to post a reply.