- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试开发一个简单的游戏来学习基本的 Java 知识。一切似乎都运行良好,但过了一段时间我就会开始崩溃。我认为发生的情况是我创建了太多对象并且内存不足,但我不知道如何防止这种情况。我想知道是否有人有任何提示或可以看到我在哪里犯了错误,我很高兴听到建议。
package com.colordash.game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import java.util.Random;
class GameScreen implements Screen {
private final ColorDash game;
private Texture backgroundTexture;
private OrthographicCamera camera;
// rby ryb yrb ybr bry byr
private float screenWidth;
private float screenHeight;
private int leftPressedValue;
private int rightPressedValue;
private int measuredValue;
private int neededValue;
private float redHeight;
private float blueHeight;
private float yellowHeight;
private Stage stage;
private Texture centerDisplayTexture;
Texture buttonLRed;
private float time;
int randomNum;
public GameScreen(final ColorDash gam) {
this.game = gam;
time = 0;
backgroundTexture = new Texture(Gdx.files.internal("background2.png"));
backgroundTexture.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat);
// create the camera and the SpriteBatch
stage = new Stage(new ScreenViewport()); //Set up a stage for the ui
screenWidth = stage.getWidth();
screenHeight = stage.getHeight();
float buttonWidth = 3 * (screenWidth / 12);
float buttonHeight = 4 * (screenHeight / 12);
float buttonLower = 1 * (screenHeight / 3) - buttonHeight;
float buttonMid = 2 * (screenHeight / 3) - buttonHeight;
float buttonHigher = 3 * (screenHeight / 3) - buttonHeight;
float col1 = screenWidth - 23 * (screenWidth / 24);
float col2 = screenWidth - 1 * (screenWidth / 24) - buttonWidth;
Random randomButtons = new Random();
int buttonLocations = randomButtons.nextInt(6);
if (buttonLocations == 0) {
redHeight = buttonLower;
blueHeight = buttonMid;
yellowHeight = buttonHigher;
} else if (buttonLocations == 1) {
redHeight = buttonLower;
blueHeight = buttonHigher;
yellowHeight = buttonMid;
} else if (buttonLocations == 2) {
redHeight = buttonMid;
blueHeight = buttonLower;
yellowHeight = buttonHigher;
} else if (buttonLocations == 3) {
redHeight = buttonHigher;
blueHeight = buttonLower;
yellowHeight = buttonMid;
} else if (buttonLocations == 4) {
redHeight = buttonHigher;
blueHeight = buttonMid;
yellowHeight = buttonLower;
} else if (buttonLocations == 5) {
redHeight = buttonMid;
blueHeight = buttonHigher;
yellowHeight = buttonLower;
}
// red 1
// blue 2
// yellow 4
int[] combinations = new int[6];
combinations[0] = 2; // red red
combinations[1] = 3; // red blue
combinations[2] = 4; // blue blue
combinations[3] = 5; // yellow red
combinations[4] = 6; // yellow blue
combinations[5] = 8; // yellow yellow
leftPressedValue = 0;
rightPressedValue = 0;
measuredValue = 0;
Random rn = new Random();
int randomNum = rn.nextInt(6);
if (ColorDash.lastRandomNumber == randomNum) {
randomNum = rn.nextInt(6);
}
neededValue = combinations[randomNum];
if (neededValue == 2) {
centerDisplayTexture = new Texture(Gdx.files.internal("redBig.png"));
} else if (neededValue == 3) {
centerDisplayTexture = new Texture(Gdx.files.internal("purple.png"));
} else if (neededValue == 4) {
centerDisplayTexture = new Texture(Gdx.files.internal("blueBig.png"));
} else if (neededValue == 5) {
centerDisplayTexture = new Texture(Gdx.files.internal("orange.png"));
} else if (neededValue == 6) {
centerDisplayTexture = new Texture(Gdx.files.internal("green.png"));
} else if (neededValue == 8) {
centerDisplayTexture = new Texture(Gdx.files.internal("bigyellow.png"));
}
// center display
TextureRegion centerDisplayTextureRegion = new TextureRegion(centerDisplayTexture);
TextureRegionDrawable centerDisplayTexRegionDrawable = new TextureRegionDrawable(centerDisplayTextureRegion);
Button centerDisplay = new Button(centerDisplayTexRegionDrawable);
centerDisplay.setSize(screenHeight / 2, screenHeight / 2);
centerDisplay.setPosition(screenWidth / 2 - (screenHeight / 2) / 2, screenHeight / 2 - (screenHeight / 2) / 2);
stage.addActor(centerDisplay);
// red buttons
Texture redButtonTexture = new Texture(Gdx.files.internal("redright.png"));
TextureRegion redButtonTextureRegion = new TextureRegion(redButtonTexture);
TextureRegionDrawable redButtonTexRegionDrawable = new TextureRegionDrawable(redButtonTextureRegion);
Button buttonLRed = new Button(redButtonTexRegionDrawable);
buttonLRed.setSize(buttonWidth, buttonHeight);
buttonLRed.getStyle().up = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("redleft.png"))));
//buttonLRed.getStyle().down = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("redleft.png"))));
buttonLRed.setPosition(col1, redHeight);
Button buttonRRed = new Button(redButtonTexRegionDrawable);
buttonRRed.setSize(buttonWidth, buttonHeight);
buttonRRed.getStyle().up = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("redright.png"))));
//buttonRRed.getStyle().down = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("redright.png"))));
buttonRRed.setPosition(col2, redHeight);
buttonLRed.addListener(new InputListener() {@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
leftPressedValue = 1;
}@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
leftPressedValue = 1;
return true;
}
});
buttonRRed.addListener(new InputListener() {@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
rightPressedValue = 1;
}@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
rightPressedValue = 1;
return true;
}
});
stage.addActor(buttonLRed);
stage.addActor(buttonRRed); //Add the button to the stage to perform rendering and take input.
// blue buttons
Texture blueButtonTexture = new Texture(Gdx.files.internal("blue.png"));
TextureRegion blueButtonTextureRegion = new TextureRegion(blueButtonTexture);
TextureRegionDrawable blueButtonTexRegionDrawable = new TextureRegionDrawable(blueButtonTextureRegion);
Button buttonLBlue = new Button(blueButtonTexRegionDrawable);
buttonLBlue.setSize(buttonWidth, buttonHeight);
buttonLBlue.getStyle().up = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("blueleft.png"))));
//buttonLBlue.getStyle().down = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("blueleft.png"))));
buttonLBlue.setPosition(col1, blueHeight);
Button buttonRBlue = new Button(blueButtonTexRegionDrawable);
buttonRBlue.setSize(buttonWidth, buttonHeight);
buttonRBlue.getStyle().up = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("blueright.png"))));
//buttonRBlue.getStyle().down = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("right.png"))));
buttonRBlue.setPosition(col2, blueHeight);
buttonLBlue.addListener(new InputListener() {@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
leftPressedValue = 2;
}@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
leftPressedValue = 2;
return true;
}
});
buttonRBlue.addListener(new InputListener() {@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
rightPressedValue = 2;
}@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
rightPressedValue = 2;
return true;
}
});
stage.addActor(buttonLBlue);
stage.addActor(buttonRBlue); //Add the button to the stage to perform rendering and take input.
// yellow buttons
Texture yellowButtonTexture = new Texture(Gdx.files.internal("yellow.png"));
TextureRegion yellowButtonTextureRegion = new TextureRegion(yellowButtonTexture);
TextureRegionDrawable yellowButtonTexRegionDrawable = new TextureRegionDrawable(yellowButtonTextureRegion);
Button buttonLYellow = new Button(yellowButtonTexRegionDrawable);
buttonLYellow.setSize(buttonWidth, buttonHeight);
buttonLYellow.getStyle().up = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("yellowleft.png"))));
//buttonLYellow.getStyle().down = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("yellowleft.png"))));
buttonLYellow.setPosition(col1, yellowHeight);
Button buttonRYellow = new Button(redButtonTexRegionDrawable);
buttonRYellow.setSize(buttonWidth, buttonHeight);
buttonRYellow.getStyle().up = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("yellowright.png"))));
//buttonRYellow.getStyle().down = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("yellowright.png"))));
buttonRYellow.setPosition(col2, yellowHeight);
buttonLYellow.addListener(new InputListener() {@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
leftPressedValue = 4;
}@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
leftPressedValue = 4;
return true;
}
});
buttonRYellow.addListener(new InputListener() {@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
rightPressedValue = 4;
}@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
rightPressedValue = 4;
return true;
}
});
stage.addActor(buttonLYellow);
stage.addActor(buttonRYellow); //Add the button to the stage to perform rendering and take input.
Gdx.input.setInputProcessor(stage);
//Start taking input from the ui
camera = new OrthographicCamera();
camera.setToOrtho(false, stage.getWidth(), stage.getHeight());
ColorDash.lastRandomNumber = randomNum;
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
game.batch.begin();
game.batch.draw(backgroundTexture, 0, 0, 0, 0, Math.round(screenWidth), Math.round(screenHeight));
//game.font.draw(game.batch,toString().valueOf(randomNum),400,350);
//game.font.draw(game.batch,toString().valueOf(ColorDash.lastRandomNumber),400,400);
game.batch.end();
// tell the camera to update its matrices.
camera.update();
float deltaTime = Gdx.graphics.getDeltaTime();
time += deltaTime;
double timeSpeeedMultiplier = ColorDash.counter * 0.005;
// tell the SpriteBatch to render in the
// coordinate system specified by the camera.
game.batch.setProjectionMatrix(camera.combined);
game.batch.begin();
stage.act(Gdx.graphics.getDeltaTime()); //Perform ui logic
stage.draw(); //Draw the ui
game.batch.end();
measuredValue = leftPressedValue + rightPressedValue;
if (measuredValue > 20) {
measuredValue = 0;
}
if (time > (2 - 2 * timeSpeeedMultiplier)) {
game.setScreen(new GameOverScreen(game));
ColorDash.scoreFinal = ColorDash.counter;
}
if (Gdx.input.isTouched()) {
if (leftPressedValue > 0 && rightPressedValue > 0 && measuredValue != neededValue) {
game.setScreen(new GameOverScreen(game));
ColorDash.scoreFinal = ColorDash.counter;
}
if ((leftPressedValue > 0 && rightPressedValue > 0 && measuredValue == neededValue)) {
ColorDash.counter++;
if (ColorDash.counter == 15) {
game.setScreen(new GameScreenLevel2(game));
} else {
game.setScreen(new GameScreen(game));
}
leftPressedValue = 0;
rightPressedValue = 0;
measuredValue = 0;
ColorDash.scoreFinal = +((2 - 2 * timeSpeeedMultiplier) - deltaTime);
}
} else {
leftPressedValue = 0;
rightPressedValue = 0;
measuredValue = 0;
}
}
@Override
public void resize(int width, int height) {}
@Override
public void show() {
// start the playback of the background music
// when the screen is shown
}
@Override
public void hide() {}
@Override
public void pause() {}
@Override
public void resume() {}
@Override
public void dispose() {
backgroundTexture.dispose();
Gdx.input.setInputProcessor(null);
}
}
最佳答案
在构造函数或 create()
中初始化的每个 Disposable 对象都必须在对此屏幕的引用丢失之前被释放。您可以将所有 dispose()
调用放入屏幕的 dispose()
方法中,并从其 调用屏幕的
方法,因为您没有重用屏幕。 dispose()
方法>hide()
纹理、SpriteBatches 和阶段都是一次性的。
但是,丢弃屏幕并加载同一屏幕的另一个实例对您来说是一种浪费,因为它会重新加载所有相同的资源。制作一种重置游戏相关内容状态的方法并使用它而不是创建和设置新屏幕会更有意义。
顺便说一句,stage.act()
在其自己的内部 SpriteBatch 上调用 begin()
和 end()
。即使让它拥有自己的 SpriteBatch 也是浪费的,因此您应该将 esixitng 实例传递到 Stage 构造函数中。
关于java - Android Studio/LibGDX 游戏内存不足,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46324939/
libGDX Array类的javadoc说:一个可调整大小,有序或无序的对象数组。如果是无序的,则此类在删除元素时避免了内存复制(最后一个元素移动到删除的元素的位置)。 去除元素的改进是此类的唯一优
声音API似乎缺少指示声音播放完毕的功能。还有其他方法可以确定声音是否完成了吗? 最佳答案 据我所知,OpenAL和Android的底层声音后端甚至都没有内部跟踪信息,尽管Music API具有的is
我在应用程序启动时执行了此代码 val resolver = InternalFileHandleResolver() asset.setLoader(FreeTypeFontGenerator::c
我想要获取点 1 和点 2 之间相对于中心点的角度。如何使用 Vector2 执行此操作? Vector2 center = new Vector2((float)Gdx.graphics.getWi
我正在尝试渲染平滑的可缩放位图字体。检查后question answers之一提到使用距离场字体。 我正在按照 LibGDX wiki article 中提到的方式进行操作关于距离场字体。但是我无法让
我已经寻找答案大约 2 个小时了,但还没有找到我想要的答案。我的问题是,是否可以以及如何绘制圆形纹理,以便在圆形之外,纹理将是透明的,这甚至可能吗? 提前致谢!到目前为止,该网站提供了很大的帮助! 最
我用 libgdx 开始了一个项目,我有一个 GameStateManager 用于我的 GameStates 菜单和播放。 如果我运行该项目,它会显示菜单,然后我可以单击一个按钮来打开 Play G
我正在使用 libgdx。我需要缩放和定位文本。假设我想绘制 30 像素高的 X,并且我希望它位于屏幕中间。我想在不同的位置和不同的比例画更多的人。 有什么办法可以实现吗?我在任何地方都找不到解决方案
我正在 Libgdx 中制作纹理打包机,其中如果我使用打包机 2,048 * 2,048 的大小,只制作一个大小为 3.14 Mb 的打包机图像,如果我使用打包机的大小 1,024 * 1,024 那
我在每一帧中都进行了大量的三角函数计算。 Java 的 Math 函数是否比 Libgdx 的 MathUtils 更快? 或者我可以使用任何其他比这两个都更快的库吗? 最佳答案 com.badlog
你好,对于 LibGDX 来说有点新,目前在 pc 上的全屏模式存在问题,我想要做的是每当有人按下一个键时将我的游戏设置为全屏,而当我在 main 方法中输入一些东西时,这不会做任何事情桌面启动器.j
几年前我开始开发我的游戏,中间有很大的停顿。那时没有 gradle,只有简单的 java 安装应用程序。如何找到所使用的 LibGDX 版本? 最佳答案 干得好: Gdx.app.log("Gdx v
我正在尝试使用 libGDX 实现一个简单的动画,但目前我遇到了一件事。假设我有一堆 Sprite 需要一些时间才能完成。例如,大约 30 个 Sprite 像这个链接:https://github.
如何在游戏过程中更改窗口标题?我找不到任何方法。例如,我想在标题栏中显示我得了多少分。 最佳答案 尝试这个 : Gdx.graphics.setTitle(""+yourScore); 祝你好运 !
我正在使用 LibGdx 中的声音接口(interface)来播放 mp3 音频文件。 And when choose to loop playing the sound more than one
所以我在 LibGDX 中制作游戏,我使用 AssetManager 加载我的所有 Assets 。 我只是不确定哪种是正确的使用方法。 目前我正在第一个屏幕之前加载所有游戏 Assets 。 然后我
我一直在寻找来自 libgdx SVN 的 skinpacker,但没有成功。 然后我知道它不再存在。所以,问题是如何创建一个 skin.json 文件以在我的 libgdx 项目中使用。你知道有什么
我正在制作连点类型的游戏,我必须在屏幕上触摸的位置画一条线,所以我使用矢量来存储我触摸的各个点。 我用过 if(Gdx.input.isTouched()) { touchpos.set(Gd
我查看了测试项目中的默认皮肤文件。 我不明白为什么 uiskin.png 里面有字体图片。 uiskin.atlas 文件还包含拆分字段,我不明白为什么需要它以及如何使用它。 我在哪里可以找到所有事情
我到处搜索,但没有引用。 我想用this着色器从 shadertoy 到我的 libgdx 项目,所以我尝试首先从以下位置导入简单着色器:https://www.shadertoy.com/view/
我是一名优秀的程序员,十分优秀!