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

3 days ago

Getting a Variable from a Different Subclass

Haylaylo11 Haylaylo11

3 days ago

#
Hey, so I'm trying to carry over the value of a variable from one subclass to another subclass. However, this doesn't work. Could somebody help? Here's the code.
Haylaylo11 Haylaylo11

3 days ago

#
This is the code where the value of the variable is first stored.
public void chooseLevel()
    {
        openText3 = "Choose a level: 1, 2, or 3";
        OpeningBackground menu = (OpeningBackground)getWorld();
        menu.showText(openText3, 512, 288);
        Greenfoot.delay(100);
        levelNumber = Greenfoot.ask("Which level?");
        level = Integer.parseInt(levelNumber);
        
    }
Haylaylo11 Haylaylo11

3 days ago

#
This is in another subclass of Actor that uses the level variable to choose what type of question is used depending on the level that the user chooses in the beginning.
public void act()
    {
        c = Greenfoot.getRandomNumber(100) + 1;
        int gameLevel = level.getLevel();
        if (gameLevel == 1)
        {
            for (count = 1; count < 5; count++)
            {
                randomQuestions1();
                Greenfoot.delay(100);
                answer();
                if (inputAnswer != realAnswer)
                {
                    count = 0;
                }
            }
            MyWorld world = (MyWorld)getWorld();
            world.showText ("", 780, 135);
        }
        else if (gameLevel == 2)
        {
            if (c < 50)
            {
                for (count = 1; count < 5; count++)
                {
                    randomQuestions2Multiplication();
                    Greenfoot.delay(300);
                    answer();
                    if (inputAnswer != realAnswer)
                    {
                        count = 0;
                    }
                }
                MyWorld world = (MyWorld)getWorld();
                world.showText ("", 780, 135);
            }
            else
            {
                for (count = 1; count < 5; count++)
                {
                    randomQuestions2Division();
                    Greenfoot.delay(300);
                    answer();
                    if (inputAnswer != realAnswer)
                    {
                        count = 0;
                    }
                }
                MyWorld world = (MyWorld)getWorld();
                world.showText ("", 780, 135);
            }
        }
        else if (gameLevel == 3)
        {
            for (count = 1; count < 5; count++)
            {
                randomQuestions3();
                Greenfoot.delay(100);
                answer();
                if (inputAnswer != realAnswer)
                {
                    count = 0;
                }
            }
            MyWorld world = (MyWorld)getWorld();
            world.showText ("", 780, 135);
        }
    } 
Haylaylo11 Haylaylo11

3 days ago

#
As of right now, in the second segment of code, level is just returned as 0 instead of what the user inputs.
danpost danpost

3 days ago

#
Haylaylo11 wrote...
As of right now, in the second segment of code, level is just returned as 0 instead of what the user inputs.
In liine 4, what does 'level.getLevel()' do?
You need to login to post a reply.