This site requires JavaScript, please enable it in your browser!
Greenfoot back
rachpaguia@gmail.com
rachpaguia@gmail.com wrote ...

2017/3/14

APPEARING OBJECT

1
2
I'm sorry i've been asking too many questions. Um can someone tell me how to make actors appear randomly? and in different places? thank you
danpost danpost

2017/3/14

#
rachpaguia@gmail.com wrote...
I'm sorry i've been asking too many questions. Um can someone tell me how to make actors appear randomly? and in different places? thank you
What state(s) do you wish to randomize? when to appear? which actor should appear? (where was covered at the end of your post). Without knowing exactly what you want to do, I could end up giving you more than you require or the wrong thing altogether. As far as where, do you have a set of specific places to be chosen from or anywhere? Also, are there limits as to how many of these object(s) can be in the world?
Nosson1459 Nosson1459

2017/3/14

#
With the information that you gave me you can do this:
1
2
3
4
5
6
7
8
9
10
11
// place outside all methods
private int timer = Greenfoot.getRandomNumber(100) + 100; // the amount of time before the actor appears
 
// in act
timer--;
if (timer == 0)
{
    addObject(new ClassName(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
    // I didn't see you say that a random amount should appear so there will just be one
    timer = Greenfoot.getRandomNumber(100) + 100;
}
danpost wrote...
rachpaguia@gmail.com wrote...
I'm sorry i've been asking too many questions. Um can someone tell me how to make actors appear randomly? and in different places? thank you
What state(s) do you wish to randomize? when to appear? which actor should appear? (where was covered at the end of your post). Without knowing exactly what you want to do, I could end up giving you more than you require or the wrong thing altogether. As far as where, do you have a set of specific places to be chosen from or anywhere? Also, are there limits as to how many of these object(s) can be in the world?
Oh, I meant where and when! Sorry for being vague
@Nosson, does this certain code make actors appear at different times?
danpost danpost

2017/3/14

#
With the code Nosson1459 supplied, a new object will spawn somewhere in the world every 2 to 4 seconds (about).
Nosson1459 Nosson1459

2017/3/14

#
It will make a certain type of actor with the name ClassName spawn every random amount of time in a random place on the frame.
Oh, that's great! I'll try it out, thank you!
I tried the code but it said that private int is considered an error, as it is an illegal start of an expression. What must I put to correct it?
danpost danpost

2017/3/14

#
rachpaguia@gmail.com wrote...
I tried the code but it said that private int is considered an error, as it is an illegal start of an expression. What must I put to correct it?
Check your bracketing -- make sure everything is in its proper place. If you cannot fix it, post the entire class code here for help.
i dont think i'm missing anything,, here is my entire code { //This is too make our image (balloons) smaller GreenfootImage image = getImage(); image.scale(330,150); setImage(image); //This is too make the object fall setLocation (getX(), getY()+7); Greenfoot.setSpeed(+20); //This is to make the object disappear when a the said key is pressed String key = "enter"; if (Greenfoot.isKeyDown("A")) { getWorld().removeObject(this); } public int timer = Greenfoot.getRandomNumber(100) + 100; timer--; if (timer = 0) { addObject(new C.java(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight())); timer = Greenfoot.getRandomNumber(100) + 100; } } }
Super_Hippo Super_Hippo

2017/3/14

#
The code for adding objects should be in your world's act method. The 'private int ...' line should be outside methods.
danpost danpost

2017/3/14

#
rachpaguia@gmail.com wrote...
i dont think i'm missing anything,, here is my entire code
No. That is only one block of code. You need to show the entire class (from the line before the first '{' curly bracket to the last '}' curly bracket. Open the class editor and press Ctrl-Shift-I to attempt to have the class properly indented. Then press Ctrl-A and Ctrl-C to copy all to clipboard. Then click the 'code' link below the reply box on this page and press Ctrl-V to paste the code into the text frame of the dialog box and click the button with the checkmark and then complete the posting.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
{
    /**
     * Act - do whatever the A wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
       //This is too make our image (balloons) smaller
       GreenfootImage image = getImage();
       image.scale(330,150);
       setImage(image);
        
       //This is too make the object fall
       setLocation (getX(), getY()+7);
       Greenfoot.setSpeed(+20);
        
       //This is to make the object disappear when a the said key is pressed
      String key = "enter";
      if (Greenfoot.isKeyDown("A"))
     {
      getWorld().removeObject(this);
     }   
     
     private int timer = Greenfoot.getRandomNumber(100) + 100;
      timer--;
     if (timer = 0)
    {
     addObject(new C.java(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
     timer = Greenfoot.getRandomNumber(100) + 100;
    }
  }
}
like this?
oh yea nice one
There are more replies on the next page.
1
2