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

#
i have a class that displays 1 of 3 images depending on how many lives the player has between 1 and 3. the lives value and the getLives method (return lives) are both in mover. in the health class i have... public Health () { if (lives==1) { setImage("Lives1.png"); getImage().scale(150, 50); } if(lives==2) { setImage("Lives2.png"); getImage().scale(150, 50); } if(lives==3) { setImage("Lives3.png"); getImage().scale(150, 50); } } but whenever i pick up the item that adds lives, my players lives go up but the image dosnt change and i dont know how to reffer to the players lives count, please help.
danpost danpost

2014/3/30

#
Maybe you should put the bulk of the code you posted in an 'act' method so it will be constantly checked.
Rubric94 Rubric94

2014/3/30

#
will try and get back to you
Rubric94 Rubric94

2014/3/30

#
still not working
danpost danpost

2014/3/30

#
Re-post your current Health class code. Use the 'code' link below the 'Post a reply' box to insert your code into your post.
Rubric94 Rubric94

2014/3/30

#
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
33
34
35
36
37
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 Mover
{
     
    /**
     * 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");
      }
       
    }   
 
}
Rubric94 Rubric94

2014/3/30

#
And this is mover
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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
   public int lives=1;
    //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 int getlives()
    {
        return lives;
    }//gets the score
     
    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));
//            }
            
        lives=lives-1;
           //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

#
Why is the Health class a subclass of the Mover class? Is your player created from a subclass of Mover, also? Even if it was, the lives field in the player will not be the same field as the lives field in the Health object. If you cannot say that health is a type of mover, then Health should not subclass Mover.
Rubric94 Rubric94

2014/3/30

#
i know, forgot to change that, any other suggestions
Rubric94 Rubric94

2014/3/30

#
it was for testing because it would always flag that it couldnt find variable lives, so i tested it like that
danpost danpost

2014/3/30

#
No immediately. That would be a start.
Rubric94 Rubric94

2014/3/30

#
its been changed
danpost danpost

2014/3/30

#
What has it been changed to? show the new Health class code.
Rubric94 Rubric94

2014/3/30

#
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
33
34
35
36
37
38
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
{
     
    /**
     * 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");
      }
       
    }   
 
}
  
danpost danpost

2014/3/30

#
You need to move the lives field and the methods dealing with it into this class from the Mover class.
There are more replies on the next page.
1
2