- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
好的,我有我的主类,它从我的框架中加载所有内容。我现在遇到的麻烦是我的碰撞检测;我明白了,所以如果它撞到什么东西,就会发生什么事。我有一个在 GameScreen 类中调用 GameOverUI 的方法,但我想在我的 Enemy.class 中使用它。我为该对象创建了一个新实例,但它说它未定义。我没有得到的是该类没有定义,但名为 GameScreen 的方法由 (Game game) 定义。
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import android.graphics.Color;
import android.graphics.Paint;
import com.vaughanslater.framework.Game;
import com.vaughanslater.framework.Graphics;
import com.vaughanslater.framework.Image;
import com.vaughanslater.framework.Input.TouchEvent;
import com.vaughanslater.framework.Screen;
public class GameScreen extends Screen {
enum GameState {
Ready, Running, Paused, GameOver
}
GameState state = GameState.Ready;
// Variable Setup
private static Background bg1, bg2;
private static Robot robot;
public static Heliboy hb, hb2;
private Image currentSprite, character, character2, character3, heliboy,
heliboy2, heliboy3, heliboy4, heliboy5;
private Animation anim, hanim;
private ArrayList tilearray = new ArrayList();
int livesLeft = 1;
Paint paint, paint2;
public GameScreen(Game game) {
super(game);
// Initialize game objects here
bg1 = new Background(0, 0);
bg2 = new Background(2160, 0);
robot = new Robot();
hb = new Heliboy(340, 360);
hb2 = new Heliboy(700, 360);
character = Assets.character;
character2 = Assets.character2;
character3 = Assets.character3;
heliboy = Assets.heliboy;
heliboy2 = Assets.heliboy2;
heliboy3 = Assets.heliboy3;
heliboy4 = Assets.heliboy4;
heliboy5 = Assets.heliboy5;
anim = new Animation();
anim.addFrame(character, 1250);
anim.addFrame(character2, 50);
anim.addFrame(character3, 50);
anim.addFrame(character2, 50);
hanim = new Animation();
hanim.addFrame(heliboy, 100);
hanim.addFrame(heliboy2, 100);
hanim.addFrame(heliboy3, 100);
hanim.addFrame(heliboy4, 100);
hanim.addFrame(heliboy5, 100);
hanim.addFrame(heliboy4, 100);
hanim.addFrame(heliboy3, 100);
hanim.addFrame(heliboy2, 100);
currentSprite = anim.getImage();
loadMap();
// Defining a paint object
paint = new Paint();
paint.setTextSize(30);
paint.setTextAlign(Paint.Align.CENTER);
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
paint2 = new Paint();
paint2.setTextSize(100);
paint2.setTextAlign(Paint.Align.CENTER);
paint2.setAntiAlias(true);
paint2.setColor(Color.WHITE);
}
private void loadMap() {
ArrayList lines = new ArrayList();
int width = 0;
int height = 0;
Scanner scanner = new Scanner(SampleGame.map);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
// no more lines to read
if (line == null) {
break;
}
if (!line.startsWith("!")) {
lines.add(line);
width = Math.max(width, line.length());
}
}
height = lines.size();
for (int j = 0; j < 12; j++) {
String line = (String) lines.get(j);
for (int i = 0; i < width; i++) {
if (i < line.length()) {
char ch = line.charAt(i);
Tile t = new Tile(i, j, Character.getNumericValue(ch));
tilearray.add(t);
}
}
}
}
@Override
public void update(float deltaTime) {
List touchEvents = game.getInput().getTouchEvents();
// We have four separate update methods in this example.
// Depending on the state of the game, we call different update methods.
// Refer to Unit 3's code. We did a similar thing without separating the
// update methods.
if (state == GameState.Ready)
updateReady(touchEvents);
if (state == GameState.Running)
updateRunning(touchEvents, deltaTime);
if (state == GameState.Paused)
updatePaused(touchEvents);
if (state == GameState.GameOver)
updateGameOver(touchEvents);
}
private void updateReady(List touchEvents) {
// This example starts with a "Ready" screen.
// When the user touches the screen, the game begins.
// state now becomes GameState.Running.
// Now the updateRunning() method will be called!
if (touchEvents.size() > 0)
state = GameState.Running;
}
private void updateRunning(List touchEvents, float deltaTime) {
// This is identical to the update() method from our Unit 2/3 game.
// 1. All touch input is handled here:
int len = touchEvents.size();
for (int i = 0; i < len; i++) {
TouchEvent event = (TouchEvent) touchEvents.get(i);
if (event.type == TouchEvent.TOUCH_DOWN) {
if (event.x > 400) {
robot.jump();
currentSprite = anim.getImage();
robot.setDucked(false);
}
else if (inBounds(event, 0, 350, 65, 65)) {
if (robot.isDucked() == false && robot.isJumped() == false
&& robot.isReadyToFire()) {
robot.shoot();
}
}
else if (event.x < 400
&& robot.isJumped() == false) {
currentSprite = Assets.characterDown;
robot.setDucked(true);
robot.setSpeedX(0);
}
//if (event.x > 400) {
// Move right.
// robot.moveRight();
// robot.setMovingRight(true);
//}
}
if (event.type == TouchEvent.TOUCH_UP) {
if (event.x < 400) {
currentSprite = anim.getImage();
robot.setDucked(false);
}
if (inBounds(event, 0, 0, 35, 35)) {
pause();
}
if (event.x > 400) {
// Move right.
robot.stopRight();
}
}
}
// 2. Check miscellaneous events like death:
if (livesLeft == 0) {
state = GameState.GameOver;
}
// 3. Call individual update() methods here.
// This is where all the game updates happen.
// For example, robot.update();
robot.update();
if (robot.isJumped()) {
currentSprite = Assets.characterJump;
} else if (robot.isJumped() == false && robot.isDucked() == false) {
currentSprite = anim.getImage();
}
ArrayList projectiles = robot.getProjectiles();
for (int i = 0; i < projectiles.size(); i++) {
Projectile p = (Projectile) projectiles.get(i);
if (p.isVisible() == true) {
p.update();
} else {
projectiles.remove(i);
}
}
updateTiles();
hb.update();
hb2.update();
bg1.update();
bg2.update();
animate();
if (robot.getCenterY() > 500) {
state = GameState.GameOver;
}
}
private boolean inBounds(TouchEvent event, int x, int y, int width,
int height) {
if (event.x > x && event.x < x + width - 1 && event.y > y
&& event.y < y + height - 1)
return true;
else
return false;
}
private void updatePaused(List touchEvents) {
int len = touchEvents.size();
for (int i = 0; i < len; i++) {
TouchEvent event = (TouchEvent) touchEvents.get(i);
if (event.type == TouchEvent.TOUCH_UP) {
if (inBounds(event, 0, 0, 800, 240)) {
if (!inBounds(event, 0, 0, 35, 35)) {
resume();
}
}
if (inBounds(event, 0, 240, 800, 240)) {
nullify();
goToMenu();
}
}
}
}
private void updateGameOver(List touchEvents) {
int len = touchEvents.size();
for (int i = 0; i < len; i++) {
TouchEvent event = (TouchEvent) touchEvents.get(i);
if (event.type == TouchEvent.TOUCH_DOWN) {
if (inBounds(event, 0, 0, 800, 480)) {
nullify();
game.setScreen(new MainMenuScreen(game));
return;
}
}
}
}
private void updateTiles() {
for (int i = 0; i < tilearray.size(); i++) {
Tile t = (Tile) tilearray.get(i);
t.update();
}
}
@Override
public void paint(float deltaTime) {
Graphics g = game.getGraphics();
g.drawImage(Assets.background, bg1.getBgX(), bg1.getBgY());
g.drawImage(Assets.background, bg2.getBgX(), bg2.getBgY());
paintTiles(g);
ArrayList projectiles = robot.getProjectiles();
for (int i = 0; i < projectiles.size(); i++) {
Projectile p = (Projectile) projectiles.get(i);
g.drawRect(p.getX(), p.getY(), 10, 5, Color.YELLOW);
}
// First draw the game elements.
g.drawImage(currentSprite, robot.getCenterX() - -15,
robot.getCenterY() - -17);
g.drawImage(hanim.getImage(), hb.getCenterX() - 48,
hb.getCenterY() - 48);
g.drawImage(hanim.getImage(), hb2.getCenterX() - 48,
hb2.getCenterY() - 48);
// Example:
// g.drawImage(Assets.background, 0, 0);
// g.drawImage(Assets.character, characterX, characterY);
// Secondly, draw the UI above the game elements.
if (state == GameState.Ready)
drawReadyUI();
if (state == GameState.Running)
drawRunningUI();
if (state == GameState.Paused)
drawPausedUI();
if (state == GameState.GameOver)
drawGameOverUI();
}
private void paintTiles(Graphics g) {
for (int i = 0; i < tilearray.size(); i++) {
Tile t = (Tile) tilearray.get(i);
if (t.type != 0) {
g.drawImage(t.getTileImage(), t.getTileX(), t.getTileY());
}
}
}
public void animate() {
anim.update(10);
hanim.update(50);
}
private void nullify() {
// Set all variables to null. You will be recreating them in the
// constructor.
paint = null;
bg1 = null;
bg2 = null;
robot = null;
hb = null;
hb2 = null;
currentSprite = null;
character = null;
character2 = null;
character3 = null;
heliboy = null;
heliboy2 = null;
heliboy3 = null;
heliboy4 = null;
heliboy5 = null;
anim = null;
hanim = null;
// Call garbage collector to clean up memory.
System.gc();
}
private void drawReadyUI() {
Graphics g = game.getGraphics();
g.drawARGB(155, 0, 0, 0);
g.drawString("Tap to Start.", 400, 240, paint);
}
private void drawRunningUI() {
Graphics g = game.getGraphics();
//g.drawImage(Assets.button, 0, 285, 0, 0, 65, 65);
//g.drawImage(Assets.button, 0, 350, 0, 65, 65, 65);
//g.drawImage(Assets.button, 0, 415, 0, 130, 65, 65);
g.drawImage(Assets.button, 0, 0, 0, 195, 35, 35);
}
private void drawPausedUI() {
Graphics g = game.getGraphics();
// Darken the entire screen so you can display the Paused screen.
g.drawARGB(155, 0, 0, 0);
g.drawString("Resume", 400, 165, paint2);
g.drawString("Menu", 400, 360, paint2);
}
public void drawGameOverUI() {
Graphics g = game.getGraphics();
g.drawRect(0, 0, 1281, 801, Color.BLACK);
g.drawString("GAME OVER.", 400, 240, paint2);
g.drawString("Tap to return.", 400, 290, paint);
}
@Override
public void pause() {
if (state == GameState.Running)
state = GameState.Paused;
}
@Override
public void resume() {
if (state == GameState.Paused)
state = GameState.Running;
}
public GameState getState() {
return state;
}
public void setState(GameState state) {
this.state = state;
}
@Override
public void dispose() {
}
@Override
public void backButton() {
pause();
}
private void goToMenu() {
// TODO Auto-generated method stub
game.setScreen(new MainMenuScreen(game));
}
public static Background getBg1() {
// TODO Auto-generated method stub
return bg1;
}
public static Background getBg2() {
// TODO Auto-generated method stub
return bg2;
}
public static Robot getRobot() {
// TODO Auto-generated method stub
return robot;
}
}
这也是我的敌人类:
import android.graphics.Rect;
public class Enemy {
private int power, centerX, speedX, centerY;
private Background bg = GameScreen.getBg1();
private Robot robot = GameScreen.getRobot();
public Rect r = new Rect(0, 0, 0, 0);
public int health = 5;
private int movementSpeed;
// Behavioural Methods
public void update() {
follow();
centerX += speedX;
speedX = bg.getSpeedX() * 5 + movementSpeed;
r.set(centerX - 25, centerY - 25, centerX + 25, centerY + 35);
if (Rect.intersects(r, Robot.yellowRed)) {
checkCollision();
}
}
private void checkCollision() {
if (Rect.intersects(r, Robot.rect) || Rect.intersects(r, Robot.rect2)
|| Rect.intersects(r, Robot.rect3)
|| Rect.intersects(r, Robot.rect4)) {
GameScreen state = new GameScreen(); // This is where it errors
}
}
public void follow() {
if (centerX < -95 || centerX > 810) {
movementSpeed = 0;
}
else if (Math.abs(robot.getCenterX() - centerX) < 5) {
movementSpeed = 0;
}
else {
if (robot.getCenterX() >= centerX) {
movementSpeed = 1;
} else {
movementSpeed = -1;
}
}
}
public void die() {
}
public void attack() {
}
public int getPower() {
return power;
}
public int getSpeedX() {
return speedX;
}
public int getCenterX() {
return centerX;
}
public int getCenterY() {
return centerY;
}
public Background getBg() {
return bg;
}
public void setPower(int power) {
this.power = power;
}
public void setSpeedX(int speedX) {
this.speedX = speedX;
}
public void setCenterX(int centerX) {
this.centerX = centerX;
}
public void setCenterY(int centerY) {
this.centerY = centerY;
}
public void setBg(Background bg) {
this.bg = bg;
}
所以这是错误的地方:
GameScreen state = new GameScreen(); // This is where it errors
我不明白,我不是在调用已定义的方法 GameScreen(Game game),而是在调用未定义的类。我想使用 drawGameOverUI();。
抱歉代码墙..至少你可以看到一切。
最佳答案
如果您定义任何构造函数,则永远不会使用默认构造函数。请定义 GameScreen() 的实现或删除 GameScreen(Game game) 的实现。
像这样定义一个默认构造函数-
public GameScreen() {
super();
// Initialize game objects here
bg1 = new Background(0, 0);
bg2 = new Background(2160, 0);
robot = new Robot();
hb = new Heliboy(340, 360);
hb2 = new Heliboy(700, 360);
character = Assets.character;
character2 = Assets.character2;
character3 = Assets.character3;
heliboy = Assets.heliboy;
heliboy2 = Assets.heliboy2;
heliboy3 = Assets.heliboy3;
heliboy4 = Assets.heliboy4;
heliboy5 = Assets.heliboy5;
anim = new Animation();
anim.addFrame(character, 1250);
anim.addFrame(character2, 50);
anim.addFrame(character3, 50);
anim.addFrame(character2, 50);
hanim = new Animation();
hanim.addFrame(heliboy, 100);
hanim.addFrame(heliboy2, 100);
hanim.addFrame(heliboy3, 100);
hanim.addFrame(heliboy4, 100);
hanim.addFrame(heliboy5, 100);
hanim.addFrame(heliboy4, 100);
hanim.addFrame(heliboy3, 100);
hanim.addFrame(heliboy2, 100);
currentSprite = anim.getImage();
loadMap();
// Defining a paint object
paint = new Paint();
paint.setTextSize(30);
paint.setTextAlign(Paint.Align.CENTER);
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
paint2 = new Paint();
paint2.setTextSize(100);
paint2.setTextAlign(Paint.Align.CENTER);
paint2.setAntiAlias(true);
paint2.setColor(Color.WHITE);
}
关于java - 如果定义了一个带参数的构造函数,为什么我的代码不使用默认的非参数化构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21733256/
运行 PostgreSQL(7.4 和 8.x),我认为这是可行的,但现在我遇到了错误。 我可以单独运行查询,它工作得很好,但如果我使用 UNION 或 UNION ALL,它会抛出错误。 这个错误:
我试图为我的应用程序创建一个导航,使用抽屉导航我的 fragment 之一(HomeFragment)有一个 ViewPager,可容纳 3 个 fragment (Bundy Clock、Annou
以我目前正在开发的应用为例: - 它有一个包含多个项目的抽屉导航;现在有两个项目让我感兴趣,我将它们称为 X 和 Y。 X 和 Y 都在单击时显示包含 x 元素或 y 元素列表的 fragment 选
我有一个形状为 (370,275,210) 的 NumPy 数组,我想将其重新整形为 (275,210,370)。我将如何在 Python 中实现这一点? 370是波段数,275是行数,210是图像包
我们如何与被子 UIViewController 阻止的父 UIViewController(具有按钮)交互。显然,触摸事件不会通过子 Nib 。 (启用用户交互) 注意:我正在加载默认和自定义 NI
我是 Jpa 新手,我想执行过程 我的代码如下 private static final String PERSISTENCE_UNIT_NAME = "todos"; private static
与安装了 LAMP 的 GCE 相比,选择与 Google Cloud SQL 链接的 GCE 实例有哪些优势? 我确定 GCE 是可扩展的,但是安装在其上的 mysql 数据库的可扩展性如何? 使用
这个问题在这里已经有了答案: Value receiver vs. pointer receiver (3 个答案) 关闭 3 年前。 我刚接触 golang。只是想了解为 Calc 类型声明的两种
我不小心按了一个快捷键,一个非常漂亮的断线出现在日期上。 有点像 # 23 Jun 2010 -------------------- 有人知道有问题的快捷方式吗?? (我在 mac 上工作!) 在
我正在Scala中编写正则表达式 val regex = "^foo.*$".r 这很好,但是如果我想做 var x = "foo" val regex = s"""^$x.*$""".r 现在我们有
以下 XML 文档在技术上是否相同? James Dean 19 和: James Dean 19 最佳答案 这两个文档在语义上是相同的。在 X
我在对数据帧列表运行稳健的线性回归模型(使用 MASS 库中的 rlm)时遇到问题。 可重现的示例: var1 <- c(1:100) var2 <- var1*var1 df1 <- data.f
好的,我有一个自定义数字键盘,可以在标签(numberField)中将数字显示为 0.00,现在我需要它显示 $0.00。 NSString *digit = sender.currentTitle;
在基于文档的应用程序中,使用 XIB 文件,创建新窗口时其行为是: 根据最后一个事件的位置进行定位和调整大小 window 。 如果最后一个事件窗口仍然可见,则新窗口 窗口应该是级联的,这样它就不会直
我想使用参数进行查询,如下所示: SELECT * FROM MATABLE WHERE MT_ID IN (368134, 181956) 所以我考虑一下 SELECT * FROM MATABLE
我遇到一些性能问题。 我有一个大约有 200 万行的表。 CREATE TABLE [dbo].[M8]( [M8_ID] [int] IDENTITY(1,1) NOT NULL,
我在 jquery 中的按键功能遇到问题。我不知道为什么按键功能不起作用。我已经使用了正确的 key 代码。在我的函数中有 2 个代码,其中包含 2 个事件键,按一个键表示 (+) 代码 107 和(
我想显示音频波形,我得到了此代码,它需要.raw音频输入并显示音频波形,但是当我放入.3gp,.mp3音频时,我得到白噪声,有人可以帮助我如何使其按需与.3gp一起使用使用.3gp音频运行它。 Inp
我无法让 stristr 函数返回真值,我相信这是因为我的搜索中有一个 $ 字符。 当我这样做时: var_dump($nopricecart); 完整的 $nopricecart 值是 $0 ,我得
如果我有这样的循环: for(int i=0;i O(n) 次。所以do some执行了O(n)次。如果做某事是线性时间,那么代码片段的复杂度是O(n^2)。 关于algorithm - 带 If 语
我是一名优秀的程序员,十分优秀!