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

2012/9/6

Counter Class error

1
2
3
4
MatheMagician MatheMagician

2012/9/7

#
I'm not sure if you still want me to tell you this since it has been a while, but here I go: The List<Money> creates a List object from the information given by getWorld().getObectsAt() and then the <Money> part casts it to Money actors instead of just objects, which was messing you up earlier. Danpost is right though, you can just say:
Money money = (Money) getWorld().getObjectsAt(517, 314, Money.class).get(0); 
ManiHallam ManiHallam

2012/9/7

#
Dear MatheMagician, Thanks ever so much for the explanation. just could you please explain that, what does ".get() do?"
ManiHallam ManiHallam

2012/9/7

#
oops sorry,
.get(0)
danpost danpost

2012/9/7

#
It has been an hour, and he has not responded yet, so ... sorry, MatheMagician. .get(0) : gets the first item from the list (counting starts at zero) that getObjectsAt(...) returns.
ManiHallam ManiHallam

2012/9/7

#
Dear Danpost, Thanks ever so much for your support. :-) Hope you are fine.
danpost danpost

2012/9/7

#
I am. Thanks! Ready for a restfull weekend! And you?
danpost danpost

2012/9/7

#
The documentation on get(int) is here.
ManiHallam ManiHallam

2012/9/7

#
Thanks again. I did use your PM Scenario, did you get my message? Unfortunately, I do not get message from clicking on Coins/Notes yet.
public class FiftyPence extends Money
{
    private int n = 50;  
    private long markTime = 0;
    GreenfootImage image = null;  
    
    public FiftyPence()  
    {
        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(555, 453);  
            setImage(new GreenfootImage(image));  
        }  
    }
and this is code for Money Class:
public class Money extends Actor
{
    private int n;
    
    /**
     * 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 n;
    }
    }
ManiHallam ManiHallam

2012/9/7

#
danpost wrote...
Use this for your screen class:
import greenfoot.*;
import java.awt.Color;

public class Screen extends Body
{
    public Screen()
    {
        setText(" ");
    }

    public void setText(String text);
    {
        GreenfootImage image = new GreenfootImage(185, 75); // whatever size the image is
        image.setColor(Color.white);
        image.fill();
        int fontSize = 36; // adjust to fit
        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);
    }
}
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().'.
Could you let me know please, what do you mean by "Create only one of this type object ...." and also where could they be used in except world class?!!! Actor?
danpost danpost

2012/9/7

#
I do not see a problem with the code you are showing here. However, I do not see the current code for the credit() method. Please show what you have there now (and remind me what class you have it in).
danpost danpost

2012/9/7

#
Yes. An actor can set the text with the code as is; but the world does not need to 'getWorld()' (as it is the world).
danpost danpost

2012/9/7

#
I must have checked my messages just before you posted yours. But I did notice that you had opened the scenario at some point before then, as your UserName and UserImage were displayed; but, there was not a message at that point. I have responded.
ManiHallam ManiHallam

2012/9/7

#
Thanks for explanation. you mean if I don't want to use it in the world, I should put it in Actor class or in every single of Actor's subclass? I think in Actor is enough, that is my opinion. am I right? if not Could you correct me please. this is the code for Credit method, sorry.
private void credit()
    {
        GreenfootImage img = getImage();
        img.setColor(Color.WHITE);
        img.drawString("Credit: " + n, 562, 531);
    }
ManiHallam ManiHallam

2012/9/8

#
danpost wrote...
I must have checked my messages just before you posted yours. But I did notice that you had opened the scenario at some point before then, as your UserName and UserImage were displayed; but, there was not a message at that point. I have responded.
Yes this morning I looked at it, and I thought that, it would report to the administrator. I am going to write a suggestion there, I think it would be useful to improve it.
danpost danpost

2012/9/8

#
You should put it wherever, when you need to change the text that is displayed. Now, in the credit() method (which should probably be in the Money class, so you do not have to copy/paste it in all the sub-classes of it) your drawString statement has the same problem as the other drawString statement we worked on. See if you can fix this one, now.
There are more replies on the next page.
1
2
3
4