找不到符号时出错 - 我程序中的变量
Error with not finding symbol - variable in my program
好的,所以我正在尝试学习 java(因为我通常用 html 和 css 做事),我只是在研究一个非常基本和通用的 2d 块断路器游戏。我遇到了这个错误,不知道为什么会收到。我对我的拼写进行了三重检查,并在多个方面进行了检查。
这是我的游戏 class 代码(错误行将用星号突出显示):
package BrickBreaker;
import java.awt.*;
import java.awt.event.*;
import javax.swing.Timer;
import javax.swing.JPanel;
public class Gameplay extends JPanel implements KeyListener, ActionListener {
private boolean play = false;
private int score = 0;
private int totalBricks = 21;
private Timer timer;
private int delay = 8;
private int playerX = 310;
private int ballposX = 120;
private int ballposY = 350;
private int ballXdir = -1;
private int ballYdir = -2;
private MapGenerator map;
public Gameplay() {
map = new MapGenerator(3, 7);
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
timer = new Timer(delay, this);
timer.start();
}
public void paint(Graphics g) {
// background
g.setColor(Color.black);
g.fillRect(1,1, 692, 592);
// drawing map
map.draw((Graphics2D)g);
// borders
g.setColor(Color.yellow);
g.fillRect(0, 0, 3, 592);
g.fillRect(0, 0, 692, 3);
g.fillRect(691, 0, 3, 592);
// paddle
g.setColor(Color.green);
g.fillRect(playerX, 550, 100, 8);
// ball
g.setColor(Color.yellow);
g.fillOval(ballposX, ballposY, 20, 20);
g.dispose();
}
@Override
public void actionPerformed(ActionEvent e) {
timer.start();
if (play){
if(new Rectangle(ballposX, ballposY, 20, 20).intersects(new Rectangle(playerX, 550, 100, 8))){
ballYdir = -ballYdir;
}
A: for (int i = 0; i < map.map.length; i++) {
for (int j = 0; j< map.map.length; j++) {
if (map.map[i][j] > 0) {
int brickX = j * map.brickWidth + 80;
int brickY = i * map.brickHeight + 50;
int brickWidth = map.brickWidth;
int brickLength = map.brickWidth;
************************Rectangle rect = new Rectangle(brickX, brickY, brickWidth, brickHeight);
Rectangle ballRect = new Rectangle(ballposX, ballposY, 20, 20);
Rectangle brickRect = rect;
if (ballRect.intersects(brickRect)) {
map.setBrickValue(0, i, j);
totalBricks--;
score += 5;
if (ballposX + 19 <= brickRect.x || ballposX + 1 >= brickRect.x + brickRect.width) {
ballXdir = -ballXdir;
}
else {
ballYdir = -ballYdir;
}
break A;
}
}
}
}
ballposX += ballXdir;
ballposY += ballYdir;
if(ballposX < 0) {
ballXdir = -ballXdir;
}
if(ballposY < 0) {
ballYdir = -ballYdir;
}
if(ballposX > 670) {
ballXdir = -ballXdir;
}
}
repaint();
}
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void keyReleased(KeyEvent e) {}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
if (playerX >= 600) {
playerX = 600;
} else{
moveRight();
}
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
if (playerX < 10) {
playerX = 10;
} else{
moveLeft();
}
}
}
public void moveRight() {
play = true;
playerX +=20;
}
public void moveLeft() {
play = true;
playerX -=20;
}
}
我收到的错误代码是:
Blockquote
cannot find symbol - variable brickHeight
Blockquote
我的 MapGenerator class 代码是:(这是我最初使用 "brickHeight" 变量的地方。
package BrickBreaker;
import java.awt.*;
public class MapGenerator {
public int map[][];
public int brickWidth;
public int brickHeight;
public MapGenerator(int row, int col) {
map = new int[row][col];
for(int i = 0; i < map.length; i++) {
for(int j = 0; j < map[0].length; j++) {
map[i][j] = 1;
}
}
brickWidth = 540/col;
brickHeight = 150/col;
}
public void draw(Graphics2D g) {
for(int i = 0; i < map.length; i++) {
for(int j = 0; j < map[0].length; j++) {
if (map[i][j] > 0) {
g.setColor(Color.white);
g.fillRect(j * brickWidth + 80, i * brickHeight + 50, brickWidth, brickHeight);
g.setStroke(new BasicStroke(3));
g.setColor(Color.black);
g.drawRect(j * brickWidth + 80, i * brickHeight + 50, brickWidth, brickHeight);
}
}
}
}
public void setBrickValue(int value, int row, int col) {
map[row][col] = value;
}
}
请注意,我是 java 初学者,我仍在学习中,因此非常感谢任何提示或指示。我也没有完成这个项目,所以有些东西可能因此看起来不完整。
您使用了未定义的变量 'brickHeight'。我想你的意思是 brickHeight 等于 map.brickHeight.
查看此游戏代码 class 是否如您所愿
package BrickBreaker;
import java.awt.*;
import java.awt.event.*;
import javax.swing.Timer;
import javax.swing.JPanel;
public class Gameplay extends JPanel implements KeyListener, ActionListener {
private boolean play = false;
private int score = 0;
private int totalBricks = 21;
private Timer timer;
private int delay = 8;
private int playerX = 310;
private int ballposX = 120;
private int ballposY = 350;
private int ballXdir = -1;
private int ballYdir = -2;
private MapGenerator map;
public Gameplay() {
map = new MapGenerator(3, 7);
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
timer = new Timer(delay, this);
timer.start();
}
public void paint(Graphics g) {
// background
g.setColor(Color.black);
g.fillRect(1,1, 692, 592);
// drawing map
map.draw((Graphics2D)g);
// borders
g.setColor(Color.yellow);
g.fillRect(0, 0, 3, 592);
g.fillRect(0, 0, 692, 3);
g.fillRect(691, 0, 3, 592);
// paddle
g.setColor(Color.green);
g.fillRect(playerX, 550, 100, 8);
// ball
g.setColor(Color.yellow);
g.fillOval(ballposX, ballposY, 20, 20);
g.dispose();
}
@Override
public void actionPerformed(ActionEvent e) {
timer.start();
if (play){
if(new Rectangle(ballposX, ballposY, 20, 20).intersects(new Rectangle(playerX, 550, 100, 8))){
ballYdir = -ballYdir;
}
A: for (int i = 0; i < map.map.length; i++) {
for (int j = 0; j< map.map.length; j++) {
if (map.map[i][j] > 0) {
int brickX = j * map.brickWidth + 80;
int brickY = i * map.brickHeight + 50;
int brickWidth = map.brickWidth;
int brickHeight = map.brickHeight;
Rectangle rect = new Rectangle(brickX, brickY, brickWidth, brickHeight);
Rectangle ballRect = new Rectangle(ballposX, ballposY, 20, 20);
Rectangle brickRect = rect;
if (ballRect.intersects(brickRect)) {
map.setBrickValue(0, i, j);
totalBricks--;
score += 5;
if (ballposX + 19 <= brickRect.x || ballposX + 1 >= brickRect.x + brickRect.width) {
ballXdir = -ballXdir;
}
else {
ballYdir = -ballYdir;
}
break A;
}
}
}
}
ballposX += ballXdir;
ballposY += ballYdir;
if(ballposX < 0) {
ballXdir = -ballXdir;
}
if(ballposY < 0) {
ballYdir = -ballYdir;
}
if(ballposX > 670) {
ballXdir = -ballXdir;
}
}
repaint();
}
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void keyReleased(KeyEvent e) {}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
if (playerX >= 600) {
playerX = 600;
} else{
moveRight();
}
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
if (playerX < 10) {
playerX = 10;
} else{
moveLeft();
}
}
}
public void moveRight() {
play = true;
playerX +=20;
}
public void moveLeft() {
play = true;
playerX -=20;
}
}
好的,所以我正在尝试学习 java(因为我通常用 html 和 css 做事),我只是在研究一个非常基本和通用的 2d 块断路器游戏。我遇到了这个错误,不知道为什么会收到。我对我的拼写进行了三重检查,并在多个方面进行了检查。
这是我的游戏 class 代码(错误行将用星号突出显示):
package BrickBreaker;
import java.awt.*;
import java.awt.event.*;
import javax.swing.Timer;
import javax.swing.JPanel;
public class Gameplay extends JPanel implements KeyListener, ActionListener {
private boolean play = false;
private int score = 0;
private int totalBricks = 21;
private Timer timer;
private int delay = 8;
private int playerX = 310;
private int ballposX = 120;
private int ballposY = 350;
private int ballXdir = -1;
private int ballYdir = -2;
private MapGenerator map;
public Gameplay() {
map = new MapGenerator(3, 7);
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
timer = new Timer(delay, this);
timer.start();
}
public void paint(Graphics g) {
// background
g.setColor(Color.black);
g.fillRect(1,1, 692, 592);
// drawing map
map.draw((Graphics2D)g);
// borders
g.setColor(Color.yellow);
g.fillRect(0, 0, 3, 592);
g.fillRect(0, 0, 692, 3);
g.fillRect(691, 0, 3, 592);
// paddle
g.setColor(Color.green);
g.fillRect(playerX, 550, 100, 8);
// ball
g.setColor(Color.yellow);
g.fillOval(ballposX, ballposY, 20, 20);
g.dispose();
}
@Override
public void actionPerformed(ActionEvent e) {
timer.start();
if (play){
if(new Rectangle(ballposX, ballposY, 20, 20).intersects(new Rectangle(playerX, 550, 100, 8))){
ballYdir = -ballYdir;
}
A: for (int i = 0; i < map.map.length; i++) {
for (int j = 0; j< map.map.length; j++) {
if (map.map[i][j] > 0) {
int brickX = j * map.brickWidth + 80;
int brickY = i * map.brickHeight + 50;
int brickWidth = map.brickWidth;
int brickLength = map.brickWidth;
************************Rectangle rect = new Rectangle(brickX, brickY, brickWidth, brickHeight);
Rectangle ballRect = new Rectangle(ballposX, ballposY, 20, 20);
Rectangle brickRect = rect;
if (ballRect.intersects(brickRect)) {
map.setBrickValue(0, i, j);
totalBricks--;
score += 5;
if (ballposX + 19 <= brickRect.x || ballposX + 1 >= brickRect.x + brickRect.width) {
ballXdir = -ballXdir;
}
else {
ballYdir = -ballYdir;
}
break A;
}
}
}
}
ballposX += ballXdir;
ballposY += ballYdir;
if(ballposX < 0) {
ballXdir = -ballXdir;
}
if(ballposY < 0) {
ballYdir = -ballYdir;
}
if(ballposX > 670) {
ballXdir = -ballXdir;
}
}
repaint();
}
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void keyReleased(KeyEvent e) {}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
if (playerX >= 600) {
playerX = 600;
} else{
moveRight();
}
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
if (playerX < 10) {
playerX = 10;
} else{
moveLeft();
}
}
}
public void moveRight() {
play = true;
playerX +=20;
}
public void moveLeft() {
play = true;
playerX -=20;
}
}
我收到的错误代码是:
Blockquote cannot find symbol - variable brickHeight Blockquote
我的 MapGenerator class 代码是:(这是我最初使用 "brickHeight" 变量的地方。
package BrickBreaker;
import java.awt.*;
public class MapGenerator {
public int map[][];
public int brickWidth;
public int brickHeight;
public MapGenerator(int row, int col) {
map = new int[row][col];
for(int i = 0; i < map.length; i++) {
for(int j = 0; j < map[0].length; j++) {
map[i][j] = 1;
}
}
brickWidth = 540/col;
brickHeight = 150/col;
}
public void draw(Graphics2D g) {
for(int i = 0; i < map.length; i++) {
for(int j = 0; j < map[0].length; j++) {
if (map[i][j] > 0) {
g.setColor(Color.white);
g.fillRect(j * brickWidth + 80, i * brickHeight + 50, brickWidth, brickHeight);
g.setStroke(new BasicStroke(3));
g.setColor(Color.black);
g.drawRect(j * brickWidth + 80, i * brickHeight + 50, brickWidth, brickHeight);
}
}
}
}
public void setBrickValue(int value, int row, int col) {
map[row][col] = value;
}
}
请注意,我是 java 初学者,我仍在学习中,因此非常感谢任何提示或指示。我也没有完成这个项目,所以有些东西可能因此看起来不完整。
您使用了未定义的变量 'brickHeight'。我想你的意思是 brickHeight 等于 map.brickHeight.
查看此游戏代码 class 是否如您所愿
package BrickBreaker;
import java.awt.*;
import java.awt.event.*;
import javax.swing.Timer;
import javax.swing.JPanel;
public class Gameplay extends JPanel implements KeyListener, ActionListener {
private boolean play = false;
private int score = 0;
private int totalBricks = 21;
private Timer timer;
private int delay = 8;
private int playerX = 310;
private int ballposX = 120;
private int ballposY = 350;
private int ballXdir = -1;
private int ballYdir = -2;
private MapGenerator map;
public Gameplay() {
map = new MapGenerator(3, 7);
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
timer = new Timer(delay, this);
timer.start();
}
public void paint(Graphics g) {
// background
g.setColor(Color.black);
g.fillRect(1,1, 692, 592);
// drawing map
map.draw((Graphics2D)g);
// borders
g.setColor(Color.yellow);
g.fillRect(0, 0, 3, 592);
g.fillRect(0, 0, 692, 3);
g.fillRect(691, 0, 3, 592);
// paddle
g.setColor(Color.green);
g.fillRect(playerX, 550, 100, 8);
// ball
g.setColor(Color.yellow);
g.fillOval(ballposX, ballposY, 20, 20);
g.dispose();
}
@Override
public void actionPerformed(ActionEvent e) {
timer.start();
if (play){
if(new Rectangle(ballposX, ballposY, 20, 20).intersects(new Rectangle(playerX, 550, 100, 8))){
ballYdir = -ballYdir;
}
A: for (int i = 0; i < map.map.length; i++) {
for (int j = 0; j< map.map.length; j++) {
if (map.map[i][j] > 0) {
int brickX = j * map.brickWidth + 80;
int brickY = i * map.brickHeight + 50;
int brickWidth = map.brickWidth;
int brickHeight = map.brickHeight;
Rectangle rect = new Rectangle(brickX, brickY, brickWidth, brickHeight);
Rectangle ballRect = new Rectangle(ballposX, ballposY, 20, 20);
Rectangle brickRect = rect;
if (ballRect.intersects(brickRect)) {
map.setBrickValue(0, i, j);
totalBricks--;
score += 5;
if (ballposX + 19 <= brickRect.x || ballposX + 1 >= brickRect.x + brickRect.width) {
ballXdir = -ballXdir;
}
else {
ballYdir = -ballYdir;
}
break A;
}
}
}
}
ballposX += ballXdir;
ballposY += ballYdir;
if(ballposX < 0) {
ballXdir = -ballXdir;
}
if(ballposY < 0) {
ballYdir = -ballYdir;
}
if(ballposX > 670) {
ballXdir = -ballXdir;
}
}
repaint();
}
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void keyReleased(KeyEvent e) {}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
if (playerX >= 600) {
playerX = 600;
} else{
moveRight();
}
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
if (playerX < 10) {
playerX = 10;
} else{
moveLeft();
}
}
}
public void moveRight() {
play = true;
playerX +=20;
}
public void moveLeft() {
play = true;
playerX -=20;
}
}