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

2019/11/27

Showing Text in a Game

Haylaylo11 Haylaylo11

2019/11/27

#
So I've been working on this game and right now, I'm trying to get some text to show up that prints a math problem. However, it doesn't seem to work. COuld anybody help?
private void randomQuestions()
    {
        a = Greenfoot.getRandomNumber(20) + 1;
        b = Greenfoot.getRandomNumber(20) + 1;
        c = Greenfoot.getRandomNumber(100) + 1;
        
        if (c<50)
        {
            text = "What is " + a + "+" + b;
            MyWorld world = (MyWorld)getWorld();
            world.showText(text, 100, 400);
        }
        else if (c>50)
        {
            if (a>b)
            {
                text = "What is " + a + "-" + b;
                MyWorld world = (MyWorld)getWorld();
                world.showText(text, 100, 400);
            }
            else
            {
                text = "What is " + b + "-" + a;
                MyWorld world = (MyWorld)getWorld();
                world.showText(text, 100, 400);
            }
        }
    }
Haylaylo11 Haylaylo11

2019/11/27

#
By the way, this is part of an actor subclass that is to be called by another actor subclass in the program.
danpost danpost

2019/11/27

#
Haylaylo11 wrote...
By the way, this is part of an actor subclass that is to be called by another actor subclass in the program.
Show how actor calls this code. Btw, change line 7 to:
if (c <= 50)
to ensure a question every time the method is called. This is NOT a fix to your issue however.
Haylaylo11 Haylaylo11

2019/11/28

#
I have this
    public void act() 
    {
            randomQuestions();
    }
Haylaylo11 Haylaylo11

2019/11/28

#
Which is called by this
private void kirbyIntersect()
    {
        if (isTouching(Bowser.class) )
        {
           TextBox textBox = new TextBox ();
           MyWorld world = (MyWorld)getWorld();
           world.addObject(textBox, 780, 150);
        }
    }
Haylaylo11 Haylaylo11

2019/11/28

#
The first two pieces of code are from a TextBox subclass under Actor that is being called by another Actor subclass called Kirby in the third piece of code
Haylaylo11 Haylaylo11

2019/11/28

#
Nevermind, I just figured it out. I feel really dumb, but thanks for your help!
You need to login to post a reply.