首页 > 解决方案 > I am having trouble understanding constructor parameter values relating to coordinate detection

问题描述

package tictactoe;

import processing.core.PApplet;


public class TicTacToe extends PApplet {

    int cols = 3;
    int rows = 3;
    int h;
    int w;


    public static void main(String[] args) {
        PApplet.main("tictactoe.TicTacToe");

    }

    public void setup() {
        //Organization for size of columns and rows
        w = width / cols;
        h = height / rows;
    }

    public void settings() {
        size(300, 300);

    }


    public void draw() {
        //Draw a tic-tac-toe grid
        background(255);
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                stroke (0);
                noFill();
                rect(i*w, j*h, w, h);

            }
        }





        }
    public class GridSquare{
        //Game state (x's and o's), variables, etc.
        public float x;
        public float y;
        public float w;
        public float h;
        public int state;
        public void drawTurn() {
            if (state == 0) {
                ellipse(x+w/2,y+h/2,w,h);

            }
            if (state == 1) {
                line(x,y,x+w,y+h);
                line(x+w,y,x,y+h);


            }


        }
        //Trouble understanding Grid Detection

        void onClick(int clickedX, int clickedY, int turn) {
            if (clickedX > x && clickedX < x + w && clickedY > y && clickedY < y + h) {

        }
    }
    }
}

I am having trouble understanding the grid detection if statement. Since the parameters x, y, w, and h all had a semi colon when being defined in the GridSquare class, wouldn't that mean that those variables all equal 0? In that case, wouldn't the if statement be checking if the click x integer is greater than 0, and clicked x is less than 0 (x) + 0 (w)? Or am I wrong, and are the variables representing integers previously defined in the code?

标签: javaprocessing

解决方案


如果您考虑同时进行多个游戏(GridSquares),每个游戏周围都有一个边框,并且每个游戏都绘制在同一个面板上,这可能更容易理解。检查只是确定面板中的点击是否对该游戏有效(GridSquare)。一次玩多个游戏,每个游戏的 x 和 y 值都会不同;想象两个游戏,并排,第一个可能有 (x=0, y=0),但第二个游戏将在右侧至少 300 个单位(可能是像素),给出值 (x=300, y =0)。


推荐阅读