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/2

#
Guys, i have an error when changing world from FarmWorld to GoldWorld the error was like this.
java.lang.ClassCastException: GoldWorld cannot be cast to FarmWorld
	at Petani.moveUp(Petani.java:79)
	at Petani.checkKey(Petani.java:45)
	at Petani.act(Petani.java:35)
what happened :(( because I think because this code
    public void moveDown(){
        FarmWorld farmWorld = (FarmWorld)getWorld();
        Counter counter = farmWorld.getCounter();
        this.setLocation(getX(), getY()+speed);
        if(animationCounter % 12 == 0){
        animatedBawah();
        counter.walkCount(1);
        }
    }
I use the move method for FarmWorld so when changing world it will send an error. The actor must have 1 gold/walk when on the FarmWorld and 2gold/walk when on the GoldWorld so I made a counter walk for that (But only at FarmWorld First because I have no idea for the gold world). Can you help me guyss :(( It's really meaningfull from me thankss
danpost danpost

2019/6/2

#
Line 2 in your moveDown and moveUp methods (and possibly other move methods) limit your actor to only be in a FarmWorld world. You could use a conditional to allow for other world types:
Counter counter = null;
if (getWorld() instanceof FarmWorld) counter = ((FarmWorld)getWorld()).getCounter();
else if .. // continue for next type world
gacha321 gacha321

2019/6/2

#
@danpost, OMG it's workss!! thank you so muchhh !!! but i have new problems. why when I change the World, my gold is reset to 0 again :( I think the counter will save the counter from the farm world before. do you have a solution for this?? I'm so thankfull to youuu
danpost danpost

2019/6/2

#
gacha321 wrote...
why when I change the World, my gold is reset to 0 again
Probably because each world created has its own Counter object and all have an initial value of zero. You could get the Counter object as above and then do something like the following:
GoldWorld gw = new GoldWorld();
gw.getCounter().setValue(counter.getValue());
Greenfoot.setWorld(gw);
gacha321 gacha321

2019/6/3

#
Where I must place that code?? because it said cannot find simbol getValue(); :((
gacha321 gacha321

2019/6/3

#
Here's my code for changing world
if(isTouching(Goa.class)){
      Greenfoot.setWorld(new GoldWorld());
}
and this is for walkcount on another world
public void moveDown(){
        Counter counter = null;
        this.setLocation(getX(), getY()+speed);
        if (getWorld() instanceof FarmWorld) {
        counter = ((FarmWorld)getWorld()).getCounter();
        if(animationCounter % 19 == 0){
        animatedBawah();
        counter.walkCount(1);
        }
        }
        else if(getWorld() instanceof GoldWorld){
        counter = ((GoldWorld)getWorld()).getCounter();
        if(animationCounter % 19 == 0){
        animatedBawah();
        counter.walkCount(2);
        }
        }
all of move (up, down , right and left) all same. but I don't know where to place your code. because in changeMap method I only use isTouching. but the Counter is on the moveKey :(
gacha321 gacha321

2019/6/3

#
Here's the Count 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;
    }
}

when I try to write your code in Here (Line 14)
    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();
        GoldWorld gw = new GoldWorld();
        gw.getCounter().setValue(counter.getValue());
        if(animationCounter % 25 == 0){
        animatedBawah();
        counter.walkCount(2);
        }
        }
    
    }
It said can not find symbol method setValue(); but when I code like this in else state
        else if(getWorld() instanceof GoldWorld){
        GoldWorld.getCounter().setValue(counter.getValue());
        if(animationCounter % 25 == 0){
        animatedBawah();
        counter.walkCount(2);
        }
It said non-static method getCounter() cannot be referred from a static context :(((
gacha321 gacha321

2019/6/3

#
Helpp me pleasee this is my final exam T^T
danpost danpost

2019/6/3

#
gacha321 wrote...
Here's the Count code << Code Omitted >> when I try to write your code in Here (Line 14) << Code Omitted >> It said can not find symbol method setValue(); but when I code like this in else state << Code Omitted >> It said non-static method getCounter() cannot be referred from a static context :(((
Those are the wrong places to put it.
gacha321 wrote...
Here's my code for changing world
if(isTouching(Goa.class)){
      Greenfoot.setWorld(new GoldWorld());
}
It would replace line 2 here. The reference to the Counter object in the current world will need to be acquired first for counter to have a value.
gacha321 gacha321

2019/6/3

#
I'm sorry but I didn't understand :(( can you tell me the detail what must I do :(( I'm a very newbie using greenfoot :(( so sorry .. the counter object in the current world is set on all moveKey();
    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);
        }
        }
        
    }
at line 5 and 12 here. How can I acquire the Counter object from the current world if the counter objects are used in moveKey() :(( I'm so sorry :(((
gacha321 gacha321

2019/6/3

#
it said can not find simbol setValue(). or I should make a set method for the current count? :((
Super_Hippo Super_Hippo

2019/6/3

#
public void setValue(int newValue)
{
    count = newValue;
}
gacha321 gacha321

2019/6/3

#
Where's must I place that code?? :( Because when I place that in farmer.class it's error too :(
danpost danpost

2019/6/3

#
You can still use the same kind of code:
Counter counter = ((FarmWorld)getWorld()).getCounter();
gacha321 gacha321

2019/6/3

#
When I use that code in isTouching(Goa.class) it's still not continue the gold from farmworld and error :(
There are more replies on the next page.
1
2
3