簡體   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