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

2018/5/3

String a class name

Miguel.Valdez Miguel.Valdez

2018/5/3

#
so currently I have a class with a method that prints something with the class name of the actor causing it to print. But how can I make it so that no matter where I insert this method it pulls the name of the actor that is using the method?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class TextPrinter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class TextPrinter  
{
    String className = getClass().getName();
    /**
     * Constructor for objects of class TextPrinter
     */
    
    public void endGame()
    {
         System.out.print("\f");
         System.out.println(className + ":");
         System.out.println("Thank you for playing my game Angry Flappy");
         System.out.println("Please press ENTER to try again"); 
         System.out.println();
         return;
    }
}
TextPrinter printer = new TextPrinter();
public class Birdy extends Actor
{
	public void birdDead()
    {
        GameMusic.stop();
        Greenfoot.playSound("MBGameOver.mp3");
        getWorld().addObject(new GameOver(), getWorld().getWidth()/2,getWorld().getHeight()/2);
        printer.endGame();
    }
}
right now this prints in the terminal: TextPrinter: Thank you for playing my game Angry Flappy Please press ENTER to try again
danpost danpost

2018/5/4

#
Change the method signature to:
public void endGame(Actor actor)
and classname on the second line within the method to actor.getClass().toString(). Then you can call it with:
printer.endGame(this);
Miguel.Valdez Miguel.Valdez

2018/5/4

#
danpost wrote...
Change the method signature to:
public void endGame(Actor actor)
and classname on the second line within the method to actor.getClass().toString(). Then you can call it with:
printer.endGame(this);
earlier I was trying the same thing but I was slightly off. is it normal for this to show (the numbers)?
Birdy@143053c:
Thank you for playing my game Angry Flappy
Please press ENTER to try again

danpost danpost

2018/5/4

#
Miguel.Valdez wrote...
is it normal for this to show (the numbers)?
Birdy@143053c:
Actually, that is the location in the memory of your computer where the object is stored. If you do not want them to show, use getName() instead of toString().
Miguel.Valdez Miguel.Valdez

2018/5/4

#
danpost wrote...
Miguel.Valdez wrote...
is it normal for this to show (the numbers)?
Birdy@143053c:
Actually, that is the location in the memory of your computer where the object is stored. If you do not want them to show, use getName() instead of toString().
use the getName() in the TextPrinter class or in the class where TextPrinter.endGame(Actor actor) is being called? the reason i ask is from prevuios having the "String className = getClass().getName()" and it would print the TextPrinter name
Miguel.Valdez Miguel.Valdez

2018/5/4

#
i am trying different things but keep getting java.lang.string cannot be converted to greenfoot.Actor.
danpost danpost

2018/5/4

#
Miguel.Valdez wrote...
i am trying different things but keep getting java.lang.string cannot be converted to greenfoot.Actor.
Why would you try to use the String representation of a class name to get an actor?
Miguel.Valdez Miguel.Valdez

2018/5/4

#
danpost wrote...
Miguel.Valdez wrote...
i am trying different things but keep getting java.lang.string cannot be converted to greenfoot.Actor.
Why would you try to use the String representation of a class name to get an actor?
how can implement the getClass().getName into the TextPrinter.endGame()? I'm as well reading in the Oracle docs, for getName();
Miguel.Valdez Miguel.Valdez

2018/5/4

#
I've been trying multiple ways now, and either TextPrinter name keeps getting called, or Greenfoot won't even run.
Miguel.Valdez Miguel.Valdez

2018/5/4

#
Never mind I finally figure it out... there might be another way(please share it there is), but this worked.
    public void endGame(Actor actor)
    {
         System.out.print   ("\f");
         System.out.println (actor.getClass().getName() + ":");
         System.out.println ("Thank you for playing my game Angry Flappy");
         System.out.println ("Please press ENTER to try again"); 
         System.out.println ();
         return;
    }
    public void birdDead()
    {
        printer.endGame(this);
    }
You need to login to post a reply.