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

2016/4/11

Labels

zobear888 zobear888

2016/4/11

#
I am having trouble with the label class, I have a label and my "bullet" is supposed to make +1 label appear when ever it hits an enemy but the bullet does not appear anymore. Here is my code for the "bullet": public void seesJam()//Is it touching the jamBread? { if ( canSee(jamBread.class) )//If so, eat the jamBread, place a +1 label, and leave. { eat(jamBread.class); getWorld().addObject (new Label("+1"), getX(), getY()); getWorld().removeObject(this); } } (Note: JamBread is the enemy class) and here is the label code: public Label(String text) { GreenfootImage img = new GreenfootImage (text.length()*20, 30); img.drawString (text, 4, 40); setImage (img); }
danpost danpost

2016/4/11

#
Nothing seems amiss with the code given -- and actually the code given is for when the bullet is in the world, not for putting a bullet in the world. Show the code which detects the proper conditions for a bullet to spawn and then spawning a bullet (and any code related to it).
zobear888 zobear888

2016/4/13

#
Sorry, I misspoke. The bullet appears and works fine, but the label does not.
danpost danpost

2016/4/13

#
zobear888 wrote...
Sorry, I misspoke. The bullet appears and works fine, but the label does not.
You are probably drawing the text too low (below the image instead of on it). Try changing the 'drawString' line to something more like this:
img.drawString(text, 4, 25);
zobear888 zobear888

2016/4/13

#
Thank you! I added the code and realized it was off of the screen.
You need to login to post a reply.