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/6/1

#
KrystalLo wrote...
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.
Try commenting out the following 'if' block:
if (iCounter %10==0){
    iHurt = 1;
    setImage("pirate"+direction+iHurt+".png");
}
That entire block should probably be removed entirely.
KrystalLo KrystalLo

2016/6/1

#
I'm not too sure about the oCounter iCounter code, i'll have to ask my teacher as I just used one of the old codes we had. And I commented out the entire block of that if statement. And now once the pirate touches the bomb he's images change, but now he is now not moving left or right when I move him on the arrow keys.
danpost danpost

2016/6/1

#
KrystalLo wrote...
I'm not too sure about the oCounter iCounter code, i'll have to ask my teacher as I just used one of the old codes we had. And I commented out the entire block of that if statement. And now once the pirate touches the bomb he's images change, but now he is now not moving left or right when I move him on the arrow keys.
Well, I doubt he was before. Change "Right" to "right" and "Left" to "left" when checking the movement keys in the Pirate class.
KrystalLo KrystalLo

2016/6/1

#
    private void checkKeys(){
        if(Greenfoot.isKeyDown("right")){[h1]
            direction = "right";
            this.setLocation(getX()+moveBy,getY());

        }
        if (Greenfoot.isKeyDown("left")){
            direction = "left";[/h1]
            this.setLocation(getX()-moveBy,getY());

        }
    }
}
Doesn't seem to be working. Oh, and when the pirate gets hit by the bomb and changes image, he also turns right or left and just stays in that direction until he gets hit by another bomb.
danpost danpost

2016/6/2

#
KrystalLo wrote...
< Code Omitted > Doesn't seem to be working. Oh, and when the pirate gets hit by the bomb and changes image, he also turns right or left and just stays in that direction until he gets hit by another bomb.
Please post your updated Pirate class code. From what I can tell, the pirate should be able to move with the keys after a bomb is contacted. You may have to post the Bomb class code also.
KrystalLo KrystalLo

2016/6/2

#
Pirate Code
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 iCounter =0;
    int iHurt =1; 
    Counter oCounter;
    boolean boolExplode =false;
    private String direction = "right";
    public Pirate(Counter myCounter){
        oCounter = myCounter;
        setImage("pirate"+direction+iHurt+".png");
    }

    /**
     * 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();

        int iCounter = oCounter.getValue();
        //if (iCounter%10 == 0){
           // iHurt = 1;
           // setImage("pirate"+direction+iHurt+".png");
        //}
        if(isTouching(Bomb.class) && (boolExplode==false)){
            boolExplode=true;
            iHurt++;
            Greenfoot.playSound("explode.wav");
            setImage("pirate"+direction+iHurt+".png");
            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());

        }
    }
}
Bomb Code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Bomb here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bomb extends Actor
{
    private boolean boolRemove; 
    /**
     * Act - do whatever the Bomb wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
     boolRemove = false;
     boolRemove = wasITouched();
    {
        // Add your action code here.
        setLocation(this.getX(),this.getY()+3);
        // at the bottom of the world delete the coin
        if(getY()>430){
            boolRemove = true;
        }
    }  
    if (boolRemove){
        this.getWorld().removeObject(this); 
    }
    }    
    private boolean wasITouched(){
        return (isTouching(Pirate.class));
        
    }
}
danpost danpost

2016/6/2

#
Okay. Let us simplify the code a little bit to make it easier to work with. Instead of having the boolean 'boolExplode' in the Pirate class and the Bomb object removing itself when touching a pirate using the 'boolRemove' variable. We can just have the pirate remove the bomb when touched. In the Pirate class: - remove line 15; - remove line 38; - remove lines 46 through 48; - remove and insert the following at line 37:
if (isTouching(Bomb.class)){
    removeTouching(Bomb.class);
In the Bomb class: - remove line 11 - replace the entire act method with this:
public void act()
{
    setLocation(getX(), getY());
    if (getY() > 430) getWorld().removeObject(this);
}
After these changes, test it out and report back what needs to be done next.
KrystalLo KrystalLo

2016/6/2

#
Okay so after doing that the character still doesn't change direction, and the bombs are now getting stuck in the Y = 0
danpost danpost

2016/6/2

#
I missed adding the '+3' to the 'getY()' in the 'setLocation' line of the Bomb act method. My last line 3 should be:
setLocation(getX(), getY()+3);
Then, you need a copy of the following line inserted at line 31 of the Pirate class:
setImage("pirate"+direction+iHurt+".png");
KrystalLo KrystalLo

2016/6/2

#
It's working now! Thank you so much Dan!
danpost danpost

2016/6/2

#
KrystalLo wrote...
It's working now! Thank you so much Dan!
I did not get to the last change that could be done to the Bomb class. You can also remove the 'wasITouched' method from the class.
You need to login to post a reply.
1
2
3