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

2016/6/7

Putting a actor in another world (text)

1
2
danpost danpost

2016/6/7

#
KrystalLo wrote...
Sorry, i'm not understanding anything. ... Theirs a error with new gameOver, counter and go.
Replace 'counter' with 'myCounter'. Replace 'go' with 'g'. Use your original Gameover constructor with the prepare code in it.
KrystalLo KrystalLo

2016/6/7

#
Sorry, like this? I'm not too sure what to do with constructor new gameOver as its popping up with an error.
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 = 3;
    int iCounter =0;
    int iHurt =1; 
    Counter myCounter;
    private String direction = "right";
    public Pirate(Counter 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();
        setImage("pirate"+direction+iHurt+".png");

        if (isTouching(Bomb.class)){
            removeTouching(Bomb.class);
            iHurt++;
            Greenfoot.playSound("explode.wav");
            setImage("pirate"+direction+iHurt+".png");
            if(iHurt>3){
                gameOver g = new gameOver();
                g.addObject(myCounter, g.getWidth()/2, 80);
                Greenfoot.setWorld(g);

            }
        }
        if (isTouching(Coin.class))
        {
            Coin coin = (Coin)getOneIntersectingObject(Coin.class);
            ((Counter)getWorld().getObjects(Counter.class).get(0)).add(coin.getPoints());
            getWorld().removeObject(coin);
            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/7

#
Replacing 'counter' with 'myCounter' may not work since you are not using the Counter argument in the Pirate class constructor. Change line 15 of the Pirate class to:
public Pirate() {
and create the Pirate object with 'new Pirate()' (without passing a Counter object). Then replace 'counter' above with '(Actor)getWorld().getObjects(Counter.class).get(0)'.
danpost danpost

2016/6/7

#
The Gameover constructor should be:
public Gameover()
{
    super(659, 483, 1);
    addObject(new Gameover1(), 350, 219);
}
KrystalLo KrystalLo

2016/6/7

#
Sorry, please void this message. I don't know how to delete this.
KrystalLo KrystalLo

2016/6/7

#
So is it something like this? Theirs errors. Pretty sure i'm incorrect though.
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 = 3;
    int iCounter =0;
    int iHurt =1; 
    private String direction = "right";
    (Actor)getWorld().getObjects(Counter.class).get(0)
    public Pirate(){
        new Pirate();
        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();
        setImage("pirate"+direction+iHurt+".png");

        if (isTouching(Bomb.class)){
            removeTouching(Bomb.class);
            iHurt++;
            Greenfoot.playSound("explode.wav");
            setImage("pirate"+direction+iHurt+".png");
            if(iHurt>3){
                gameOver g = new gameOver();
                g.addObject(myCounter, g.getWidth()/2, 80);
                Greenfoot.setWorld(g);

            }
        }
        if (isTouching(Coin.class))
        {
            Coin coin = (Coin)getOneIntersectingObject(Coin.class);
            ((Counter)getWorld().getObjects(Counter.class).get(0)).add(coin.getPoints());
            getWorld().removeObject(coin);
            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/7

#
danpost wrote...
replace 'counter' above with '(Actor)getWorld().getObjects(Counter.class).get(0)'.
This is for line 39 where 'counter' is now 'myCounter'.
KrystalLo KrystalLo

2016/6/7

#
if (isTouching(Bomb.class)){ removeTouching(Bomb.class); iHurt++; Greenfoot.playSound("explode.wav"); setImage("pirate"+direction+iHurt+".png"); if(iHurt>3){ gameOver g = new gameOver(); g.addObject(myCounter((Actor)getWorld().getObjects(Counter.class).get(0)); Greenfoot.setWorld(g); } } Sorry, is it like this? I doubt though, its incorrect. Sorry for the inconvenience , I don't understand code very well.
danpost danpost

2016/6/7

#
No. More like this:
g.addObject((Actor)getWorld().getObjects(Counter.class).get(0), g.getWidth()/2, 80);
You need to login to post a reply.
1
2