作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试制作这个游戏,现在我有一个可以移动的角色,但我想有效地改变角色的宽度和高度。因此,角色上下移动的尺寸与角色左右移动的尺寸不同,所以我做了这个 if 语句,如果玩家向上移动,那么宽度就是这个,如果玩家向右移动,那么 with 就是这样。但是当我写完代码时,它给了我两个错误:
Syntax error on token ";", { expected after this token
Syntax error, insert "}" to complete Block
有人可以告诉我出了什么问题吗?
代码如下:
public class player extends creature {
// other code
private static int player_width;
private static int player_height = 70; // error 1 here
if(currentimage == assets.player_up) {
player_width = 50;
} else if(currentimage == assets.player_down) {
player_width = 50;
} else if(currentimage == assets.player_right) {
player_width = 60;
} else if(currentimage == assets.player_left) {
player_width = 60;
} // error 2 here
public player(game game, float x, float y) {
super(x, y,player_width,player_height);
this.game = game;
}
这是生物构造函数:
public abstract class creature extends entity {
public static final int DEFAULT_HEALTH = 10;
public static final float DEFAULT_SPEED = 3.0f;
public static final int DEFAULT_CREATURE_WIDTH = 64,
DEFAULT_CREATURE_HEIGHT = 64;
protected int health;
protected float speed;
protected float xMove, yMove;
public creature(float x, float y, int width, int height) {
super(x, y, width, height);
health = DEFAULT_HEALTH;
speed = DEFAULT_SPEED;
xMove = 0;
yMove = 0;
}
最佳答案
嗯,你应该将这些 if 语句放在你的构造函数中。如果它们位于类内而不是方法内,则它们不会运行。只需将它们放入您的
public player(game game, float x, float y)
方法,你应该是金子。
注意:只是一个旁注,类名应该大写,这样更容易阅读代码。
关于Javaplayer_width和player_height错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44711314/
我是一名优秀的程序员,十分优秀!