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

2018/10/20

I need help creating new objects based on the score

Alexlife2002003 Alexlife2002003

2018/10/20

#
So, I have a game and when you shoot an object it gets eliminated and adds 1 to the Counter but how do I add more Objects when said Counter reaches let's say 12.
danpost danpost

2018/10/20

#
What have you tried? Show codes for how objects and counter are added to the world to begin with. Show Counter class codes. Show codes for when score is increased. Show attempted code for adding additional objects. Give classes for where each set of codes is located (or just show entire classes -- which is much more helpful). It may be necessary to supply the class codes for the objects in question, as well (that which you will be adding more of).
Alexlife2002003 Alexlife2002003

2018/10/21

#
import greenfoot.*;
  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Space here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Space extends World
{

    /**
     * Constructor for objects of class Space.
     * 
     */
    
    private int timer=3600;
    Texto timerTexto;
    public static int i = 0;
    public static int counter = 0;
    
    private Counter theCounter;
    public static Spaceship x = new Spaceship(20.0, new Vector(0, 2.0));
    private GreenfootSound bkgMusic;
   
    /**
     * Creates your Spaceship and 10 enemy ships (5 at each edge of the screen). A counter is placed in the bottom left-hand corner to show your score.
     * The ship counter in the ship class is set to zero so that ships will be added when shot even after the scenario is reset (otherwise you would have
     * to restart th e game from its file location to get more ships to appear on screen after they are shot).
     */
    public Space()
    {
        
        
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(900, 580, 1);
        timerTexto=new Texto("Tiempo:"+timer/60);
        addObject(timerTexto,115,10);
         theCounter = new Counter();
         addObject(theCounter, 5, 5);
        i = 0;
       
        
        x.lives = 3;
        x.setRotation(0);
        addObject(x, 50, getHeight()/2);
        int pos [][] = {{486,30},{557,70},{490,135},{554,161},{473,227},{575,255},{501,308},{566,359},{500,400},{566,450},{500,500},{566,550}};
        for(int i=0;i<12;i++)
        {
            addObject(new Ship(),pos[i][0],pos[i][1]);
        } 
        
        
        
        setActOrder(Fireball.class, Ship.class);
        
        Ship.counter = 0;
           bkgMusic=new GreenfootSound("sounds/Last summer.mp3");
        bkgMusic.playLoop();
        
       
        
        
    }
    /**
     * Constantly checks the ship class for whether the score should be incremented or not. Also checks to see if the game is over.
     */
    public void act()
    {
        if(counter==12)
        {
            remakeShips();
       }  
        
     timer--;
     if(timer%60==0)
     timerTexto.setTexto("Tiempo: "+timer/60);
     if (timer==0)
     {
        timerTexto.setTexto("Se acabó el tiempo");
           bkgMusic.stop();        
        GameOver go=new GameOver();
        Greenfoot.setWorld(go);
}
        
    }

    /**
     * Adds a number of points to the score counter.
     */
    
   
    
    public static Spaceship returnShip()
    {
        return x;
    }
    public void remakeShips()
    {
        
        int pos [][] = {{486,30},{557,70},{490,135},{554,161},{473,227},{575,255},{501,308},{566,359}};
        for(int i=0;i<8;i++)
        {
            addObject(new Ship2(),pos[i][0],pos[i][1]);
        }        
    }
     public Counter getCounter()
    {
        return theCounter;
    }
    public int getValue()
    {
     return counter;
    }
}



    


Alexlife2002003 Alexlife2002003

2018/10/21

#
So in Space class in the method act as you can see I added an if so that from there it is supposed to call the remakeShips method and it compiles correctly it just does not do anything
Alexlife2002003 Alexlife2002003

2018/10/21

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


/**
 * Write a description of class Counter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Counter  extends Actor
{
    private int totalCount = 0;

    public Counter()
    {
        setImage(new GreenfootImage("0", 20, Color.WHITE, Color.BLACK));
    }

    /**
     * Increase the total amount displayed on the counter, by a given amount.
     */
    public void bumpCount(int amount)
    {
        totalCount += amount;
        setImage(new GreenfootImage("" + totalCount, 20, Color.WHITE, Color.BLACK));
    }
}
Alexlife2002003 Alexlife2002003

2018/10/21

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Ship here. * * @author (your name) * @version (a version number or a date) */ public class Ship extends SmoothMover { /** * Act - do whatever the Ship wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public static int counter = 0; //counter for the number of ships on screen public static boolean notEnoughShips = false; //used to denote whether the number of ships on screen should be incremented private Vector move = new Vector(); //speed used for all members of the ship class public static boolean upScore = false; //used to denote whether score should be incremented Spaceship userShip = Space.returnShip(); public void act() { checkForCollisions(); } public Ship() //the mass is not actually used { addForce(move); } /** * Relocate ships to either edge of the screen if hit by a fireball. If a multiple of 20 ships are hit by fireballs, another ships is added as long as less than 2000 ships have * already been added. Sets upScore to true so that the score can be incremented. */ private void checkForCollisions() { Actor Ship = getOneIntersectingObject(Fireball.class); if( Ship != null ) { hitUnaBurra(); getWorld().removeObject(this); } } private void hitUnaBurra() { Space spaceWorld = (Space) getWorld(); // get a reference to the world Counter counter = spaceWorld.getCounter(); // get a reference to the counter counter.bumpCount(1); } }
Alexlife2002003 Alexlife2002003

2018/10/21

#
method hitUnaBurra adds the points and Ship class is also the object I am trying to remake
danpost danpost

2018/10/21

#
Alexlife2002003 wrote...
when you shoot an object it gets eliminated and adds 1 to the Counter but how do I add more Objects when said Counter reaches let's say 12. << Codes Omitted >>
First, please note that you have no need at this time for any "static" content -- there is nothing that needs to be retained from one running of the scenario to the next. I noticed that the value of the counter field in the Space class is used in the condition to remake the ships; however, it is the totalCount field held by the Counter object called theCounter, which the getCounter method returns, that is being bumped when a ship is destroyed by a fireball. No wonder no ships are remade.
Alexlife2002003 Alexlife2002003

2018/10/21

#
oh Wow thanks, I did not notice that
You need to login to post a reply.