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

2012/9/5

How can I make a pause between two action of methods (in Act method)?

1
2
3
ManiHallam ManiHallam

2012/9/5

#
Hi, Could you please help me with this. how can I make a pause for one second between two action of objects or methods. I tried these two methods "wait" and "delay", but they didn't work. the code which I need a method to make a pause or delay for one second, is as below:
public class FivePence extends Money
{
    private int n;

    /**
     * Act - do whatever the FivePence wants to do.
     */
    public void act() 
    {
        insertMoney();
        cr5();
    }

    /**
     * Moving the coin into the Coin Insertion, when this coin is clicked by mouse.
     */
    public void insertMoney() 
    {
        if (Greenfoot.mouseClicked(this)) {
            setLocation(517, 314);
        }
    }

    /**
     * 
     */
    public void cr5()
    {
        int n;
        n = 5;
        getImage().drawString("Credit: " + n, 592, 170);
    }
    
    /**
     * 
     */
    public void getBack()
    {
        World world;
        world = getWorld();
        getWorld().addObject(this, 589, 416);
    }
}
Thanks,and it is appreciated in advance. :-)
MatheMagician MatheMagician

2012/9/5

#
I am slightly confused as to what you need. If you want the pause just to pause this one actor and not the rest of them, paste this code right above act():
long pauseTime = 0; 
    public boolean running()
    {
        long time = System.currentTimeMillis();
        return time > pauseTime;
    }
    public void pause(long delay)
    {
        long time = System.currentTimeMillis();
        pauseTime = time+delay;
    }
Then, in your act cycle, put this code.
if(running())
{
        insertMoney();  
        cr5();
}
Now, for this code to work, you will need to start it pausing using the method
pause(long delay);
when you call this method, the running() method will return false for the specified amount of milliseconds. In your case, you probably want to call pause(1000); to pause for a second.
MatheMagician MatheMagician

2012/9/5

#
I have posted a demo of this on the sight called "Pause".
Gevater_Tod4711 Gevater_Tod4711

2012/9/5

#
Im not sure what you want. If You want to stop the acting of only one object MatheMagicans way probably is the best. But if you want all objects to stop for one second you should use Greenfoot.delay(milliseconds). If this doesn't work you also could try this:
try {
    Thread.currentThread().sleep([i]milliseconds[/i]);
}
catch(InterruptedException ie) {
    ie.printStackTrace(); // only use this line if you want the exception to be printed on the terminal.;
}
I'm not sure if this only stops the Tread of the object or of whole Greenfoot. You have to try this.
MatheMagician MatheMagician

2012/9/5

#
Thanks Gevater_Tod4711 for adding that.
danpost danpost

2012/9/5

#
I am thinking that an instance boolean field might help for control, here (call it 'clickedOn'). On second thought, instead of a boolean, add a Long to hold the start time of your delay and set it to zero. When it is zero, check for clicks; and when not zero, check timer. The class code should be more like the following:
public class FivePence extends Money
{
    private int n = 5;
    private Long markTime = 0;

    public void act() 
    {
        insertMoney();
        cr5();
    }

    public void insertMoney() 
    {
        if (markTime == 0 && Greenfoot.mouseClicked(this))
        {
             setLocation(517, 314);
             markTime = System.currentTimeMillis();
        }
     }

    public void cr5()
    {  // checks for a one second delay time
        if (markTime != 0 && System.currentTimeMillis() - markTime > 1000)
        {
            markTime = 0;
            getImage().drawString("Credit: " + n, 592, 170);
        }
    }
}
I removed your 'getBack()' method. I was flawed, as well as not being used. In line 41, you are trying to add the object into the world; a world you are trying to get with 'getWorld()'. When the object is not in the world, 'getWorld()' will return 'null', and you will get a 'nullPointerException' error. As a side note, lines 39 and 40 get a reference to the world in 'world', but you did not use it in line 41, but tried calling 'getWorld()' directly. For example: let us say you were adding a different object into the world with that method (not 'this'). You could write
World world;
world = getWorld();
world.addObject(new FivePence(), 589, 416);
or simply
getWorld().addObject(new FivePence(), 589, 416);
Either code-set would add a new coin into the world, while keeping the one that was clicked on. You can then remove 'this' with 'getWorld().removeObject(this);' if you wanted the clicked on coin removed. Once it is removed, you lose the ability to have use of 'this.getWorld()' (the 'this.' part being understood). However, if you had created your new FivePence first, and then added it to the world as follows:
FivePence fivepence = new FivePence();
getWorld().addObject(fivepence, 589, 416);
Now, 'fivepence.getWorld()' would return the world. I do not know if you wanted the text 'Credit: 5' on the coin being inserted or on the coin that was clicked on; or even if they are the same coin or not. But, if goes on the inserted coin you can, after using the code last listed, write 'fivepence.getImage().drawString("Credits: " + n, 592, 170);'. Also using (592, 170) to draw the string at would never show on the coin. Those coordinates are offsets from the top-left corner of the image itself, whose coordinates are (0, 0). For the whold text to show, the coordinates should be in the ranges of (0 to getImage().getWidth() - textWidth, textHeight to getImage().getHeight() - 1); provided that the image is large enough for the text. Hope I have been of some help. Hope this helps.
ManiHallam ManiHallam

2012/9/5

#
Thanks MatheMagician. just, I have a problem about moving back the object to the first location before clicking on it, which is (589, 416). briefly, the method getBack(); in Act does not work. Also, I don't know, why the cr5() method is not invoked, which is supposed to draw the string, ("Credit: "+ n) in the specified location. Thanks again for your help. just to have a better picture, let you know that, this is for programming a Chocolate Dispenser Machine.
public class FivePence extends Money
{
    private int n;
    long pauseTime = 0; 

    /**
     * 
     */
    public boolean running()  
    {  
        long time = System.currentTimeMillis();  
        return time > pauseTime;  
    }  

    public void pause(long delay)  
    {  
        long time = System.currentTimeMillis();  
        pauseTime = time+delay;  
    }  

    /**
     * Act - do whatever the FivePence wants to do.
     */
    public void act() 
    {
        insertMoney();
        cr5();
        if(running())  
        {  
            insertMoney();    
            cr5();  
        }  
        pause(1000);
        getBack();
    }

    /**
     * Moving the coin into the Coin Insertion, when this coin is clicked by mouse.
     */
    public void insertMoney() 
    {
        if (Greenfoot.mouseClicked(this)) {
            setLocation(517, 314);
        }
    }

    /**
     * 
     */
    public void cr5()
    {
        int n;
        n = 5;
        getImage().drawString("Credit: " + n, 592, 170);
    }

    /**
     * 
     */
    public void getBack()
    {
        getWorld().addObject(this, 589, 416);
    }
}
Best Regards, ManiHallam
danpost danpost

2012/9/5

#
When, exactly, do you want the pause to take place? (between what actions)
Gevater_Tod4711 Gevater_Tod4711

2012/9/5

#
maybe you could use:
public void getBack() {
    setLocation(589, 416);
}
instead of your getBack() method. And the methods insertMoney() and cr5() will still be invoked after every act. Only if one second is over it'll be invoked two times in one act. Don't know if this is wat you want, but to me it seams to be very pointless.
ManiHallam ManiHallam

2012/9/5

#
Dear Gevater_Tod4711, I have actually tried this method before, unfortunately, didn't work, and got error. This is for a Chocolate Dispenser, which here I have some coins and some notes to insert into the machine. So I just need to sort the problem out for one of them, then others will be the same code, except for calculating and displaying the price.
ManiHallam ManiHallam

2012/9/5

#
wow, dear me! Thanks ever so much for your help and showing interest to help me. You guys are so faster than me! LoL
ManiHallam ManiHallam

2012/9/5

#
Dear Gevater_Tod4711, If I do that, when running the application, the coin does not move at all. Because of just the problem for the pause or delay. If it could stay on Coin Insertion location for 1 second, yes, you would be right. By the way, Thanks again. :-)
ManiHallam ManiHallam

2012/9/5

#
Dear Danpost, Thanks. But n is the value of the coins or notes in their own class, which is gonna to be counted in Counter class. So the the value is not always same, it depends on user that, which type of money he chooses: 5p, 10p, 20p, 50p, £1, £2, £5, £10, and £20.
ManiHallam ManiHallam

2012/9/5

#
@Danpost, The object has been added to the world as preparation for the world. This is the world class which is ChocolateDispenser:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 *    Chocolate Dispenser 
 *  @ 
 * 
 *  @ Author: Mani
 *  @ Version: 1.0.0
 */
public class ChocolateDispenser extends World
{
    
    /**
     * Constructor for our world ChocolateDispenser, and prepare it for starting the program.
     * 
     * Create the world with 722x640 cells with a cell size of 1x1 pixels.
     */
    public ChocolateDispenser() 
    {   
        super(722, 640, 1);

        prepareBody();
        prepareKeyboard();
        prepareMoney();
        prepareChocolates();
        preparePrices();

    }
    
    /**
     * Act - do whatever the Key0 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        // Add your action code here.

    }

    /**
     * "Body" Objects and add them to the world, to prepare the world for the start of the program.
     * 
     */
    private void prepareBody()
    {
        addObject(new CoinInsertion(), 517, 314);
        
        addObject(new NoteInsertion(), 663, 312);

        addObject(new Screen(), 593, 252);
        
        addObject(new UpAdvertPanel(), 592, 170);

        addObject(new AdvertDisplay(), 235, 37);

        addObject(new ChangeCollection(), 654, 440);

        addObject(new ChocolateCollection(), 233, 550);

        addObject(new ChocolateAdvertBoard(), 592, 70);
    }

    /**
     * "Keyboard" Objects and add them to the world, to prepare the world for the start of the program.
     * 
     */
    private void prepareKeyboard()
    {
        addObject(new Key0(), 592, 367);

        addObject(new Key1(), 570, 313);

        addObject(new Key2(), 592, 313);

        addObject(new Key3(), 614, 313);

        addObject(new Key4(), 570, 331);

        addObject(new Key5(), 592, 331);

        addObject(new Key6(), 614, 331);

        addObject(new Key7(), 570, 349);

        addObject(new Key8(), 593, 349);

        addObject(new Key9(), 614, 348);

        addObject(new KeyC(), 571, 367);

        addObject(new KeyE(), 614, 366);
    }

    /**
     * "Money" Objects and add them to the world, to prepare the world for the start of the program.
     * 
     */
    private void prepareMoney()
    {
        addObject(new TwentyPounds(), 506, 573);

        addObject(new TenPounds(), 575, 573);

        addObject(new FivePounds(), 504, 457);

        addObject(new TwoPounds(), 555, 493);

        addObject(new OnePound(), 590, 494);

        addObject(new FiftyPence(), 555, 453);

        addObject(new TwenyPence(), 589, 453);

        addObject(new TenPence(), 555, 416);

        addObject(new FivePence(), 589, 416);
    }

    /**
     * "Chocolates" Objects and add them to the world, to prepare the world for the start of the program.
     * 
     */
    private void prepareChocolates()
    {
        addObject(new Junbao(), 359, 131);

        addObject(new Junbao(), 113, 131);

        addObject(new Hobby(), 345, 248);

        addObject(new Hobby(), 127, 248);

        addObject(new MiniTablet(), 359, 332);

        addObject(new MiniTablet(), 112, 332);

        addObject(new HappyDivorce(), 355, 441);

        addObject(new HappyDivorce(), 116, 441);
    }

    /**
     * "Prices" Objects and add them to the world, to prepare the world for the start of the program.
     * 
     */
    private void preparePrices()
    {
        addObject(new No67(), 227, 126);

        addObject(new No78(), 227, 198);

        No84 no84 = new No84();
        addObject(no84, 233, 313);

        No94 no94 = new No94();
        addObject(no94, 232, 412); // just to show another way to add object to the world
    }

    /**
     * "Counter" Object and add it to the world, to prepare the world for the start of the program.
     * 
     */
    private void counter()
    {
        addObject(new Counter(), 593, 252);
    }
}
danpost danpost

2012/9/5

#
My revised code
public class FivePence extends Money
{
    private int n = 5;
    private Long markTime = 0;
    GreenfootImage image = null;

    public FivePence()
    {
        image = new GreenfootImage(getImage());
    }

    public void act() 
    {
        if (markTime == 0 && Greenfoot.mouseClicked(this))
        {
            setLocation(517, 314);
            getImage().drawString("Credit: " + n, 592, 170);
            markTime = System.currentTimeMillis();
        }
        if (markTime != 0 && System.currentTimeMillis() - markTime > 1000)
        {
            markTime = 0;
            setLocation(589, 416);
            setImage(new GreenfootImage(image));
        }
    }
}
Now the text is removed when the coin comes back. I do not care much for one-liner methods that are called only once.
There are more replies on the next page.
1
2
3