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

2020/6/7

how do i make multiples objects out of 1 actor subclass and how do i make them come from out of the screen?

lilshopa lilshopa

2020/6/7

#
so, these objects are the enemies in my game, they come from the right to the left and i want to put like 15 or 20 enemies of them but how do i make multiple of them and make them spawn randomlly (from the middle to down part of the screen). I also would like to know how i make them come out from out of the screen. Thank you previously.
danpost danpost

2020/6/8

#
lilshopa wrote...
so, these objects are the enemies in my game, they come from the right to the left and i want to put like 15 or 20 enemies of them but how do i make multiple of them and make them spawn randomlly (from the middle to down part of the screen). I also would like to know how i make them come out from out of the screen. Thank you previously.
Add an act method to your World subclass. Use 'Greenfoot.getRandomNumber(int num)' for random spawn chance and for where to spawn below middle of screen.
lilshopa lilshopa

2020/6/10

#
what do you mean? like, im doing some tests and this is how im putting it(just for test) addObject(new inimigo(),712,437) inimigo means enemy so instead of the x and y i have to put the getrandomnumber?
danpost danpost

2020/6/10

#
lilshopa wrote...
what do you mean? like, im doing some tests and this is how im putting it(just for test) addObject(new inimigo(),712,437) inimigo means enemy so instead of the x and y i have to put the getrandomnumber?
Yes. That would take care of where. Then, for when, you need to place a condition on the spawning.
lilshopa lilshopa

2020/6/10

#
how do i create this act method?
danpost danpost

2020/6/10

#
lilshopa wrote...
how do i create this act method?
what have you tried?
lilshopa lilshopa

2020/6/10

#
so its something like this now(testing) addObject(new inimigo(),900, Greenfoot.getRandomNumber(800)); the x its okay the enemy i want to come from the same part that is off the screen but how do i put it to only appears from the middle to down
danpost danpost

2020/6/10

#
lilshopa wrote...
so its something like this now(testing) addObject(new inimigo(),900, Greenfoot.getRandomNumber(800)); the x its okay the enemy i want to come from the same part that is off the screen but how do i put it to only appears from the middle to down
Add a constant outside; subtract the same constant inside.
lilshopa lilshopa

2020/6/10

#
private int num = 600; addObject(new inimigo(),905, Greenfoot.getRandomNumber(389+num)); i did this so its random spawn on the 389(Y) to down but its still appearing up
lilshopa lilshopa

2020/6/10

#
ok i was being stupid, i put the num outside the parentheses and it worked, but i would like to know how i create multiples of them (like the main character is going to shoot the enemies so they disappear and more come from the same side but in the random y)
danpost danpost

2020/6/10

#
lilshopa wrote...
ok i was being stupid, i put the num outside the parentheses and it worked, but i would like to know how i create multiples of them (like the main character is going to shoot the enemies so they disappear and more come from the same side but in the random y)
Show your current code.
lilshopa lilshopa

2020/6/10

#
 private int num = 389;
    public Mundo1()
    {    
        super(905, 679, 1); 
        GreenfootImage cenario = new GreenfootImage("cenario_9.png");
        setBackground(cenario);
        addObject(new buzz(),104,438);
        //inimigo[] inimigos = new inimigo[11];
        //for(int i=0; i<inimigos.length; i++)
        //{
         //   inimigos[i] = new inimigo();
         //   int inimigoX = 905;
         //   int inimigoY = Greenfoot.getRandomNumber((200)+num);
         //   addObject(inimigos[i], inimigoX, inimigoY);
        //}
         addObject(new inimigo(),905, Greenfoot.getRandomNumber(200)+num);
these in // it was a video i was watching to put multiples enemies but when i used that it created all of them ontop of each other and i was kinda stucked
lilshopa lilshopa

2020/6/10

#
the enemy code
private int dir = 3;
    public void act() 
    {
     
        setLocation(getX()-dir, getY());
        
        Actor buzz;
        buzz = getOneIntersectingObject(buzz.class);
        
        if(buzz!= null)
        {
            World Mundo1;
            Mundo1 = getWorld();
            GameOver gameover = new GameOver();
            Mundo1.addObject(gameover, Mundo1.getWidth()/2, 180);
            GameOver2 gameover2 = new GameOver2();
            Mundo1.addObject(gameover2, Mundo1.getWidth()/2,250);
            Mundo1.removeObject(buzz);
        }  
danpost danpost

2020/6/10

#
You need to use an act method in your Mundo1 class to spawn the enemies.
You need to login to post a reply.