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

2016/5/30

Changing the image of the Actor when losing health

1
2
3
danpost danpost

2016/5/31

#
It can still be made to work by adding a String field for the current direction:
1
private String direction = "right";
and using something like this:
1
setImage("pirate"+direction+iHurt+".png");
However, with the current Pirate class code above, you could remove line 27 and just change lines 41 and 46 to use 'iHurt' in place of the number in the image filename.
KrystalLo KrystalLo

2016/5/31

#
so for example, piraterightiHurt1 ? something like that?
danpost danpost

2016/5/31

#
KrystalLo wrote...
so for example, piraterightiHurt1 ? something like that?
No. Something like this (showing the change in line 46):
1
this.setImage("pirateleft"+iHurt+".png");
KrystalLo KrystalLo

2016/5/31

#
Okay, i've done that. I've tried running the game, and its not working again. java.lang.NullPointerException at Pirate.act(Pirate.java:28) at greenfoot.core.Simulation.actActor(Simulation.java:594) at greenfoot.core.Simulation.runOneLoop(Simulation.java:552) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) and apparently,
1
int iCounter = oCounter.getValue();
is the problem. Sorry, I'm not too sure on how to read these properly.
danpost danpost

2016/5/31

#
KrystalLo wrote...
< Exception Trace Omitted > Sorry, I'm not too sure on how to read these properly.
The first line that contains the name of a class that you created, namely 'Pirate' in this case, shows where the error occurred. Here, it is saying that line 28 in the Pirate class, which is in the 'act' method, is where it happened, which does appear to be the line of code you posted above. Now, in that line there is only one item that could possibly be 'null' and that is 'oCounter', which is supposed to contain a reference to a Counter object. Apparently, it does not reference one or you would not have gotten that error. This leads us to where it is supposed to be set -- the constructor, which is supposed to get a Counter object passed by your World subclass; this is where the root of the problem resides. Post your World subclass code -- at minimum the fields, the constructor and the prepare method (to start).
KrystalLo KrystalLo

2016/5/31

#
Here is my World class code.
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class MyWorld here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
    //background was taken from this website
    //background was edited to make the height higher
    Pirate Nog;
    Counter myCounter;
    /**
     * Constructor for objects of class MyWorld.
     *
     */
    public MyWorld()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(659, 483, 1);
        prepare();
         
       // randomCoins();
    }
 
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Pirate Nog = new Pirate(myCounter);
        addObject(Nog,306,411);
        Nog.setLocation(286,411);
        Nog.setLocation(268,414);
        Counter counter = new Counter();
        addObject(counter,599,32);
        counter.setLocation(602,25);
        counter.setLocation(590,28);
    }
    int iCounter = 0;
    
    public void act(){
    iCounter++;
    if (iCounter==40){
         iCounter =0;
         //shorthand code http://www.greenfoot.org/topics/56657/0#bottom
            int rand = Greenfoot.getRandomNumber(4);
            int x = Greenfoot.getRandomNumber(getWidth());
            addObject(rand == 0? new Bomb() : new Coin(rand), x, 0);
             
           
           //Coin myCoin = new Coin();
           //addObject(myCoin,100,0);
           // addObject(new Coin(),Greenfoot.getRandomNumber(getWidth()),0);
            //http://www.greenfoot.org/topics/4602/0
          
        }
        }
    }
KrystalLo KrystalLo

2016/5/31

#
Oh with some help, the problem was found. I needed to put the newCounter above the new pirate first.
KrystalLo KrystalLo

2016/6/1

#
My pirate image is not changing when he touches the bomb, and it does not stop after the pirate touches the bomb 4 times. My teacher has told me that the image set code is being updated first before the greenfoot.stop code... something on the lines of that. Here's the code, please help me.
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Pirate here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Pirate extends Actor
{
    int moveBy = 4;
    int iHurt =1;
    Counter oCounter;
    boolean boolExplode =false;
    private String direction = "right";
    public Pirate(Counter myCounter){
        oCounter = myCounter;
    }
    /**
     * Act - do whatever the Pirate wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        // Add your action code here.
        //sounds & music gotten from http://freesound.org/
        checkKeys();
         
        setImage("pirate"+direction+iHurt+".png"); //put it in isTouching Bomb.class code
        int iCounter = oCounter.getValue();
        if((iCounter %10)==0) {iHurt = 1;}
        if(isTouching(Bomb.class) && (boolExplode==false)){
            boolExplode=true;
            iHurt++;
            Greenfoot.playSound("explode.wav");
            if(iHurt>4){
            Greenfoot.stop();
            }
        }
        if(!(isTouching(Bomb.class))){
            boolExplode = false;
        }
        if(isTouching(Coin.class)){
            //gold coins will give 15 points, silver 10 points and bronze 5 points
            iCounter++;
            Greenfoot.playSound("coins.wav");
        }
    }  
    private void checkKeys(){
        if(Greenfoot.isKeyDown("Right")){
            direction = "right";
            this.setLocation(getX()+moveBy,getY());
         
        }
        if (Greenfoot.isKeyDown("Left")){
            direction = "left";
            this.setLocation(getX()-moveBy,getY());
         
        }
    }
}
danpost danpost

2016/6/1

#
KrystalLo wrote...
Oh with some help, the problem was found. I needed to put the newCounter above the new pirate first.
Yes. Only the value or reference is passed -- not the variable itself.
danpost danpost

2016/6/1

#
Insert a copy of line 29 at line 35 and at line 18; then remove line 29. This will have the image initially set (at line 18) when the actor is created and change (at line 35) only when needed (when the value of 'iHurt' changes). I guess you will need to change line 31 as well. Replace that one line by inserting the following there:
1
2
3
4
if (iCounter %10==0){
    iHurt = 1;
    setImage("pirate"+direction+iHurt+".png");
}
danpost danpost

2016/6/1

#
From what I can tell, the pirate maintains perfect health because the value of the 'oCounter' Counter object is never changed (it remains at zero -- unless it is being changed from another class, which I doubt). As long as it remains at zero, 'iCounter' will be initially assigned a zero value (line 30) and 'iHurt' will be set to '1' (line 31). Also, line 45 appears to be changing the value of the local variable 'iCounter' for no apparent reason. I am almost certain that these are related issues. But even more than that, both these issues have underlying issues with them. For the first issue, if you even get the value of the 'oCounter' Counter object to change, as long as it stays at a multiple of 10, the pirate will still get and maintain perfect health. For the other one, as long as the pirate is on the coin, the code at line 45 will repeatedly execute (currently, however, that line is meaningless as the local variable is lost once the method is done executing and re-initialized the next time the method is run).
KrystalLo KrystalLo

2016/6/1

#
So how can I fix these issues? Will I need to change the oCounter Multiple?
danpost danpost

2016/6/1

#
KrystalLo wrote...
So how can I fix these issues? Will I need to change the oCounter Multiple?
Well, what do you think line 45 should be doing (if not incrementing 'iCounter', which does nothing)? I believe what you really wanted to do was add the coin's value to the 'oCounter' value (but, I am not going to presume anything). And I believe that the coin should at that time be removed from the world (which would fix the underlying issue). The first issue will probably need a different condition (other than 'iCounter%10 == 0')for it to work properly (or some added condition). You will have to determine what that will be as I cannot read your mind as to what exactly you are trying to do there.
KrystalLo KrystalLo

2016/6/1

#
oh iCounter probably doesn't work since I haven't put in a int iCounter =0; for it? ... Sorry i'm not understanding too well. I'm trying to make it so when the pirate touches the bombs he'll lose a life (has 4 lives) and when he loses a life he gets gunpower all over him. But the images aren't setting properly it only shows the image for a second and turns back to normal to the pirate with full health instead of staying on the second image where he has gotten hurt. And after he loses all his lives the game would then stop. .........
danpost danpost

2016/6/1

#
Let me get this straight -- 'iHurt' is counting lives (got that); the 'oCounter' Counter object I am not sure about (please inform); the 'iCounter' int variable is assigned whatever value the 'oCounter' Counter object has, and since you are hoping to increase that value when a coin is touched, I can only think that the 'oCounter' Counter is supposed to count coin points received (but I could be wrong). Please clear up the confusion. EDIT: maybe you think that since you set the variable 'iCounter' to the value of the 'oCounter' Counter object, that by changing the value of 'iCounter', it will change the value in the 'oCounter' Counter object. Well, that is not the case, as 'iCounter' is an 'int' variable that has no connection to the 'oCounter' Counter object. You are not assigning the object to the variable; you are only setting the value of the variable to the current value held by the Counter object. No link is retained between the two.
There are more replies on the next page.
1
2
3