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

2012/9/7

Showing messages from Classes and updated messages from Counter

1
2
3
4
danpost danpost

2012/9/8

#
Thanks, davmac. I do not know what lead me to believe that, as I have always used 'getImage' for the actors. I sit corrected with fp.
ManiHallam ManiHallam

2012/9/8

#
Dear Davmac, Thanks ever so much for the explanation. It is very clear and easily understandable. :-)
ManiHallam ManiHallam

2012/9/8

#
Dear Danpost, Thanks to you too for the explanation and all the support and your kindness.
danpost danpost

2012/9/8

#
Dear ManiHallam, Sorry for the mis-information about 'getImage()'. I guess that is something I should have double-checked before posting. Again, I apologize.
ManiHallam ManiHallam

2012/9/8

#
Oh no. You do not need to apologise. Don't worry. However, I should apologise for bothering you. I truly thank you for your help.
ManiHallam ManiHallam

2012/9/10

#
Ddear Danpost, I still do have problem for displaying messages on the screen. Could you please help me with that. these are the code for Screen class, one of money subclass, and the code added to the world preparation. Screen Class
public class Screen extends Body
{
    public Screen()
    {  
        setText("");
    } 
    
    /**
     * 
     */
    public void setText(String text)
    {   
        GreenfootImage image = new GreenfootImage(136, 88);
        image.setColor(Color.white);
        image.fill();
        int fontSize = 20;
        GreenfootImage txtImg = new GreenfootImage(text, fontSize, Color.blue, Color.white);
        int across = (image.getWidth() - txtImg.getWidth()) / 2;
        int down = (image.getHeight() - txtImg.getHeight()) / 2;
        image.drawImage(txtImg, across, down);
        setImage(image);
    }
}
Money Class:
public class Money extends Actor
{
    private int credit;
    
    /**
     * Act - do whatever the Money wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public int getValue()
    {
        return credit;
    }
    }
FiftyPence Class, which is a subclass for Money Class public class FiftyPence extends Money { private int value = 50; private long markTime = 0; GreenfootImage image = null; /** * */ 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(555, 453); } } /** * */ private void credit() { ((Screen) getWorld().getObjects(Screen.class).get(0)).setText("Credit: " + value); } } and this is the code which you said to add to the world.(italic)
public ChocolateDispenser() 
    {                           
        super(722, 640, 1);
        
        prepareBody();
        prepareKeyboard();
        prepareMoney();
        prepareChocolates();
        preparePrices();
        [i][u]Screen screen = (Screen) getObjects(Screen.class).get(0);[/u][/i]
    }
Also just to let you know that, I changed the "n" to "value" Thanks. :-)
danpost danpost

2012/9/10

#
I do not see where I said to add line 10 to the world. I do not see a reason for it to be there. If I did say to put it there, I would like to see the context of the discussion (it was not this one). If you are referring to my last post on page 1 of the 'Counter class' discussion, it was only if you needed to change the text there (remember, you called it 'magic' code). There is no need to change the text in the constructor of the world.
danpost danpost

2012/9/10

#
As far as still not getting messages: are you running the scenario and clicking the coins and still not getting them?
ManiHallam ManiHallam

2012/9/10

#
Hi Danpost, Yes it was on the other discussion which I might misunderstood. Just to remind you what it was, and for correcting me please, I copied that here Create only one of this type object at world creation, and any class can then use: Screen screen = (Screen) getWorld().getObjects(Screen.class).get(0); screen.setText("New text"); If in the world class, drop 'getWorld().'.
ManiHallam ManiHallam

2012/9/10

#
yes exactly as you say, it was
ManiHallam ManiHallam

2012/9/10

#
now I get message Thanks. I made that mistake , putting that code on the world. Thanks. I am ganna to check the counter now. If I have any problem ,may I come back to you again?
ManiHallam ManiHallam

2012/9/10

#
So, now I get the Value of any money, which is clicked, on the Screen. I copy again just the code for FiftyPence Class and the Counter Class. Could you please help me with that? Thanks FiftyPence:
public class FiftyPence extends Money
{
    private int value = 50;
    private long markTime = 0;
    GreenfootImage image = null;

    /**
     * 
     */
    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(555, 453);
        }
    }

    /**
     * 
     */
    private void credit()
    {
        ((Screen) getWorld().getObjects(Screen.class).get(0)).setText("Credit: " + value);
    }
}
Counter Class:
public class Counter extends Actor
{
    private int value;
    private boolean moneyFound  = false;

    /**
     * Act - do whatever the Counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        coinFound();
        noteFound();
        getCredit();
        update();
    }

    /**
     * Counting all Coins in Money class, which are inserted into the Coin Insertion.
     */
    public void coinFound()
    {
        if (!moneyFound && !getWorld().getObjectsAt(517, 314, Money.class).isEmpty())
        {
            moneyFound = true; 
            Money money = (Money) getWorld().getObjectsAt(517, 314, Money.class).get(0);
            value += money.getValue();
        }

        if (moneyFound && getWorld().getObjectsAt(517, 314, Money.class).isEmpty())
        {
            moneyFound = false;
        }
    }

    /**
     * Counting all Notes in Money class, which are inserted into the Note Insertion.
     */
    public void noteFound()
    {
        if (!moneyFound && !getWorld().getObjectsAt(517, 314, Money.class).isEmpty())
        {
            moneyFound = true;
            Money money = (Money) getWorld().getObjectsAt(517, 314, Money.class).get(0);
            value += money.getValue();
        }

        if (moneyFound && getWorld().getObjectsAt(517, 314, Money.class).isEmpty())
        {
            moneyFound = false;
        }
    }

    /**
     * 
     */
    public int getCredit()
    {
        return value;
    }

    /**
     * 
     */
    public void update()
    {
        ((Screen) getWorld().getObjects(Screen.class).get(0)).setText("Credit: " + value);
    }
}
Thank you.
danpost danpost

2012/9/10

#
No problem. I was now wondering why you had the credit variable and 'getValue' method in the Money class. Am looking into when and why you put it there (should not be there). You still have a Credit class, do you not? I think you said the Credit class did not have an image, does that mean you do not add a Credit object into the world (you just create one)?
ManiHallam ManiHallam

2012/9/10

#
no I do not have any Credit Class, just Counter class and Money super class for Notes and Coins. The only thing I changed in code, I replaced value instead of credit in all classes, wherever it was.
ManiHallam ManiHallam

2012/9/10

#
oops sorry also if you remember any money was "int n" I replaced it with "int value"
There are more replies on the next page.
1
2
3
4