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

2017/1/1

Decreasing count

Hondrok Hondrok

2017/1/1

#
I want to make the count decrease (scoreCount function, line 55) and it doesn't work
import greenfoot.*;
   
public class Dolphin extends Actor
{
    private int speed = 7;
    private int vSpeed = 0;
    private int acceleration = 2;
    private int jumpStrength = 25;
    private Label myLabel;
    private int count = 0;
     
    public Dolphin (Label label)
    {
        myLabel = label;
    }
     
    public void act() 
    {
       checkKeys();
       checkFall();
       hitGoomba();
       hitCastle();
    }
     
    private void checkKeys()  
    {
       if(Greenfoot.isKeyDown("left"))
        {
           moveLeft();
        } 
       if(Greenfoot.isKeyDown("right"))
       {
           moveRight();
           scoreCount();
       }
       if(Greenfoot.isKeyDown("up"))
       {
           jump();
           scoreCount();
       }
    }    
 
    public void checkFall()
    {
        if(onGround() && vSpeed != -jumpStrength)
        {
           vSpeed=0; 
        }
        else
        {
            fall();
        }
    }
 
    public void scoreCount()
    {
        count+=1;
        myLabel.setText("SCOR: " + count);
    }
     
    public boolean onGround()
    {
        Actor under = getOneObjectAtOffset ( 0, getImage().getHeight()/2, Ground.class);
        return under != null;
    }
     
    public void fall()
    {
        setLocation ( getX(), getY() + vSpeed);
        vSpeed = vSpeed + acceleration;
    }   
         
    public void jump()
    {
       if (onGround())
       {
           vSpeed = - jumpStrength;
       }
    }
     
    public void hitGoomba()
    {
        Actor goomba = getOneIntersectingObject(Goomba.class);
        if(goomba != null)
        {
            World myWorld = getWorld();
            GameOver gameover = new GameOver();
            myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
            myWorld.removeObject(this);
            myWorld.removeObject(goomba);
        }
    }
     
    public void hitCastle()
    {
        Actor castle = getOneIntersectingObject(Castle.class);
        if(castle != null)
        {
            World myWorld = getWorld();
            GameOver_Win gameover_win = new GameOver_Win();
            myWorld.addObject(gameover_win, myWorld.getWidth()/2, myWorld.getHeight()/2);
            myWorld.removeObject(this);
        }
    }
     
    public void moveRight()
    {
        setLocation ( getX() + speed, getY());
    }
     
    public void moveLeft()
    {
        setLocation ( getX() - speed, getY());
    }
     
     
}
danpost danpost

2017/1/1

#
Your label is in the world class. Need to see that code.
Hondrok Hondrok

2017/1/1

#
danpost wrote...
Your label is in the world class. Need to see that code.
Here it is:
import greenfoot.*;

public class MyWorld extends World
{
    int x;
    public MyWorld()
    {
        super(1500, 730, 1); 
        prepare();
    }

    public void prepare()
    {
        Ground ground = new Ground();
        Label label = new Label("SCORE : 0");
        Dolphin dolphin = new Dolphin(label);
        Castle castle = new Castle();
        Goomba goomba = new Goomba();
        Goomba_1 goomba1 = new Goomba_1();
        Goomba_2 goomba2 = new Goomba_2();
        Goomba_3 goomba3 = new Goomba_3();

        addObject(dolphin,/**x*/64, /**y*/689);
        addObject(ground, 538, 722);    
        addObject(label, 1359, 495);
        addObject(castle, 1359, 689);
        addObject(goomba1, 250, 689);
        addObject(goomba2, 500, 689);
        addObject(goomba3, 750, 689);
    }
}
danpost danpost

2017/1/1

#
Good so far. Now the Label class.
Hondrok Hondrok

2017/1/1

#
danpost wrote...
Good so far. Now the Label class.
import greenfoot.*;
public class Label extends Actor
{
    public Label(String text)
    {
        GreenfootImage img = new GreenfootImage (text.length()*20, 30); 
        img.drawString(text, 2, 20);
        img.setFont(new java.awt.Font("Algerian", java.awt.Font.PLAIN, 20));  
        setImage (img);
    }
    
    public void setText(String text)
    {
        GreenfootImage img = getImage();
        img.clear();
        img.drawString(text, 2, 20);
        img.setFont(new java.awt.Font("Algerian", java.awt.Font.PLAIN, 30));        
    }
}
danpost danpost

2017/1/1

#
Sorry, still looking.
Hondrok Hondrok

2017/1/1

#
ok
danpost danpost

2017/1/1

#
Why does lines 16 have a larger font size than line 7? An helping hint will ensue if you find that was the cause and you want one.
Hondrok Hondrok

2017/1/1

#
Fixed that thank you :D
danpost danpost

2017/1/1

#
Hondrok wrote...
Fixed that thank you :D
You may find after scoring 10, 20, or 100 points that displayed score will be cut off at the end. You are setting a fixed width to the image based on the initial text length. Either make the width some width that will accommodate whatever length the text could be or scale the image each time the text changes (scaling will cause a centered text look--slight shifting in where text begins as more digit are added to the score; while an initially wider image will cause a left-adjusted text look-- left side remains in place while more digits are added to the right).
danpost danpost

2017/1/1

#
Helping hint: When you have a 'set' method, it is usually best to use it in the constructor instead of creating duplicate code. Your Label class could be better written as follows:
public class Label extends Actor
{
    public Label(String text)
    {
        GreenfootImage img = new GreenfootImage (text.length()*20+60, 30); 
        img.setFont(new java.awt.Font("Algerian", java.awt.Font.PLAIN, 30));
        setImage(img);
        setText(text);
    }
     
    public void setText(String text)
    {
        GreenfootImage img = getImage();
        img.clear();
        img.drawString(text, 2, 30);
    }
}
You need to login to post a reply.