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

2012/11/10

ADDING A MESSAGE.

jabirfatah91 jabirfatah91

2012/11/10

#
Hi I am working with a cannon shooting game where a cannon ball should hit a target! Now my requirement is "A message shall tell the user if the target was hit or not". How can I add a message like this that can tell me if the target was hit or not? Note: I have three actor classes- Cannon, CannonBall and Target.
danpost danpost

2012/11/10

#
You will need a fourth actor class to display the message; call it 'Message' class. Write the constructor to accept a String text to display and create the image. You can, if you want, write a method that accepts a String text, so you can change the text displayed.
jabirfatah91 jabirfatah91

2012/11/11

#
Thanx a lot DANPOST! Well! Then how will my message know that a target is hit? Should i modify something in the target class?
danpost danpost

2012/11/11

#
Well! The message will not know, except you have it display what you want displayed at the time you need it displayed. That is, in your sub-class of World, add a field to hold your Message object:
Message messagebox = new Message("");
I used an empty string as the text to display because we do not wish to display anything yet. In fact, we have yet to add the object into the world. Since I do not know what name you gave to your sub-class of World, I will call it 'Subworld'. Just replace any occurances of this name with the actual name of your sub-class of World. OK! When, in your Target class (or Ball class ), you have determined that there was or was not a hit, use the following code:
String text = "You have hit the target"; // or not
Message msgbox = ((Subworld) getWorld()).messagebox; // getting the messagebox object
msgbox.setText(text); // calling the method that changes the text of the message
getWorld().addObject(msgbox, 100, 40); // display at coordinates of your choice
GreenGoo GreenGoo

2012/11/11

#
Wow... I wish I could figure things like that out...
jabirfatah91 jabirfatah91

2012/11/11

#
Hi DANPOST! Thanks anyway. I will try that out, but for now, you have to do a favor for me. I am having a big problem with my cannon shooting game. As far I know my codes are correct. But when I try to play that (and press “space” key in order to project the ball from cannon) the ball does some strange behavior. It does not move any projectile motion where it just goes up by drawing some lines itself and comes down automatically. I will upload my scenario with opening my source code and I will name my scenario “Cannon shooting unsolved”. Please look at my codes and give me some solution too. I will be very grateful to you.
jabirfatah91 jabirfatah91

2012/11/11

#
Danpost, Note that my game runs automatically without pressing "space" key. But if I press arrow keys then it behaves strange.
jabirfatah91 jabirfatah91

2012/11/12

#
danpost, I unfortunately can't figure that out ( about adding message), like things aren't clear to me. Can you please explain that more by looking at my current scenerio? /Thanks.
danpost danpost

2012/11/12

#
A real simple Message class would be something like this:
import greenfoot.*;
import java.awt.Color;

public class Message extends Actor
{
    public Message()
    {
        updateImage("");
    }
    
    public Message(String text)
    {
        updateImage(text);
    }
    
    public void setText(String text)
    {
        updateImage(text);
    }
    
    private void updateImage(String text)
    {
        setImage(new GreenfootImage(text, 30, Color.black, Color.white));
    }
}
Add a Message object from your Battle class (world) constructor. If and when a cannonball hits (intersects) the target, change (setText) the message to "HIT" and remove the cannonball. If and when a cannonball is removed from the world because it has reached the world edge, set the text of the message to "MISSED". When a new cannonball is created, set the text of the message back to an empty string ("").
jabirfatah91 jabirfatah91

2012/11/12

#
Hi I wrote those codes, but seems that it doesn't work. I will show my codes down for (1). "Battle" and (2). "Message" class respectively: & I uploaded my new scenerio named "Lab 5-modified". (1). import greenfoot.*; import java.awt.Color; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Battle here. * * @author (your name) * @version (a version number or a date) */ public class Battle extends World { //private Target targets= new Target ; //Color color1 = new Color(0,0,250); //Color color2 = new Color(0,0,20); private int startTargets=1; private boolean spaceDown= false; Message messagebox = new Message ("HIT"); { if (!spaceDown && Greenfoot.isKeyDown("space")) { spaceDown=true; } if (spaceDown && !Greenfoot.isKeyDown("space")) { spaceDown=true; } } /** * Constructor for objects of class Battle. * */ public Battle() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(1000, 600, 1,false); addObject(new Cannon(), 91, 502); //addTargets(); addTargets(startTargets); } private void addTargets(int count) { for(int i=0; i<count; i++) { //Target target; int x= Greenfoot.getRandomNumber(getWidth()/1); int y= Greenfoot.getRandomNumber(getHeight()/1); addObject(new Target(),x,y); //if(i<2){ //target= new Target(-3-a,color1); //addObject(target,target.x,target.y); //targets= target; } //if(i>=2 && i<9){ //target= new Target(-2-a,color2); //addObject(target,target.x,target.y); //targets= target; } } //public void act() //for(int i= 0; i<10; i++){ //if (targets !=null){ //targets.move(); (2). import greenfoot.*; import java.awt.Color; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Message here. * * @author (your name) * @version (a version number or a date) */ public class Message extends Actor { public Message() { updateImage(" "); } public Message (String text) { updateImage(text); } public void setText(String text) { updateImage(text); } private void updateImage(String text) { setImage(new GreenfootImage(text, 30, Color.black, Color.white)); } } /** * Act - do whatever the Message 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.
Malmotri Malmotri

2012/11/12

#
You must take your space boolean code from the battle class and implement it in the Cannon class. Like this
  private boolean spaceDown= false;
  public void makeCannonBall()
    {
        if (!spaceDown && Greenfoot.isKeyDown("space"))
        {
            spaceDown=true;
            World Background= getWorld();
            CannonBall cannonBall= new CannonBall(angle,power);
            Background.addObject(cannonBall, getX(), getY());
        }
        if (spaceDown && !Greenfoot.isKeyDown("space"))
         {
             spaceDown=false;
         }
    
    }
Then you can remove this boolean that checks if there is any cannonballs in the world. You have no use for this code anymore.
public boolean noCannonCannonBalls()
    {
        World background= getWorld();
        java.util.List cannonBalls= background.getObjects(CannonBall.class);
        return cannonBalls.isEmpty();
    }
jabirfatah91 jabirfatah91

2012/11/12

#
Hey Malmo, my problem is all about adding a message that can tell me if the target is hit (every other things work fine). I tried to implement the codes given by danpost for adding a message, but that does not help me...
danpost danpost

2012/11/12

#
Why does it not help? In the Cannonball class act method, you should have code reflecting the following actions (at least): - move - if not targetHit and intersects target - show "HIT" message - option to remove cannonball or set flag targetHit (boolean field) - if at world edge - if not targetHit, show "MISSED" message - remove cannonball In your Cannon class, when you detect a "space" keypress and add a new cannonball into the world, show "" message.
You need to login to post a reply.