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

2014/3/30

Help with lives counter

1
2
Rubric94 Rubric94

2014/3/30

#
ok, done, now mover cant find lives for fragment collision for the spirit class updated health code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Lives here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Health extends Actor
{
     public int lives=1;
    /**
     * Act - do whatever the Lives wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        
               
      if (lives==1)
      {
         setImage("Lives1.png");
      }
               
      if(lives==2)
      {
         setImage("Lives2.png");
      }
             
      if(lives==3)
      {
         setImage("Lives3.png");
      }
      
    }    
    
     public int getlives()
    {
        return lives;
    }//gets the score
}
danpost danpost

2014/3/30

#
Now, test it by creating one and running the method(s) on it manually. (right click on Health icon and then click in world to create one; right click on object and select a method from list to run a method). Add any other methods you think it might need (addALife, loseALife, etc).
Rubric94 Rubric94

2014/3/30

#
i tried putting these methods in but it says on the lest curly brace of each that it is missing a return statement
public int AddLife()
    {
        lives=lives+1;
    }
    
     public int LoseLife()
    {
        lives=lives-1;
    }
Rubric94 Rubric94

2014/3/30

#
changed from int to void, no syntax errors
Rubric94 Rubric94

2014/3/30

#
ok, that works now how would i refference those methods for add and lose in my player collision?
Rubric94 Rubric94

2014/3/30

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

/**
 * Write a description of class Lives here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Health extends Actor
{
     public int lives=1;
    /**
     * Act - do whatever the Lives wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       
               
      if (lives==1)
      {
         setImage("Lives1.png");
      }
               
      if(lives==2)
      {
         setImage("Lives2.png");
      }
             
      if(lives==3)
      {
         setImage("Lives3.png");
      }
      
    }   
    
    public void AddLife()
    {
        lives=lives+1;
    }
    
     public void LoseLife()
    {
        lives=lives-1;
    }
    
     public int getlives()
    {
        return lives;
    }
}
my mover class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Mover here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Mover extends Actor
{
   public int prevX;
   public int prevY;
    //stores the X and Y co-ordinates of the object

   private int level=1;
   public int storeCurrentPosition;
    //stores the current position of the object using prevX and prevY
  
    //stores the variable "score"
    
   
    /**
     * Act - do whatever the JackFrost wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        storeCurrentPosition();
        //calls the method storeCurrentPosition
    }
    
   
    
    public void storeCurrentPosition()
    {
     prevX = getX();
     prevY = getY();
    }//sets gets the X and Y co-ordinates and equates them to prevX/Y 
    
 
      
    
    public void checkCollision(Spirit spirit)
    {
      
      if(!getObjectsInRange(45, Wall1.class).isEmpty())
           setLocation(prevX , prevY); 
     
      if(!getObjectsInRange(45, Wall2.class).isEmpty())
           setLocation(prevX , prevY); 
           
      if(!getObjectsInRange(45, Wall3.class).isEmpty())
           setLocation(prevX , prevY); 
           
      if(!getObjectsInRange(45, Wall4.class).isEmpty())
           setLocation(prevX , prevY); 
      
      //checks if object player is colliding with object wall, if yes then sets location to prevX/PrevY
    
      Actor bonusCollision = getOneObjectAtOffset(0,0, Fragment.class);
      if(bonusCollision!=null)
        {

            
            Universe thisWorld = (Universe) getWorld();
//             lives=lives+1;
            removeTouching(Fragment.class);
            
            //calls the removeBonus method from Universe
        }//checks if object santa has collided with object present, if yes then adds 1 to variable score and removes the object
        
      if(!getObjectsInRange(45, Exit.class).isEmpty())  
      {
        level++;

           if(level==2)
           {
               Greenfoot.setWorld(new Level2(spirit));
           }
           
           if(level==3)
           {
               Greenfoot.setWorld(new Level3(spirit));
            }
           
//            if (level==4)
//            {
//              Greenfoot.setWorld(new Level4(spirit));
//            }
           
        
           //endif
      }//changes the world on collision with the exit dependant on the current level
 
    }//checks if objects are colliding
     
     public int getLevel()
    {
        return level;
    }//gets the number of the level
}
danpost danpost

2014/3/30

#
Looking good. Now, do you have a reference to the Health object in your world class? Show anything in your world class that relates to any Health objects; specify where each part is located in the code. Show methods if not to lengthy. Also any place in the class of your player (and include the name of the class).
Rubric94 Rubric94

2014/3/30

#
i have a universe class that all the levels are grouped under
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Universe here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Universe extends World
{
    protected Spirit player;
    
    public Fragment bonus;
    public Fragment bonus2;
    
   
    
    
    
    private int playerX;
    private int playerY;
    //stores X and Y co-ordinates of player
    private int exitX, exitY, bonusX, bonusY, bonus2X, bonus2Y;
    //stores X and Y co-ordinates of exit and bonus
    /**
     * Constructor for objects of class Universe.
     * 
     */
    public Universe()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 800, 1); 
        player = new Spirit();
      
        setPaintOrder(Health.class, Spirit.class, Enemy.class, Walls.class, Exit.class);
        populateWorld();

        
    }
    
    
     public Universe(Spirit player)
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 800, 1); 
        this.player = player;
        setPaintOrder(Spirit.class, Enemy.class, Walls.class, Exit.class);
        populateWorld();
    }
    
     /**
     * Set up the objects in the world
     */    
    private void populateWorld()
    {
        setValues();
        //calls the setValues method
        bonus = new Fragment();
        bonus2 = new Fragment();
        //bonus becomes equal to new present
        addObject(bonus, bonusX, bonusY);
        addObject(bonus2, bonus2X, bonus2Y);
        //adds the object bonus and sets its X and Y co-ordinates
        addObject(player,playerX, playerY);
        //adds the object player and sets its X and Y co-ordinates
        addObject(new Exit(),exitX, exitY);
        //adds the object exit and sets its X and Y co-ordinates
      
    }//end method populateWorld    
    

    
    /**
     * set up the values to match the level that is being played
     */    
    private void setValues()
    {
        switch (player.getLevel()){
            
            case 1: exitX=735;
                    exitY=90;
                    bonusX=50;
                    bonusY=50;
                    bonus2X=700;
                    bonus2Y=700;
                    playerX=50;
                    playerY=750;
                   
   
                    break;
            
            case 2: exitX=400;
                    exitY=40;
                    bonusX=550;
                    bonusY=550;
                    playerX=300;
                    playerY=570;
                    
                    break;
                    
            case 3: exitX=300;
                    exitY=50;
                    bonusX=240;
                    bonusY=270;
                    playerX=300;
                    playerY=570;
                    
                    break;
                }//endswitch
        //sets object locations based on which level the player is on
    }//end method setValues
    
    
    
   
    
    public void cleanup()
    {
        
        removeObjects(getObjects(Spirit.class));
        removeObjects(getObjects(Wall1.class));
        removeObjects(getObjects(Wall2.class));
        removeObjects(getObjects(Wall3.class));
        removeObjects(getObjects(Wall4.class));
        removeObjects(getObjects(Fragment.class));
        removeObjects(getObjects(Exit.class));
    }//this cleans up the santa and other objects from the screen
}
what and where should i add >.<
danpost danpost

2014/3/30

#
I do not see where any Health objects are dealt with here. The only reference to Health is for the class in the 'setPaintOrder' statement. ;{
Rubric94 Rubric94

2014/3/31

#
i didnt add any, i was wondering where i should add them, i tried to adapt the example one from the space game example earlier but it didnt work
danpost danpost

2014/3/31

#
What all will you be tracking the health of? does the class only have one object created from it or multiple objects for the health of multiple things? If multiple Health objects are to be created: Usually either the actor that requires an object to track some state of the actor (like a health bar, score counter, lives tracker, whatever), the actor will create it when it is being created and add it to the world when it is added to the world or the world will create the object first, then create the actor, passing the object to it. That actor should keep a reference to that object (not the world).
You need to login to post a reply.
1
2