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

2019/6/2

Error when changing World

1
2
3
gacha321 gacha321

2019/6/3

#
It makes me frustate T_T
danpost danpost

2019/6/3

#
Please show codes starting:
if (isTouching(Goa.class))
gacha321 gacha321

2019/6/3

#
    
public void map(){
    if(isTouching(Goa.class)){
        Counter counter = ((FarmWorld)getWorld()).getCounter();
        GoldWorld gw = new GoldWorld();
        Greenfoot.setWorld(gw);
    }
}
gacha321 gacha321

2019/6/3

#
and Here's the counter's code
public class Counter extends Actor
{
    private int count = 100;
    
    public Counter(){
        setImage(new GreenfootImage("Gold : 100", 20, Color.WHITE, Color.BLACK));  
    }
    /**
     * Act - do whatever the Counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void walkCount (int amount) 
    {
        // Add your action code here.
        count += amount;
        setImage(new GreenfootImage("Gold : " + count,20, Color.WHITE, Color.BLACK)); 
    }    
    
    public int getValue(){
        return count;
    }
}
gacha321 gacha321

2019/6/3

#
or if you want to look all code in farmer.class because I think It will be easy to see the problems:

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

/**
 * Write a description of class Petani here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Petani extends Actor
{
    private int speed = 1;
    private GreenfootImage Bawah1 = new GreenfootImage("Bawah1.png");
    private GreenfootImage Bawah2 = new GreenfootImage("Bawah2.png");
    private GreenfootImage Bawah3 = new GreenfootImage("Bawah3.png");
    private GreenfootImage Atas1 = new GreenfootImage("Atas1.png");
    private GreenfootImage Atas2 = new GreenfootImage("Atas2.png");
    private GreenfootImage Atas3 = new GreenfootImage("Atas3.png");
    private GreenfootImage Kiri1 = new GreenfootImage("Kiri1.png");
    private GreenfootImage Kiri2 = new GreenfootImage("Kiri2.png");
    private GreenfootImage Kiri3 = new GreenfootImage("Kiri3.png");
    private GreenfootImage Kanan1 = new GreenfootImage("Kanan1.png");
    private GreenfootImage Kanan2 = new GreenfootImage("Kanan2.png");
    private GreenfootImage Kanan3 = new GreenfootImage("Kanan3.png");
    private int frame = 1;
    private int animationCounter = 0;

    /**
     * Act - do whatever the Petani 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.
        checkKey();
        touchWall();
        map();
        animationCounter++;
    }    
    
    public void touchWall(){
        if(isTouching(Tembok1.class) || (isTouching(Tembok2.class)) || (isTouching(Barrel.class)) || 
        (isTouching(Harta.class)) || (isTouching(Tanda.class)) || (isTouching(Bendera.class)) || (isTouching(Crystal.class)) ||
        (isTouching(Jerami.class))){
            if(Greenfoot.isKeyDown("S")){
            this.setLocation(getX(), getY()-speed);
            }
            if(Greenfoot.isKeyDown("W")){
            this.setLocation(getX(), getY()+speed);
            }
            if(Greenfoot.isKeyDown("A")){
            this.setLocation(getX()+speed, getY());
            }
            if(Greenfoot.isKeyDown("D")){
            this.setLocation(getX()-speed, getY());
            }
        }
    }

    public void checkKey(){
        if(Greenfoot.isKeyDown("S")){
            moveDown();           
        }
        else if(Greenfoot.isKeyDown("W")){
            moveUp();
        }
        else if(Greenfoot.isKeyDown("A")){
            moveLeft();
        }
        else if(Greenfoot.isKeyDown("D")){
            moveRight();
        }
    }
    
    public void map(){
    if(isTouching(Goa.class)){
        Counter counter = ((FarmWorld)getWorld()).getCounter();
        GoldWorld gw = new GoldWorld();
        Greenfoot.setWorld(gw);
    }
    else if(isTouching(Perkebunan.class)){
        Greenfoot.setWorld(new Board1());
    }
    else if(isTouching(Peternakan.class)){
        Greenfoot.setWorld(new Board2());
    }
    }
    
    public void moveDown(){
        Counter counter = null;
        this.setLocation(getX(), getY()+speed);
        if (getWorld() instanceof FarmWorld) {
        counter = ((FarmWorld)getWorld()).getCounter();
        if(animationCounter % 25 == 0){
        animatedBawah();
        counter.walkCount(1);
        }
        }
        else if(getWorld() instanceof GoldWorld){
        counter = ((GoldWorld)getWorld()).getCounter();
        if(animationCounter % 25 == 0){
        animatedBawah();
        counter.walkCount(2);
        }
        }
    
    }
        
    public void moveUp(){
        Counter counter = null;
        this.setLocation(getX(), getY()-speed);
        if (getWorld() instanceof FarmWorld) {
        counter = ((FarmWorld)getWorld()).getCounter();
        if(animationCounter % 25 == 0){
        animatedAtas();
        counter.walkCount(1);
        }
        }
        else if(getWorld() instanceof GoldWorld){
        counter = ((GoldWorld)getWorld()).getCounter();
        if(animationCounter % 25 == 0){
        animatedAtas();
        counter.walkCount(2);
        }
        }      
    }
    
    public void moveLeft(){
        Counter counter = null;
        this.setLocation(getX()-speed, getY());      
        if (getWorld() instanceof FarmWorld) {
        counter = ((FarmWorld)getWorld()).getCounter();
        if(animationCounter % 25 == 0){
        animatedKiri();
        counter.walkCount(1);
        }
        }
        else if(getWorld() instanceof GoldWorld){
        counter = ((GoldWorld)getWorld()).getCounter();
        if(animationCounter % 25 == 0){
        animatedKiri();
        counter.walkCount(2);
        }
        }
        
        
    }
    
    public void moveRight(){
        Counter counter = null;
        this.setLocation(getX()+speed, getY());       
        if (getWorld() instanceof FarmWorld) {
        counter = ((FarmWorld)getWorld()).getCounter();
        if(animationCounter % 25 == 0){
        animatedKanan();
        counter.walkCount(1);
        }
        }
        else if(getWorld() instanceof GoldWorld){
        counter = ((GoldWorld)getWorld()).getCounter();
        if(animationCounter % 25 == 0){
        animatedKanan();
        counter.walkCount(2);
        }
        }
        
        
    }
    
    public void animatedKanan(){
        if(frame == 1){
            setImage(Kanan1);
        }
        else if(frame == 2){
            setImage(Kanan2);
        }
        else if(frame == 3){
            setImage(Kanan3);
            frame = 1;
            return;
        }
        frame++;
    }
    
    public void animatedKiri(){
        if(frame == 1){
            setImage(Kiri1);
        }
        else if(frame == 2){
            setImage(Kiri2);
        }
        else if(frame == 3){
            setImage(Kiri3);
            frame = 1;
            return;
        }
        frame++;
    }
    
    public void animatedAtas(){
        if(frame == 1){
            setImage(Atas1);
        }
        else if(frame == 2){
            setImage(Atas2);
        }
        else if(frame == 3){
            setImage(Atas3);
            frame = 1;
            return;
        }
        frame++;
    }
    
    public void animatedBawah(){
        if(frame == 1){
            setImage(Bawah1);
        }
        else if(frame == 2){
            setImage(Bawah2);
        }
        else if(frame == 3){
            setImage(Bawah3);
            frame = 1;
            return;
        }
        frame++;
    }
    
}

danpost danpost

2019/6/3

#
gacha321 wrote...
public void map(){
    if(isTouching(Goa.class)){
        Counter counter = ((FarmWorld)getWorld()).getCounter();
        GoldWorld gw = new GoldWorld();
        Greenfoot.setWorld(gw);
    }
}
Lines 4 and 5 should be replaced with the code I gave previously starting with that line.
gacha321 gacha321

2019/6/3

#
this code? I'm so sorry :((
GoldWorld gw = new GoldWorld();
gw.getCounter().setValue(counter.getValue());
Greenfoot.setWorld(gw);
danpost danpost

2019/6/3

#
gacha321 wrote...
this code? I'm so sorry :(( << Code Omitted >>
Yes
gacha321 gacha321

2019/6/3

#
It's still error because can not find symbol - method setValue() :((
gacha321 gacha321

2019/6/3

#
    public void map(){
    if(isTouching(Goa.class)){
        Counter counter = ((FarmWorld)getWorld()).getCounter();
        GoldWorld gw = new GoldWorld();
        gw.getCounter().setValue(counter.getValue());
        Greenfoot.setWorld(gw);
    }
danpost danpost

2019/6/3

#
gacha321 wrote...
It's still error because can not find symbol - method setValue() :((
Alright, use walkCount() instead.
gacha321 gacha321

2019/6/3

#
It's run! but... it continue from 200 gold :(( didn't continue from recent gold in the farm world
gacha321 gacha321

2019/6/3

#
public void map(){
    if(isTouching(Goa.class)){
        Counter counter = ((FarmWorld)getWorld()).getCounter();
        GoldWorld gw = new GoldWorld();
        gw.getCounter().walkCount(counter.getValue());
        Greenfoot.setWorld(gw);
    }
gacha321 gacha321

2019/6/3

#
I'm sorry I mean when I earn 109 gold in Farm world but when changing world it's change to 209
gacha321 gacha321

2019/6/3

#
didn't continue from 109 again
There are more replies on the next page.
1
2
3