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
danpost danpost

2012/9/5

#
ManiHallam wrote...
@Danpost, The object has been added to the world as preparation for the world.
It just appeared to be trying to add 'this' into the world using 'getWorld()'. Being the method was 'public', I could not tell whether it was being called from within its class or from somewhere else, and I did not see it being called from within. At any rate 'getWorld().addObject(this, x, y);' should never (have to) be used. As far as the 'n' value, being the class name was 'FivePence', it seemed logical that 'n' would always be 5; and it can be nothing but 5 as I see that class code at this point. If you have other classes for the other coins, just change the value of 'n' in those classes. Hope this makes sense.
ManiHallam ManiHallam

2012/9/5

#
@danpost, which package of java should be imported?
danpost danpost

2012/9/5

#
For the code I provided, just 'import greenfoot.*;'; no others.
ManiHallam ManiHallam

2012/9/5

#
it does not compile The error says, "incompatible type - found int but expected java.lang.Long"
ManiHallam ManiHallam

2012/9/5

#
this error is for line 4
private Long markTime = 0;
danpost danpost

2012/9/5

#
Sorry, 'Long' should not be capitalized; use 'long'.
ManiHallam ManiHallam

2012/9/5

#
Oh, lol it should be
long
But it was Long which I believe that was just typing mistake. Thanks @danpost
ManiHallam ManiHallam

2012/9/5

#
wow! Thanks @danost. the problem about the 1 second pause was solved. Thanks ever so much. just, the text for Credit does not appear
getImage().drawString("Credit: " + n, 592, 170);  
do you have any idea? Thanks again.
ManiHallam ManiHallam

2012/9/5

#
@danpost just to let you know that, I have a class it is called Counter, which does not have any picture. this is the source code for Class Counter, which is not completed yet, it needs some mathematical calculation for defining the value for n, before and after user buy the chocolate:
public class Counter extends Actor
{
    private int credit;
    private int n;
    
    public Counter()
    {
        credit = 0;
        setImage(new GreenfootImage(135, 85));
        update();
    }
    
    public void addCredit()
    {
        credit+= n;
        update();
    }
    
    public void update()
    {
        GreenfootImage img = getImage();
        img.clear();
        img.setColor(Color.BLUE);
        img.drawString("Credit: " + credit, 593, 252);
    }
    
}
danpost danpost

2012/9/5

#
I mentioned in an earlier post that the values 592 and 170 were way too large. The text is drawn on the image (not the world background). The value (592 and 170) are offsets from the top-left corner of the image itself to the bottom-left of the text. Try 0 and 20 to start, and adjust from there. After a couple of tries, you will understand what happens.
ManiHallam ManiHallam

2012/9/5

#
(593, 252) is the location for the Screen, which messages will be shown, not the width and length.
danpost danpost

2012/9/5

#
In your Counter class, remove line 4, as the value of 'n' to be added to 'credit' must come from the coin. Again, there are many way to go about it, but I will try to keep it as simple as possible. We know exactly where the coin will be after it is clicked on, so 'getWorld().getObjectsAt(x, y, Money.class)' will give a list of any object (Money.class or any sub-class of it) at that location. You will need a boolean to track if something was found there or not (so we do not keep finding it there and acting upon it being there multiple times). Use something like:
// instance field
private boolean moneyFound  = false;
// code in act
if (!moneyFound && !getWorld().getObjectsAt(517, 314).isEmpty())
{
    moneyFound = true;
    Money money = getWorld().getObjectsAt(517, 314).get(0);
    credit += money.getValue();
}
if (moneyFound && getWorld().getObjectsAt(517, 314).isEmpty())
{
    moneyFound = false;
}
You will have to add the following method in the Money class:
public int getValue()
{
    return n;
}
danpost danpost

2012/9/5

#
ManiHallam wrote...
(593, 252) is the location for the Screen, which messages will be shown, not the width and length.
Then maybe you should create another actor class for 'Message' and add it to the world at that location:
import greenfoot.*;
import java.awt.Color;

public class Message extends Actor
{
    public Message(String text)
    {
        updateImage(text);
    }

    private void updateImage(String message)
    {
        setImage(new GreenfootImage(message, 20, Color.black, new Color(0, 0, 0, 0)));
    }

    public void setText(String text)
    {
        updateImage(text);
    }
}
In the world class, add one new instance of this class at world creation using an empty string (" ").
addObject(new Message(""), 593, 252);
Other objects in the world can then do the following to change the text:
Message msg = (Message) getWorld().getObjects(Message.class).get(0);
msg.setText("New Text");
To clear the text, use 'msg.setText("");'.
ManiHallam ManiHallam

2012/9/5

#
I have actually made some change for Class FivePence regarding showing message on the Screen, but still nothing ! here is its Code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Chocolate Dispenser Machine
 */
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);  
            credit(); 
            markTime = System.currentTimeMillis();  
        }  
        
        if (markTime != 0 && System.currentTimeMillis() - markTime > 1000)  
        {  
            markTime = 0;  
            setLocation(589, 416);  
            setImage(new GreenfootImage(image));  
        }  
    }  
    
    private void credit()
    {
        GreenfootImage img = getImage();
        img.setColor(Color.BLUE);
        img.drawString("Credit: " + n, 593, 252);
    }
}
Also I send the picture of the Dispenser for you to have a full picture of what I am trying to make. :-) Thanks again for your time and kindness. :-)
ManiHallam ManiHallam

2012/9/5

#
Yes, as you see on the picture, I do have a sub class as ScreenMessages
There are more replies on the next page.
1
2
3