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

2015/12/12

Help on making a message appear when something is hit, but then disappears afterwards

16cruzs 16cruzs

2015/12/12

#
So im trying to make a breakout game for my project, and im trying to make it so when the ball hits a brick, it says "+10!" and i want it to disappear acouple of seconds afterwards. This is what i have so far
public void hit()
    {
       
            Ten = new Message("+10!",Color.BLACK,30,Color.WHITE);
            getWorld().addObject(Ten, getX()+80, getY());
            
            System.out.println(numActs);
    }
    
    public void removeTitle()
    {
            if(numActs==70)
            {
                getWorld().removeObject(Ten);
                
                numActs=0;
            }
            
            System.out.println(numActs);
    }
the issue im having is, because numActs is always running, the "+10!" message never stays on the screen the same amount of time. or if i hit multiple blocks at once, all the blocks after the last one stay on the world. if that makes sense to anyone
danpost danpost

2015/12/12

#
You could subclass the Message class to have each one remove itself after 70 act cycles (each one will have its own timer):
public class TempMessage extends Message
{
    int timer;

    public act()
    {
        timer++;
        if (timer == 70) getWorld().removeObject(this);
    }
}
Then, you will create a TempMessage object instead of a basic Message object. You will have no need for the 'Ten' reference field, as well.
16cruzs 16cruzs

2015/12/13

#
Wow you respond really fast, thank you. im going to try this tomorrow at school to see how it works. i understand what youre saying for the most part, i'll let you know if i have any issues! Thanks Again
16cruzs 16cruzs

2015/12/13

#
Actually im trying it right now, and im getting an error "no suitable constructor found for message". I think its because right now this class isnt accepting the things i need. Like if i type a new constructor "public message()" which nothing in the parenthesis it works but its calling that constructor which is empty so it wont work. i think. (This is me trying to learn haha) but heres the code for the constructor i need it to accept
public Message (String myMessage, Color tColor, int fSize, Color bColor)
    {
        Font f = new Font("Need For Font",Font.ITALIC|Font.BOLD,fSize);
        message = myMessage;
        fontSize = fSize; 
        textColor = tColor;
        backgroundColor = bColor;
        GreenfootImage image = new GreenfootImage(375, 80);
         
         
         image.setColor(tColor);
         image.setFont(f);
         image.drawString(message, 20, 60);
         setImage(image);
        
         
        
    }
danpost danpost

2015/12/13

#
Add to the TempMessage class:
import java.awt.Color;
// and
public TempMessage(String myMessage, Color tColor, int fSize, Color bColor)
{
    super(myMessage, tColor, fSize, bColor);
}
You need to login to post a reply.