繁体   English   中英

setValue之后的不同值

[英]Different value after setValue

我是Java新手。 我有一个学校项目-正在创建2048游戏。 我们有由我的主持人创建的模板,它基于Greenfoot。

编辑:我注意到的另一件事:

       fields[x][y].setValue(10);
        System.out.println("x1"+fields[x][y].getIntValue());
       fields[x-1][y].setValue(newValue);
        System.out.println("x1"+fields[x][y].getIntValue());

我将x位置的字段更改为10,此后我将x位置的字段值打印为10,然后在x-1位置将field值设置为2,然后在x位置打印字段,并将值是2而不是10。为什么?

当前字段有设置值的功能,但是在设置新值后,程序将返回不同的值。

在这里,我尝试获取字符串和整数值(不知道问题可能在哪里,所以我尝试了此操作-没有帮助):

package cz.mendelu.pjj.game2048.greenfoot;

import greenfoot.Actor;
import greenfoot.GreenfootImage;
import greenfoot.World;

import java.awt.*;

public class FieldActor extends Actor {

    private static final float ONE = Game2048World.SIZE * Game2048World.SIZE;
    private static final Font FONT = new Font(Font.MONOSPACED, Font.BOLD, 64);
    private static final int MARGIN = 5;
    private static String number = "";
    private static int sentValue = 0;

    public void setValue(int value) {
        GreenfootImage image = new GreenfootImage(Game2048World.CELL, Game2048World.CELL);

        sentValue = value;

        // Calculate nice color for background
        Color color = Color.LIGHT_GRAY;
        if (value > 1) {
            float base = Integer.SIZE - Integer.numberOfLeadingZeros(value);
            float c = base / ONE;
            color = new Color(1f - c, 1f, c);
        }

        // Draw background rectangle
        image.setColor(color);
        image.fillRect(MARGIN, MARGIN, Game2048World.CELL - (MARGIN * 2), Game2048World.CELL - (MARGIN * 2));

        // If not 0 draw Number
        if (value != 0) {
            image.setColor(Color.BLACK);
            number = Integer.toString(value);
            image.setFont(FONT);
            int x = Game2048World.CELL / 2 - (number.length() * 19);
            int y = Game2048World.CELL / 2 + 26;
            image.drawString(number, x, y);
        }
        setImage(image);
    }

    public String getValue() {
        return number;
    }

    public int getIntValue() {
        return sentValue;
    }

}

在此类中,我尝试更改当前字段的值:

    package cz.mendelu.pjj.game2048;


import cz.mendelu.pjj.game2048.greenfoot.FieldActor;

public class Game2048 {

    public Game2048(int size) {

    }

    public int get(int x, int y, FieldActor[][] fields) {

        return 1;

    }

    public void addNewNumber() {
        System.out.println("addNewNumber");

        // Pokud číslo nejde pridat čísla (všechna pole jsou plná), pak vyvolejte kontrolovanou výjimku AddNewNumberException.
    }


    public boolean moveLeft(FieldActor[][] fields) {
        System.out.println("moveLeft");
        int x = 1;
        int y = 1;
        FieldActor currentField = fields[x][y];
        FieldActor previousField = fields[x-1][y];

        if (currentField.getValue() == previousField.getValue()) {
            System.out.println("true  condition: ");
            System.out.println("current field: "+currentField.getIntValue());
            System.out.println("previous field: "+previousField.getIntValue());

            int valueOfCurrent = currentField.getIntValue();
            int valueOfPrevious = previousField.getIntValue();
            int newValue = valueOfCurrent+valueOfPrevious;

            previousField.setValue(newValue);
            currentField.setValue(0);
            System.out.println("after setValue: ");
            System.out.println("current field: "+currentField.getIntValue());
            System.out.println("ValueOfCurrent "+valueOfCurrent);
            System.out.println("previous field: "+previousField.getIntValue());
            System.out.println("newValue "+newValue);
            System.out.println("previous field: "+previousField.getIntValue());
            System.out.println("valueOfPrevious "+valueOfPrevious);
            System.out.println("string value "+previousField.getValue());
        } else {
            System.out.println("false  condition: ");
        }

        return false;
    }

    public boolean moveRight() {
        System.out.println("moveRight");
        return false;
    }

    public boolean moveUp() {
        System.out.println("moveUp");
        return false;
    }

    public boolean moveDown() {
        System.out.println("moveDown");
        return false;
    }
}

打印值:

currentField.setValue(0); moveLeft真实条件:当前字段:1上一个字段:setValue之后的1:当前字段:0 ValueOfCurrent 1上一个字段:0 newValue 2上一个字段:0 valueOfPrevious 1字符串值2

游戏中显示的值:第一个向左移动:值是2和0第二个向左移动:值是0和0

currentField.setValue(10); moveLeft真实条件:当前字段:1上一个字段:setValue之后的1:当前字段:10 ValueOfCurrent 1上一个字段:10 newValue 2上一个字段:10 valueOfPrevious 1字符串值10

游戏中显示的值:左移第一位:值为2和10左移第二位:值分别为20和10第三位...仍为20和10

此类是模板的一部分:

package cz.mendelu.pjj.game2048.greenfoot;

import cz.mendelu.pjj.game2048.Game2048;
import greenfoot.Greenfoot;
import greenfoot.World;

public class Game2048World extends World {

    static final int SIZE = 4;
    static final int CELL = 200;

    private Game2048 game2048 = new Game2048(SIZE);

    private FieldActor[][] fields = new FieldActor[SIZE][SIZE];

    public Game2048World() {
        super(SIZE, SIZE, CELL);
        for (int x = 0; x < SIZE ; x++) {
            for (int y = 0; y < SIZE; y++) {
                fields[x][y] = new FieldActor();
                addObject(fields[x][y], x, y);
            }
        }
        update();
    }

    @Override
    public void act() {
        String key = Greenfoot.getKey();
        if (key != null) {
            boolean valid = false;
            if (key == "left") {
                valid = game2048.moveLeft(fields);
            } else if (key == "right") {
                valid = game2048.moveRight();
            } else if (key == "up") {
                valid = game2048.moveUp();
            } else if (key == "down") {
                valid = game2048.moveDown();
            }

            if (valid == true) {
                try {
                    game2048.addNewNumber();
//                    update();
                } catch (RuntimeException e) {
                    //Gr
                }

            }
        }
    }


    private void update() {
        for (int x = 0; x < SIZE ; x++) {
            for (int y = 0; y < SIZE; y++) {
                fields[x][y].setValue(game2048.get(x, y, fields));
            }
        }
    }
}

模板中包含FieldActor和Game2048World类。 在FieldActor中,我刚刚添加了getValue和getIntValue并将更改后的数字和intValue作为全局变量

谢谢您的帮助!

我认为混淆可能在于sendValue是静态的。 静态一词表示所有FieldActor实例之间共享一个sendValue字段。 因此,当您为一个FieldActor设置值时(实际上是:为所有它们设​​置共享值),对于另一个FieldActor来说似乎就改变了。 我认为,如果使相关字段为非静态,则将开始获得您期望的行为。

暂无
暂无

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

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