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

2019/5/24

Random object only appears once

montiqs montiqs

2019/5/24

#
Hello Community, this is my first post. Im new to programming and I have the Task to create a game for my computer science class Im creating a jump and run and im trying to let a platform spawn randomly at the top of the screen everytime you jump on a platform of the same type. Ive wrote this:
 if (Greenfoot.isKeyDown("space")&& (onGround()== true))
        {

            this.getWorld().addObject(newGround, Greenfoot.getRandomNumber(180) + 48 , getImage().getHeight() + 500);
            vSpeed = jumpHeight;
            fall();

        }
The Problem is that only one platform is spawning and when I jump on the next platform, a new one refuses to appear. Please help me out. Thanks in advance!
danpost danpost

2019/5/24

#
The variable newGround refers to a single platform object. Unless you change what it refers to, it will continue to try to place that same platform into the world.
montiqs montiqs

2019/5/24

#
Ground newGround = new Ground();
Heres my declaration of the variable. Im sorry, but I saw that declaration on another website and I dont know how to change the number of grounds. It isnt possible to just but a number in the () of the newGround. I know that this stuff might be annoying, but could you please tell me how to declare the variable correctly?
danpost danpost

2019/5/24

#
montiqs wrote...
<< Code Omitted >> Heres my declaration of the variable. Im sorry, but I saw that declaration on another website and I dont know how to change the number of grounds.
You could add the following at line 3 above:
newGround = new Ground();
which will assign a new ground object to the variable. Better would be to just get rid of the variable and change newGround in line4 to new Ground().
montiqs montiqs

2019/5/25

#
Thank you very much!
You need to login to post a reply.