在比赛结束时设置胜利

时间:2015-10-01 06:56:35

标签: java

编译代码时遇到问题。当分数达到500或以上时,我想让我的“Win”图像弹出,但是我收到了这个错误:

  

“不兼容的类型:int无法转换为分数”

问题出在我的“private void LevelUp”()“

之下

这是我的代码:

    import greenfoot.*;

/**
 * Write a description of class MinionWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
    public class MinionWorld extends World
    {
    private long startMillis = 0;
    private int numFrames = 0;
    private final int groundHeight;

    public static final double MAX_FORCE = 11;
    public static final double MIN_FORCE = 7;
    public static final double GRAVITY = 0.15;
    public static final int LOWEST_ANGLE = 30;

    private Player[] players;
    private int curPlayer;
    private int turnCountdown;
    private int value; 

    Score score;
    Banan Banan;
    private int time, target;
    public static int poin, level;

    /**
     * Constructor for objects of class Banana.
     * 
     */
    public MinionWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 500, 1, false); 

        groundHeight = 440;

        players = new Player[] {new Minion()};
        int x = getWidth() / 2;

        for (Player p : players)
        {
            addObject(p, x, groundHeight);
        }
        curPlayer = 0;
        turnCountdown = 10;

        time = 1800;
        poin = 0;
        level = 1;
        target = 150;
        Greenfoot.setSpeed(50);
        prepare();
    }    


    private void showTime()
    {
        showText("Time: " + time/60, 700, 20);
    }    

    private void showTarget()
    {
        showText("Target: "+target, 714, 50);
    } 

    private void showLevel()
    {
        showText("Level: "+level, 50, 20);
    }  

    private void countTime()
    {
        showTime();
        if(time>0)
        {
            time--;
        }
        if (time == 0)
        {
            LevelUp();
        }   
    }

    private void lvup()
    {
        LvlUp LvlUp = new LvlUp();
        addObject(LvlUp,400,250);
        Greenfoot.delay(200);
        removeObject(LvlUp);
    }

    private void win()
    {
        Win Win = new Win();
        addObject(new Win(),400,250);
        Greenfoot.delay(200);
    }

    public void started()
    {
        numFrames = 0;
        startMillis = System.currentTimeMillis();
    }

    private double t;

    public void act()
    {       
        numFrames += 1;
        t = getTimePerFrame();

        if (turnCountdown > 0)
        {
            turnCountdown -= 1;
            if (turnCountdown == 0)
            {
                players[curPlayer].startTurn();
            }
        }
        countTime();
        showTarget();
        showLevel();
    }

    public void changeScore(int nilai)
    {
      if(score!=null)
      {
          score.setScore(nilai);
      }
    }

    public double getTimePerFrame()
    {
        return (double)(System.currentTimeMillis() - startMillis) / (1000.0 * (double)(numFrames));
    }

    public boolean hitsGround(double startX, double startY, double endX, double endY)
    {
        return endY > groundHeight;
    }

    public void landed()
    {
        curPlayer = (curPlayer + 1) % players.length;
        turnCountdown = 10;
    }

    private void LevelUp()
    {
        {


            if(poin>=target)
            {
                level++;
                if (level==2)
                {
                    lvup();
                    time=1800;
                    target=350;
                }
                if (level==3)
                {
                    lvup();
                    time=1800;
                    target=500;

                }
            }
            else
                {
                    if (score = 500)
                    {
                        Greenfoot.stop();
                        addObject(new Win(), 400,200);
                    }
                    else 
                    {
                        addObject(new GameOver(),400,200);
                        addObject(new TryAgain(),360, 320);
                        addObject(new Exit(),460,320);
                    }
                }
        }
    }

      /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        Menu Menu = new Menu();
        Greenfoot.setWorld(Menu); 
        score = new Score();

        Minion Minion = new Minion();
        addObject(Minion, 373, 435);
        Minion.setLocation(461, 400);
        removeObject(Minion);

        Evil1 evil1 = new Evil1();
        addObject(evil1, 697, 212);
        evil1.setLocation(577, 223);

        Evil2 evil2 = new Evil2();
        addObject(evil2, 259, 58);
        evil2.setLocation(695, 63);
        evil2.setLocation(693, 53);

        Evil3 evil3 = new Evil3();
        addObject(evil3, 742, 322);
        evil3.setLocation(575, 321);
        evil3.setLocation(323, 305);
        evil3.setLocation(573, 319);

        Evil4 evil4 = new Evil4();
        addObject(evil4, 92, 117);
        evil4.setLocation(92, 111);

        Score score = new Score();
        addObject(score, (697), 168);
        score.setLocation(757, 247);
        score.setLocation(561, 163);
        score.setLocation(463, 124);
    }
}

以下是我的Score类的代码:

import greenfoot.*;


public class Score extends Actor
{
/**
 * Act - do whatever the Score wants to do. This method is called whenever
 * the 'Act' or 'Run' button gets pressed in the environment.
 */
public Score(){
    GreenfootImage gfi = new GreenfootImage(200,200);
    gfi.setColor(java.awt.Color.white);
    gfi.setFont(new java.awt.Font("Las Vegas",java.awt.Font.PLAIN, 36));
    gfi.drawString("0", 30, 30);
    setImage(gfi);
}

public void setScore(int score) 
{
    GreenfootImage gfi = getImage();
    gfi.clear();
    gfi.drawString(score + "", 30,30);
    setImage(gfi);
}    

//just a little settlement
public void act()
{
    if(MinionWorld.poin>9)
    {
        setLocation(445, getY());
    }
}
}

“移动”类代码:

 import greenfoot.*;

/**
 * Write a description of class Move here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
    public class Move extends Actor
{
    int q = 0;
    private int Evil = 0;
/**
 * Act - do whatever the Fly wants to do. This method is called whenever
 * the 'Act' or 'Run' button gets pressed in the environment.
 */
public void act() 
{

}    
public void flyy()
{
    if (getX() == 0 || getX() == 799)
    {
        setLocation(getX(), 50+Greenfoot.getRandomNumber(320));
        turn(180);
        this.getImage().mirrorVertically();
        q++;
    }
}
public void right()
{
    move(3/2);
}
public void left()
{
    move(-3/2);
}
public void touch1()
{
    if(isTouching(Banan.class))
     {
        MinionWorld MinionWorld = (MinionWorld) getWorld();
        MinionWorld.poin = MinionWorld.poin + 10;
        MinionWorld.changeScore(MinionWorld.poin);
        removeTouching(Evil1.class);
        setLocation(799, 50+Greenfoot.getRandomNumber(320));
        left();
     }
}
public void touch2()
{
    if(isTouching(Banan.class))
    {    
        MinionWorld MinionWorld = (MinionWorld) getWorld();
         MinionWorld.poin = MinionWorld.poin + 10;
        MinionWorld.changeScore(MinionWorld.poin);
        removeTouching(Evil2.class);
        setLocation(0, 50+Greenfoot.getRandomNumber(320));
        right();
    }
}
public void touch3()
{
    if(isTouching(Banan.class))
    {    
        MinionWorld MinionWorld = (MinionWorld) getWorld();
        MinionWorld.poin = MinionWorld.poin + 10;
        MinionWorld.changeScore(MinionWorld.poin);
        removeTouching(Evil3.class);
        setLocation(799, 50+Greenfoot.getRandomNumber(320));
        left();
    }
}
public void touch4()
{
    if(isTouching(Banan.class))
    {
        MinionWorld MinionWorld = (MinionWorld) getWorld();
        MinionWorld.poin = MinionWorld.poin + 10;
        MinionWorld.changeScore(MinionWorld.poin);
        removeTouching(Evil4.class);
        setLocation(0, 50+Greenfoot.getRandomNumber(320));
        right();
    }
}

public void nulled2()
{
    if (getWorld().getObjects(Evil2.class).isEmpty())
    {
        getWorld().addObject(new Evil2(), 0, 50+Greenfoot.getRandomNumber(320));
        right();
    }
}
public void nulled4()
{
    if (getWorld().getObjects(Evil4.class).isEmpty())
    {
        getWorld().addObject(new Evil4(), 0, 50+Greenfoot.getRandomNumber(320));
        right();
    }
}

public void flyback1()
{       
   if(getX()<-15)
    {
        setLocation(800, 50+Greenfoot.getRandomNumber(320));
    }
   if(getX()>815)
   {
        setLocation(799, 50+Greenfoot.getRandomNumber(320));
        turn(180);
        this.getImage().mirrorVertically();
   }
}

public void flyback2()
{       
   if(getX()<-35)
    {
        setLocation(0, 50+Greenfoot.getRandomNumber(320));
        turn(180);
        this.getImage().mirrorVertically();
    }
   if(getX()>834)
   {
       setLocation(0, 50+Greenfoot.getRandomNumber(320));
   }
}  
}

2 个答案:

答案 0 :(得分:0)

对于评论来说这有点太长了所以我会在这里发布。

您很可能错过以下内容:

  1. field课程中的Score
  2. 该字段的getter
  3. 修复if (score = 500)并将其更改为if (score.getScore() == 500)
  4. 基本上是这样的:

    public class Score extends Actor {
        private int score;
    
        public void setScore(int score) {
            this.score = score;
            ...
        }
    
        public int getScore() {
            return this.score;
        }
    }
    

答案 1 :(得分:0)

您缺少分数字段。你的课应该是这样的:

public class Score extends Actor
{

private int score;

public int getScore(){
   return score;
}
/**
 * Act - do whatever the Score wants to do. This method is called whenever
 * the 'Act' or 'Run' button gets pressed in the environment.
 */
public Score(){
    GreenfootImage gfi = new GreenfootImage(200,200);
    gfi.setColor(java.awt.Color.white);
    gfi.setFont(new java.awt.Font("Las Vegas",java.awt.Font.PLAIN, 36));
    gfi.drawString("0", 30, 30);
    setImage(gfi);
}

public void setScore(int score) 
{
    GreenfootImage gfi = getImage();
    gfi.clear();
    this.score += score;
    gfi.drawString(this.score + "", 30,30);
    setImage(gfi);
}    

//just a little settlement
public void act()
{
    if(MinionWorld.poin>9)
    {
        setLocation(445, getY());
    }
}
}

然后你的if应该是:

if(score.getScore() == 500){
//doStuff
}