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

2019/6/2

Gold must increased 2times in GoldWorld

gacha321 gacha321

2019/6/2

#
hello guys, I have a problem with my game :( in first World (FarmWorld) the gold is increase 1/walk. but when we go to another World (GoldWorld) it must increased 2 times/walk. but I don't have an idea for doing that. can u help me to fix my problem :( Here's my code 1. I used the counter walk count(1) when we're on the FarmWorld. but how can I used the walk count(2) when I'm on the Gold World? :(
    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)){
        Greenfoot.setWorld(new GoldWorld());
    }
    else if(isTouching(Perkebunan.class)){
        Greenfoot.setWorld(new Board1());
    }
    }
    
    public void moveDown(){
        FarmWorld farmWorld = (FarmWorld)getWorld();
        Counter counter = farmWorld.getCounter();
        this.setLocation(getX(), getY()+speed);
        if(animationCounter % 12 == 0){
        animatedBawah();
        counter.walkCount(1);
        }
    }
        
    
    public void moveUp(){
        FarmWorld farmWorld = (FarmWorld)getWorld();
        Counter counter = farmWorld.getCounter();
        this.setLocation(getX(), getY()-speed);
        if(animationCounter % 12 == 0){
        animatedAtas();
        counter.walkCount(1);
        }
    }
    
    public void moveLeft(){
        FarmWorld farmWorld = (FarmWorld)getWorld();
        Counter counter = farmWorld.getCounter();
        this.setLocation(getX()-speed, getY());
        if(animationCounter % 12 == 0){
        animatedKiri();
        counter.walkCount(1);
        }
    }
    
    public void moveRight(){
        FarmWorld farmWorld = (FarmWorld)getWorld();
        Counter counter = farmWorld.getCounter();
        this.setLocation(getX()+speed, getY());
        if(animationCounter % 12 == 0){
        animatedKanan();
        counter.walkCount(1);
        }
    }
    
    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++;
    }
2. And Here's the counterWalk
    public void walkCount (int amount) 
    {
        // Add your action code here.
        count += amount;
        setImage(new GreenfootImage("Gold : " + count,20, Color.WHITE, Color.BLACK)); 
    }    
When I change map, It's also can;t return the value of gold that's have been earner from the farmworld :(( HEEELPPPP T^TT
danpost danpost

2019/6/2

#
Sounds like you just need to use a variable instead of literal numbers.
gacha321 gacha321

2019/6/2

#
variable in where?? I don't have an idea for that :(
danpost danpost

2019/6/2

#
gacha321 wrote...
variable in where?? I don't have an idea for that :(
You can do it without the variable field, but if you add more worlds, it will get complicated quickly. For now, use:
walkCount((getWorld() instanceof GoldWorld) ? 2 : 1);
gacha321 gacha321

2019/6/3

#
ahh I see use the ternary operator right? :D
danpost danpost

2019/6/3

#
gacha321 wrote...
ahh I see use the ternary operator right? :D
Yeah -- well it works for what you have right now (it may not be suitable later).
You need to login to post a reply.