我坚持运行此代码的想法:
package com.mygdx.Papermadness;
import java.util.ArrayList;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
public class Papermadness extends InputAdapter implements ApplicationListener {
SpriteBatch huisBatch;
Texture huisTexture;
SpriteBatch batch;
Sprite sprite;
SpriteBatch spriteBatch;
Sprite huisSprite;
Texture spriteTexture;
float scrollTimer = 0.0f;
float huisVelocity = 200f;
float huisY = 1900;
float huisX = 0;
float huisY1 = 1900;
float huisX1 = 903;
private Sprite huis;
private ArrayList<Sprite> huisArray = new ArrayList<Sprite>();
Rectangle bounds;
Player player;
Paper paper;
ShapeRenderer sr;
@Override
public void create() {
player = new Player(new Vector2(50, 100), new Vector2(100, 100));
paper = new Paper(new Vector2(Gdx.input.getX(),Gdx.graphics.getHeight()-Gdx.input.getY()), new Vector2(50, 50));
sr = new ShapeRenderer();
spriteBatch = new SpriteBatch();
huisBatch = new SpriteBatch();
huisTexture = new Texture("huis.png");
huisSprite = new Sprite(huisTexture);
spriteTexture = new Texture("b2.png");
spriteTexture.setWrap(TextureWrap.Repeat, TextureWrap.Repeat);
sprite = new Sprite(spriteTexture);
sprite.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
batch = new SpriteBatch();
}
@Override
public void render() {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
float delta = Gdx.graphics.getDeltaTime();
scrollTimer += Gdx.graphics.getDeltaTime();
if (scrollTimer > 2f)
scrollTimer = 0.0f;
sprite.setV(scrollTimer + 2);
sprite.setV2(scrollTimer);
player.update();
paper.update();
spriteBatch.begin();
sprite.draw(spriteBatch);
spriteBatch.end();
for (int i = 0; i < huisArray.size(); i++) {
huisArray.get(i).setY(huisArray.get(i).getY() - huisVelocity * delta);
if (huisArray.get(i).getY() <= 200) {
huisArray.remove(huisArray.get(i));
}
}
if (huisArray.get(0).getY() < 1200) {
addNewHuis();
}
huisBatch.begin();
for (int i = 0; i < huisArray.size(); i++) {
huisBatch.draw(huisArray.get(i), huisArray.get(i).getX(), huisArray.get(i).getY());
}
huisY -= huisVelocity * delta;
huisY1 -= huisVelocity * delta;
batch.begin();
player.draw(batch);
paper.draw(batch);
batch.end();
sr.begin(ShapeType.Filled);
sr.setColor(Color.BLUE);
sr.setColor(Color.RED);
sr.setColor(Color.YELLOW);
sr.rect(Gdx.input.getX(), Gdx.graphics.getHeight() - Gdx.input.getY(), paper.getSize().x,
paper.getSize().y);
sr.end();
}
private void addNewHuis() {
huis = new Sprite();
huis.setY(1800);
huisArray.add(0, huis);
}
(它的两个房子同时向下移动,huisY是左边的房子,huisY1是右边的房子)
我尝试了if语句,以便if(huisY == 200){运行相同的代码}但它会冻结游戏。任何帮助将不胜感激。
提前致谢!
答案 0 :(得分:1)
别忘了清除屏幕。
@Override
public void render(float delta) {
// Clear the screen buffer
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
// start the drawing batch
huisBatch.begin();
huisBatch.draw(huisSprite, huisX, huisY); // SpriteBatch.draw expects floats not ints
huisBatch.draw(huisSprite, huisX1, huisY1); // for the x and y positions
// end the drawing batch
huisBatch.end();
// update postitions ready for next render pass
huisY -= 8f;
huisY1 -= 8f;
// If the lowest point is reached then start from the top again
if (huisY <= 200) {
huisY = 1800;
huisY1 = 1800;
}
}
如果您希望房屋以恒定速度下降,则使用delta值,该值是自上次渲染过程以来经过的时间(以秒为单位)。如果您使用的渲染循环尚未提供增量,您也可以通过调用以下方法获取此值:
float delta = Gdx.graphics.getDeltaTime();
例如:
private float huisVelocity = ...your desired velocity goes here... ;
@Override
public void render() {
// Clear the screen buffer
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
float delta = Gdx.graphics.getDeltaTime();
// start the drawing batch
huisBatch.begin();
huisBatch.draw(huisSprite, huisX, huisY); // SpriteBatch.draw expects floats not ints
huisBatch.draw(huisSprite, huisX1, huisY1); // for the x and y positions
// end the drawing batch
huisBatch.end();
// update postitions using velocity and delta
huisY -= huisVelocity * delta;
huisY1 -= huisVelocity * delta;
// If the lowest point is reached then start from the top again
if (huisY <= 200) {
huisY = 1800;
huisY1 = 1800;
}
}
为了回应评论中的其他信息,下面的代码显示了如何创建精灵数组。当阵列中的每个huis达到下限时,它将从阵列中移除,当顶部huis上方有足够的空间时,会在阵列顶部添加一个新的huis。
private Sprite huis;
private List<Sprite> huisArray = new ArrayList<Sprite>();
private float huisVelocity = ...your desired velocity goes here... ;
...
...
// in a suitable place in your code, add your first huis to the Array
addNewHuis();
...
private void addNewHuis() {
// create your new huis sprite with the relevant settings
huis = new Sprite();
huis.setY(1800);
...
// Insert the new sprite at the start of the array
huisArray.add(0, huis);
}
@Override
public void render() {
// Clear the screen buffer
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
float delta = Gdx.graphics.getDeltaTime();
// update postitions using velocity and delta
for (int i = 0; i < huisArray.size(); i++) {
huisArray.get(i).setY(huisArray.get(i)getY() - huisVelocity * delta;
// remove the huis from the array if it's reached the bottom limit
if (huisArray.get(i).getY() <= 200) {
huisArray.remove(huisArray.get(i));
}
}
// If the huis at the start of the array has dropped below some height,
// add a new huis above it at the top of the screen
if (huisArray.get(0).getY() < 1200) {
addNewHuis();
}
// start the drawing batch
huisBatch.begin();
for (int i = 0; i < huisArray.size(); i++) {
huisBatch.draw(huisArray.get(i), huisArray.get(i).getX(), huisArray.get(i).getY());
}
// end the drawing batch
huisBatch.end();
}