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

2012/11/10

Random Target

1
2
danpost danpost

2012/11/12

#
Did you try comparing getX() and getY() the the top and left coordinates of the world?
Wavesludge Wavesludge

2012/11/12

#
Yeah, same thing happens. Right and bottom works, left and top doesn't.
danpost danpost

2012/11/12

#
I had a feeling that this would be next! and, yes, of course there is a way. Create another message object (using the same Message class) from the world class constructor and add it to the world near the cannon. With two message objects in the world, we will need to keep references on them to tell them apart. Add two instance Message fields to the world class code:
public Message resultMessage, infoMessage; 
The 'resultMessage' object for "HIT" and "MISSED" messages and the 'infoMessage' object for power and angle values. In the constructor
resultMessage = new Message("");
addObject(resultMessage, 750, 550); // wherever
infoMessage = new Message("");
addObject(infoMessage, 150, 550); // wherever
infoMessage.updateInfo();
// add the new 'updateInfo' method to the world class
public void updateInfo()
{
    Cannon cannon = getObjects(Cannon.class).get(0);
    infoMessage.setText("Angle: " + (30 + cannon.angle) + "\nPower: " + cannon.power);
}
Then, in the Cannon class, when the angle or power is changed, call the newly added method above.
// in the Cannon class when power or angle is changed
((WorldName) getWorld().updateInfo();
Change 'WorldName' to the name of your world.
danpost danpost

2012/11/12

#
You will have to adjust the code for the 'resultMessage' object. Instead of using getWorld().getObjects(Message.class).get(0) you will have to use ((WorldName) getWorld).resultMessage.
danpost danpost

2012/11/12

#
For the top and left edges, you tried the following?
if (getX() == 0)
// and
if (getY() == 0)
Wavesludge Wavesludge

2012/11/12

#
I've added the code into the world, but I get an error with resultMessage. it highlight "new Message("")" the help says: constructor Message in class Message cannot be applied to given types; required: no Arguments found: java.lang-string reason: actual and formal argument lists differ in length. The operator that you use here cannot be used for the type of value that you are using it for. You are either using the wrong type here, or the wrong operator.
import greenfoot.*;  

public class bg extends World
{
    int minX = 300;
    int maxX = 750;
    int minY = 100;
    int maxY = 400;
    public Message resultMessage, infoMessage; 

    public bg()
    {    
        super(800, 500, 1); 
        addObject(new Canon(), 50, 460);

        resultMessage = new Message("");  
        addObject(resultMessage, 750, 550); // wherever  
        infoMessage = new Message("");  
        addObject(infoMessage, 150, 550); // wherever  
        infoMessage.updateInfo();  
        // add the new 'updateInfo' method to the world class  
    }
    
    public void act()
    {
        checkTarget();
    }
    
    public void updateInfo()  
    {  
        Cannon cannon = getObjects(Cannon.class).get(0);  
        infoMessage.setText("Angle: " + (30 + cannon.angle) + "\nPower: " + cannon.power);  
    } 
    
    private void checkTarget()  
    {  
        if (getObjects(Target.class).isEmpty())  
        {  
            int x = minX + Greenfoot.getRandomNumber(maxX - minX);  
            int y = minY + Greenfoot.getRandomNumber(maxY - minY);  
            addObject(new Target(), x, y);  
        }  
    }
}
Wavesludge Wavesludge

2012/11/12

#
danpost wrote...
For the top and left edges, you tried the following?
if (getX() == 0)
// and
if (getY() == 0)
Worked like a charm!
danpost danpost

2012/11/12

#
Sorry about the Message thing. I though I had given you code for a Message class, but apparently it was someone else. Copy/paste the code for a Message class in this discussion post for your Message class.
You need to login to post a reply.
1
2