- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
您好,我正在创建一个基本游戏并尝试用我的代码实现 MVC 模式。我目前有:型号游戏对象、云、船水等。
世界这将所有模型联系在一起并定义对象
世界渲染器这会将所有纹理应用于对象
游戏画面这处理用户输入、触摸或键盘。
我的问题是如何以及在哪里实现游戏状态?我假设它会进入 GameScreen,但我已经实现了状态的基本布局,READY,RUNNING,PAUSED,NEXT_LVL,GAME_OVER,
但是我发现当出现“READY”状态时,游戏正在后台运行?
这是在世界渲染器中定义的,即水等级在背景中的屏幕上上升,但我希望这种情况仅在游戏状态切换到运行时发生。
当 Gamstate 切换到运行状态时,如何让 Gamestate 仅初始化 WorldRenderer。我已将语句 renderer.render();
应用到 RUNNING 语句中,但它仍然在 READY 状态下运行。
对此的任何帮助将不胜感激。
干杯
丹尼尔。
我正在附加 WorldRenderer、World 和 GameScreen 类
GameScreen Class
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
public class GameScreen implements Screen {
enum State{
READY,RUNNING,PAUSED,LEVEL_END,GAME_OVER
}
MainThing game;
OrthographicCamera cam;
SpriteBatch batch;
Texture pauseMenu;
TextureRegion pauseMenuR;
Texture pauseBtn;
Texture ready;
TextureRegion readyR;
private World world;
private WorldRenderer renderer;
public static State state = State.READY;
public GameScreen(MainThing game){
this.game = game;
cam = new OrthographicCamera(800,480);
cam.position.set(800/2, 480/2, 0);
batch = new SpriteBatch();
state = State.READY;
loadingTexture();
}
private void loadingTexture() {
pauseBtn = Assets.manager.get("pause.png", Texture.class);
pauseMenu = Assets.manager.get("resume-quit.png", Texture.class);
pauseMenu.setFilter(TextureFilter.Linear, TextureFilter.Linear);
pauseMenuR = new TextureRegion(pauseMenu,0,0,375,256);
ready = Assets.manager.get("ready.png",Texture.class);
readyR = new TextureRegion(ready,0,0,256,105);
}
public static void setState(State newState){
state = newState;
}
private void update(float delta) {
switch (state) {
case READY:
updateReady();
break;
case RUNNING:
updateRunning(delta);
break;
case PAUSED:
updatePaused();
break;
case LEVEL_END:
updateLevelEnd();
break;
case GAME_OVER:
updateGameOver();
break;
}
}
private void updateReady() {
if (Gdx.input.justTouched()) {
state = State.RUNNING;
}
}
private void updateRunning(float delta) {
if(Gdx.input.isTouched()) {
Vector3 touchPos = new Vector3();
touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
WorldRenderer.cam.unproject(touchPos);
Boat.position.x = touchPos.x - 1f / 2;
}
if(Gdx.input.isKeyPressed(Keys.LEFT)) {
Boat.velocity.x = -Boat.SPEED * delta;
}
else if(Gdx.input.isKeyPressed(Keys.RIGHT)) {
Boat.velocity.x = Boat.SPEED * delta;
}
else if(!Gdx.input.isKeyPressed(Keys.LEFT) && !(Gdx.input.isKeyPressed(Keys.RIGHT)) || (Gdx.input.isKeyPressed(Keys.LEFT) && (Gdx.input.isKeyPressed(Keys.RIGHT)))) {
Boat.velocity.x = 0;
}
Boat.update(delta);
if(Boat.position.x < 0) Boat.position.x = 0;
if(Boat.position.x > WorldRenderer.cam.viewportWidth - 1f) Boat.position.x = WorldRenderer.cam.viewportWidth - 1f;
}
private void updatePaused() {
// TODO Auto-generated method stub
}
private void updateLevelEnd() {
// TODO Auto-generated method stub
}
private void updateGameOver() {
// TODO Auto-generated method stub
}
private void draw(float deltaTime) {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
renderer.render(deltaTime);
cam.update();
batch.setProjectionMatrix(cam.combined);
batch.begin();
switch (state) {
case READY:
presentReady();
break;
case RUNNING:
presentRunning(deltaTime);
break;
case PAUSED:
presentPaused();
break;
case LEVEL_END:
presentLevelEnd();
break;
case GAME_OVER:
presentGameOver();
break;
}
batch.end();
}
private void presentReady() {
batch.draw(readyR, cam.viewportWidth /2 - 375 /2 , cam.viewportHeight /2 - 256 /2,375,256);
}
private void presentRunning(float delta) {
// TODO Auto-generated method stub
}
private void presentPaused() {
// TODO Auto-generated method stub
}
private void presentLevelEnd() {
// TODO Auto-generated method stub
}
private void presentGameOver() {
// TODO Auto-generated method stub
}
@Override
public void render(float delta) {
update(delta);
draw(delta);
}
@Override
public void resize(int width, int height) {
renderer.setSize(width,height);
}
@Override
public void show() {
world = new World();
renderer = new WorldRenderer(world, false);
}
@Override
public void hide() {
}
@Override
public void pause() {
dispose();
}
@Override
public void resume() {
}
@Override
public void dispose() {
Assets.unload();
Boat.dispose();
Cloud.dispose();
WorldRenderer.dispose();
}
}
世界级
package com.inspirednotion.thing;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.TimeUtils;
public class World {
Boat boat;
Water water;
Cloud cloud;
long lastDropTime;
Array<Rain> raindrops = new Array<Rain>();
public Array<Rain> getRain(){
return raindrops;
}
Boat getBoat(){
return boat;
}
public Water getWater(){
return water;
}
public Cloud getCloud(){
return cloud;
}
public World(){
createWorld();
spawnRaindrop();
lastDropTime = TimeUtils.nanoTime();
}
private void createWorld() {
boat = new Boat(new Vector2(WorldRenderer.CAMERA_WIDTH / 2 - Boat.BOAT_WIDTH / 2, Water.position.y +11.3f ));
cloud = new Cloud(new Vector2(WorldRenderer.CAMERA_WIDTH / 2 - Cloud.CLOUD_WIDTH /2, 8));
water = new Water(new Vector2(WorldRenderer.CAMERA_WIDTH /2 - Water.WATER_WIDTH/2, -11.3f));
}
void spawnRaindrop(){
raindrops.add( new Rain(new Vector2(MathUtils.random(cloud.position.x, cloud.position.x + Cloud.CLOUD_WIDTH ),cloud.position.y)));
lastDropTime = TimeUtils.nanoTime();
}
public static void dispose() {
}
}
世界渲染器类
public class WorldRenderer {
private static World world;
public static OrthographicCamera cam;
ShapeRenderer debugRenderer = new ShapeRenderer();
static final float CAMERA_WIDTH = 20f;
static final float CAMERA_HEIGHT = 12f;
static Texture cloudTexture;
private TextureRegion cR;
static Texture boatAniTex;
TextureRegion[] boatFrames;
Animation boatAnimation;
TextureRegion boatCurrentFrame;
static TextureRegion currentFrameBoat;
TextureRegion[][] tmpBoat;
static Texture cloudBg;
private TextureRegion cloudBgR;
static Texture rainDrop;
long lastDropTime;
float lastDropT;
private static Texture water;
TextureRegion[] waveFrames;
Animation waveAnimation;
TextureRegion currentFrame;
TextureRegion currentFrameWav;
TextureRegion[][] tmp;
float stateTime = 0f;
private static SpriteBatch batch;
static int width;
static int height;
public void setSize(int w, int h){
WorldRenderer.width= w;
WorldRenderer.height = h;
}
public WorldRenderer(World world, boolean debug){
WorldRenderer.world = world;
WorldRenderer.cam = new OrthographicCamera(CAMERA_WIDTH ,CAMERA_HEIGHT);
WorldRenderer.cam.position.set(CAMERA_WIDTH /2f, CAMERA_HEIGHT /2f, 0);
WorldRenderer.cam.update();
batch = new SpriteBatch();
loadTextures();
loadAnimations();
}
public void loadTextures(){
boatAniTex = Assets.manager.get("bin.png", Texture.class);
boatAniTex.setFilter(TextureFilter.Linear, TextureFilter.Linear);
cloudTexture = Assets.manager.get("cloud.png", Texture.class);
cloudTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
cR = new TextureRegion(cloudTexture,0,0,256,140);
cloudBg = Assets.manager.get("cloudbg.png", Texture.class);
cloudBg.setFilter(TextureFilter.Linear, TextureFilter.Linear);
cloudBgR = new TextureRegion(cloudBg,0,0,800,480);
water = Assets.manager.get("wavey.png", Texture.class);
water.setFilter(TextureFilter.Linear, TextureFilter.Linear);
rainDrop = new Texture(Gdx.files.internal("RainDrop_sml.png"));
}
private void loadAnimations() {
water.setFilter(TextureFilter.Linear, TextureFilter.Linear);
tmp = new TextureRegion(water).split(800,480);
waveFrames = new TextureRegion[14];
for (int x = 0, index = 0; x < 7; x++) {
for (int y = 0; y < 2; y++, index++) {
waveFrames[index] = tmp[x][y];
}
}
waveAnimation = new Animation(0.14f, waveFrames);
//************************
boatAniTex.setFilter(TextureFilter.Linear, TextureFilter.Linear);
tmpBoat = new TextureRegion(boatAniTex).split(64,64);
boatFrames = new TextureRegion[7];
for (int x = 0, indexed = 0; x < 1; x++) {
for (int y = 0; y < 7; y++, indexed++) {
boatFrames[indexed] = tmpBoat[x][y];
}
}
boatAnimation = new Animation(0.2f, boatFrames);
}
public void render(float delta){
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(cam.combined);
stateTime += Gdx.graphics.getDeltaTime();
currentFrameWav = waveAnimation.getKeyFrame(stateTime += delta, true);
currentFrameBoat = boatAnimation.getKeyFrame(stateTime += delta, true);
batch.begin();
batch.disableBlending();
drawBackGround();
batch.enableBlending();
drawBoat();
drawWater(delta);
drawCloud();
batch.end();
drawRain();
}
private void drawRain() {
batch.begin();
for( Rain bounds: world.raindrops) {
batch.draw(rainDrop, bounds.position.x , bounds.position.y,.2f,.2f);
}
batch.end();
if(TimeUtils.nanoTime() - world.lastDropTime > 500000000) world.spawnRaindrop();
Iterator<Rain> iter = world.raindrops.iterator();
while(iter.hasNext()) {
Rain raindrop = iter.next();
raindrop.position.y -=2f * Gdx.graphics.getDeltaTime();
if(raindrop.position.y < Water.position.y ) {
System.out.println("water is hit");
iter.remove();
}
}
}
private void drawWater(float delta) {
batch.draw(currentFrameWav, Water.position.x , Water.position.y ,Water.WATER_WIDTH , Water.WATER_HEIGHT );
if (Water.position.y < -4f){
Water.position.y += .1 * Gdx.graphics.getDeltaTime();
Boat.position.y += .1 * Gdx.graphics.getDeltaTime();
}
}
private void drawBackGround() {
batch.draw(cloudBgR, 0,0,CAMERA_WIDTH,CAMERA_HEIGHT);
}
private void drawCloud(){
Cloud cloud = world.getCloud();
batch.draw(cR,cloud.position.x ,cloud.position.y ,Cloud.CLOUD_WIDTH , Cloud.CLOUD_HEIGHT );
}
private void drawBoat(){
world.getBoat();
batch.draw(currentFrameBoat, Boat.position.x , Water.position.y +11.3f,Boat.BOAT_WIDTH , Boat.BOAT_HEIGHT );
}
private void drawDebug(){
debugRenderer.setProjectionMatrix(cam.combined);
debugRenderer.begin(ShapeType.Rectangle);
Cloud cloud = world.getCloud();
Rectangle rect = cloud.bounds;
float x1 = cloud.position.x + rect.x;
float y1 = cloud.position.y + rect.y;
debugRenderer.setColor(new Color(1,0,0,1));
debugRenderer.rect(x1, y1, rect.width, rect.height);
Boat boat = world.getBoat();
Rectangle rec = boat.bounds;
float xx1 = Boat.position.x + rec.x;
float yx1 = Boat.position.y + rec.y;
debugRenderer.setColor(new Color(1,1,0,1));
debugRenderer.rect(xx1, yx1, rec.width, rec.height);
debugRenderer.end();
}
public static void dispose(){
cloudTexture.dispose();
water.dispose();
boatAniTex.dispose();
rainDrop.dispose();
cloudBg.dispose();
batch.dispose();
}
}
请原谅困惑的代码..正在清理和调试问题..
最佳答案
底线:您的 GameScreen
处于 READY
状态,但您的 WorldRenderer
并不知道这一点,并继续其快乐的方式。您需要确保您的 WorldRenderer
具有映射到 GameScreen
状态的状态。
您在 GameScreen.draw()
中对 renderer.render(deltaTime)
的调用是罪魁祸首。无论 GameScreen
中的状态如何,它都会始终更新事物的位置。
假设所需的效果是在后台绘制游戏,但游戏尚未开始 -
WorldRenderer
添加一个状态枚举,该状态枚举至少处于 WAITING
和 PLAYING
状态,并具有此枚举类型的字段。确保在 WorldRenderer
中创建了一个公共(public)方法,允许 GameScreen
设置此值。GameScreen
创建WorldRenderer
时,请确保调用WorldRenderer
状态并将其设置为WAITING
。 GameScreen
转换到播放状态时,调用WorldRenderer
状态并将其设置为PLAYING
。WorldRenderer
中,仔细定义在 WAITING
状态、PLAYING
状态以及这两种状态下需要执行的操作。在这种情况下,您不希望水位上升,因此如果您处于 WAITING
状态,请不要更改水位和船的高度。关于java - 使用 MVC 布局实现游戏状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11766572/
我为 S3 做了一个额外的布局(所有布局的反叛),人们说,使用 layout-sw320dp 对 s3 有好处。一切正常,s3 选择了这个文件夹,布局在 s3 上看起来很棒。 但是当我尝试在 10"平
我是 CSS 的新手,我正在尝试创建一个 3 列布局。也应该有一个居中的页脚。页面的总高度应该填满当前的屏幕。宽度似乎不对。 目前,页脚在尺寸和位置上似乎都没有对齐。 I have attached
已关闭。这个问题是 off-topic 。目前不接受答案。 想要改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 已关闭10 年前。 Improve th
有没有办法确定设备是从右到左的语言(比如阿拉伯语)而不是从左到右的语言(英语)? 需要与旧 API 级别(低至 10)兼容的东西 解决方案 我最终在接受的答案中使用了 xml 方法。更进一步,我还添加
我是 QT 的新手。我试图通过实现下面看到的这个小窗口来理解布局机制。它在作为主窗口的 QWidget 下具有以下元素: 一个延伸到所有客户区域的大型 QWidget。 窗口顶部的两个 QWidget
Accordion 布局是堆叠面板布局,因为此时只有一个面板可见,但我想同时显示两个面板可见,所以我们可以使用 Accordion 面板来做到这一点吗?? 最佳答案 您不能扩展现有的 Accordio
我只是想知道,作为一个假设示例,针对以下场景布局表格的最佳方式是什么: 假设我正在编写一个用于跟踪学生出勤的应用程序。每年年初,我都想添加所有学生(我将手动执行此操作 - 现在,是否应该为这里的每个学
在 CVS 中,我们的项目中有多个目录。 有一个夜间构建,它必须从同一个 CVS 项目的不同目录中提取东西才能构建夜间构建。所以我应该记住这一点,如果我们迁移到 SVN,我必须修改构建脚本以从不同的存
我在 WPF Windows 上有几个列表框,带有 Height="Auto" Width="Auto"在表格上设置 表单大小在不同分辨率下完美匹配,但问题是当我按下最大化按钮时,在表单调整大小时会看
仅供引用,我是 WPF 的新手。 我正在我的 WPF 应用程序中创建一个侧边栏并想要圆角。我学到的不是可以附加到网格的属性。另外,我尝试将文本块放在边框控件中,但我收到的错误消息说“ child 只能
我是CodeIgniter的新手。我想使用包含菜单,页脚等的基本样式创建母版页或布局。我不想在所有页面中编写重复的内容并为所有页面自动加载。例如,我可以在asp.net中创建母版页,或者在asp.ne
我正在使用它来调试应用程序。调试的时候发现底部显示了一个窗口中变量的值,如图- 但是,当我显示表达式时,我得到这样的布局 - 我的问题是,是否可以更改变量窗口的布局也可以在右侧显示值,因为这对我来说很
上面的代码中,放置“as=”footer_links”是什么意思? 最佳答案 as="x" 语法定义模板可用来调用 block 的名称。因此,对于以下内容: 在outer_block.p
我试图编写一个检查注册表值的功能,以查看Windows上的控制台是否启用了颜色。 Computer\HKEY_CURRENT_USER\Console\VirtualTerminalLevel 如果您
我有一个布局,但无法提前定义其所有区域,因为它们是未知的。 稍后创建了 ItemView,我想使用 View 的 ID 作为区域名称在布局中创建一个新区域,这样我就可以说: layout.dynami
我们有一个相当复杂的 gulp 构建过程,涉及多个模块,每个模块都有一个或两个 watch 。我想在一个仪表板中监控这一点,如下所示: 每一列都是一个模块,列内的每一行都是后续的构建步骤。一旦第 1
这就是问题所在,我有一个 MainWindow 类,它在一个设置例程中扩展了 JFrame,我将该类的布局设置为新的 CardLayout()。这一切都工作正常,但是当我从 JFrame 请求布局并将
我正在制作一个简单的迷宫程序,用户可以在其中创建墙壁、路径、起点和终点,单击“解决”,迷宫将被解决。为此,我有一个大小为 640x480 的 java JFrame。在 JFrame 的左侧,我有一个
我正在使用它来调试应用程序。调试的时候发现底部显示了一个窗口中变量的值,如图- 但是,当我显示表达式时,我得到这样的布局 - 我的问题是,是否可以更改变量窗口的布局也可以在右侧显示值,因为这对我来说很
我已经编写了使用 VBox 作为布局的代码。我希望按钮出现在顶行,然后绘制 2 条水平线,在 400x400 场景中应位于 y=200 和 300 处。但输出显示了我给出的不同坐标处的线条。 我知道这
我是一名优秀的程序员,十分优秀!