简体   繁体   English

试图通过setter和getter增加值,但是值不会增加?

[英]Trying to increment a value through setter and getter, but value won't increment?

In my assingment, I'm supposed to modify a greenfoot project to display the values of viruses and bacteria touched and missed in the game by creating private data types and using setters and getters to demonstrate understanding of data encapsulation, as well as modify the scoring system. 在我的评估中,我应该通过创建私有数据类型并使用setter和getter展示对数据封装的理解,并修改得分来修改greenfoot项目,以显示游戏中接触和遗漏的病毒和细菌的值。系统。 I was able to get the scoring system to work, but when I run the game, it will only increment the values of setters by one and won't count passed that. 我能够使用计分系统,但是当我运行游戏时,它只会将二传手的值增加一,而不会计入通过的次数。

 public class Bloodstream extends World
{
    private int score;
    private int time;

    /**New Private variables **/

    private int numberOfBacteriaTouched;
    private int numberOfBacteriaMissed;
    private int numberOfVirusTouched;
    private int numberOfVirusMissed;

    /**
     * Constructor: Set up the staring objects.
     */
    public Bloodstream()
    {    
        super(780, 360, 1); 
        setPaintOrder(Border.class);
        prepare();
        score = 0;
        time = 2000;
        this.numberOfBacteriaTouched= 0;
        this.numberOfBacteriaMissed = numberOfBacteriaMissed;
        this.numberOfVirusTouched = 0;
        this.numberOfVirusMissed = numberOfVirusMissed;
        showScore();
        showTime();
    }

    /**
     * Create new floating objects at irregular intervals.
     */
    public void act()
    {
        if (Greenfoot.getRandomNumber(100) < 3)
        {
            addObject(new Bacteria(), 779, Greenfoot.getRandomNumber(360));
        }

        if (Greenfoot.getRandomNumber(100) < 1)
        {
            addObject(new Lining(), 779, 0);
        }

        if (Greenfoot.getRandomNumber(100) < 1)
        {
            addObject(new Lining(), 779, 359);
        }

        if (Greenfoot.getRandomNumber(100) < 1)
        {
            addObject(new Virus(), 779, Greenfoot.getRandomNumber(360));
        }

        if (Greenfoot.getRandomNumber(100) < 6)
        {
            addObject(new RedCell(), 779, Greenfoot.getRandomNumber(360));
        }
        countTime();
    }

    /** Getters for new variables **/

    public int getNumberOfBacteriaTouched()
    {
        return numberOfBacteriaTouched;
    }

    public int getNumberBacteriaMissed()
    {
        return numberOfBacteriaMissed;
    }

    public int getNumberOfVirusTouched()
    {
        return numberOfVirusTouched;
    }

    public int getNumberOfVirusMissed()
    {
        return numberOfVirusMissed;
    }

        /** Setters for new variables **/

    public void setNumberOfBacteriaTouched(int numberOfBacteriaTouched)
    {
        this.numberOfBacteriaTouched = numberOfBacteriaTouched;
    }

    public void setNumberOfBacteriaMissed(int numberOfBacteriaMissed)
    {
        this.numberOfBacteriaMissed= numberOfBacteriaMissed;
    }

    public void setNumberOfVirusTouched(int numberOfVirusTouched)
    {
        this.numberOfVirusTouched= numberOfVirusTouched;
    }

    public void setNumberOfVirusMissed(int numberOfVirusMissed)
    {
        this.numberOfVirusMissed= numberOfVirusMissed;
    }


    /**
     * Add some points to our current score. (May be negative.)
     * If the score falls below 0, game's up.
     */
    public void addScore(int points)
    {
        score = score + points;
        showScore();
        if (score < 0) 
        {
            Greenfoot.playSound("game-over.wav");
            Greenfoot.stop();
        }
    }

    /**
     * Show our current score on screen.
     */
    private void showScore()
    {
        showText("Score: " + score, 80, 25);
        showText("Bacteria Eaten: " + getNumberOfBacteriaTouched(), 80, 325);
        showText("Virus Missed: " + getNumberOfVirusMissed(), 80, 300);
    }

    /**
     * Count down the game time and display it. Stop the game 
     * with a winning message when time is up.
     */
    private void countTime()
    {
        time--;
        showTime();
        if (time == 0)
        {
            showEndMessage();
            Greenfoot.stop();
        }
    }

    /**
     * Show the remaining game time on screen.
     */
    private void showTime()
    {
        showText("Time: " + time, 700, 25);
    }

    /**
     * Show the end-of-game message on screen.
     */
    private void showEndMessage()
    {
        showText("Time is up - you win!", 390, 150);
        showText("Your final score: " + score + " points", 390, 170);
    }

    /**
     * Prepare the world for the start of the program. In this case: Create
     * a white blood cell and the lining at the edge of the blood stream.
     */
    private void prepare()
    {
        WhiteCell whitecell = new WhiteCell();
        addObject(whitecell, 128, 179);

        Lining lining = new Lining();
        addObject(lining, 126, 1);
        Lining lining2 = new Lining();
        addObject(lining2, 342, 5);
        Lining lining3 = new Lining();
        addObject(lining3, 589, 2);
        Lining lining4 = new Lining();
        addObject(lining4, 695, 5);
        Lining lining5 = new Lining();
        addObject(lining5, 114, 359);
        Lining lining6 = new Lining();
        Lining lining7 = new Lining();
        addObject(lining7, 295, 353);
        Lining lining8 = new Lining();
        Lining lining9 = new Lining();
        Lining lining10 = new Lining();
        addObject(lining10, 480, 358);
        Lining lining11 = new Lining();
        addObject(lining11, 596, 359);
        Lining lining12 = new Lining();
        addObject(lining12, 740, 354);

        Border border = new Border();
        addObject(border, 0, 180);
        Border border2 = new Border();
        addObject(border2, 770, 180);
    }
}

Thats a copy of the main class, and here is the subclass the counters are used in? 那是主类的副本,这是计数器使用的子类?

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


public class WhiteCell extends Actor
{
    /**
     * Act: move up and down when cursor keys are pressed.
     */
    public void act() 
    {
        checkKeyPress();
        checkCollision();
    }

    /**
     * Check whether a keyboard key has been pressed and react if it has.
     */
    private void checkKeyPress()
    {
        if (Greenfoot.isKeyDown("up")) 
        {
            setLocation(getX(), getY()-8);
        }

        if (Greenfoot.isKeyDown("down")) 
        {
            setLocation(getX(), getY()+8);
        }

        if (Greenfoot.isKeyDown("right")) 
        {
            setLocation(getX()+4, getY());
        }

        if (Greenfoot.isKeyDown("left")) 
        {
            setLocation(getX()-4, getY());
        }
    }

    /**
     * Check whether we are touching a bacterium or virus. Remove bacteria.
     * Game over if we hit a virus.
     */
    private void checkCollision()
    {
        if (isTouching(Bacteria.class)) 
        {
            Greenfoot.playSound("slurp.wav");
            removeTouching(Bacteria.class);
            Bloodstream bloodstream = (Bloodstream)getWorld();
            bloodstream.addScore(1);
            bloodstream.setNumberOfBacteriaTouched(+1);
        }

        if (isTouching(Virus.class)) 
        {
            removeTouching(Virus.class);
            Bloodstream bloodstream = (Bloodstream)getWorld();
            bloodstream.addScore(-2);
            bloodstream.setNumberOfVirusTouched(+1);
        }
    }
}

Thanks for the help! 谢谢您的帮助!

If you look at the setter code, it is meant to set the value and not increment it. 如果您查看设置器代码,则意味着设置该值而不是增加它。 It is setting the value of numberOfBacteriaTouched to 1 every time it is executed. 每次执行时,它将numberOfBacteriaTouched的值设置为1。 This is why your counter works for the first time, but does not count pass one. 这就是为什么您的计数器是首次工作,但不计数一次的原因。

    public void setNumberOfBacteriaTouched(int numberOfBacteriaTouched)
    {
        this.numberOfBacteriaTouched = numberOfBacteriaTouched;
    }

The correct way to increment the value will be to get the value using the getter, add one to the value and set it using the setter. 正确增加值的方法是使用getter获取值,将值加1并使用setter进行设置。

    bloodstream.setNumberOfBacteriaTouched(bloodstream.getNumberOfBacteriaTouched()+1);
    bloodstream.setNumberOfVirusTouched(bloodstream.getNumberOfVirusTouched()+1);

Alternatively, you can also write a different method that increments the variable by 1. 另外,您也可以编写另一种方法,将变量加1。

    public void incrementNumberOfBacteriaTouched()
    {
        this.numberOfBacteriaTouched++;
    }
public void setNumberOfBacteriaTouched(int numberOfBacteriaTouched)
{
    this.numberOfBacteriaTouched = numberOfBacteriaTouched;
}

Take a look at what that method is doing. 看一下该方法在做什么。 It's setting the number touched to whatever value you passed in. You're not adding the value, you're setting a completely new one. 它会将触摸的数字设置为您传递的任何值。您没有添加该值,而是设置了一个全新的值。

You are reinitializing the class Bloodstream every time you checkCollision() is called. 每次调用checkCollision()时,您都将重新初始化类Bloodstream。 Try to move the instantiation out of checkCollision() method 尝试将实例化移出checkCollision()方法

    private Bloodstream bloodstream;
      onCreate(){
      bloodstream = (Bloodstream)getWorld();          
   } 
   private void checkCollision(){
        if (isTouching(Bacteria.class)) 
        {
            Greenfoot.playSound("slurp.wav");
            removeTouching(Bacteria.class);
            bloodstream.addScore(1);
            bloodstream.setNumberOfBacteriaTouched(+1);
        }

        if (isTouching(Virus.class)) 
        {
            removeTouching(Virus.class);
            bloodstream.addScore(-2);
            bloodstream.setNumberOfVirusTouched(+1);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM