Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在渲染中重复运行代码?(libgdx)_Java_Android_Libgdx - Fatal编程技术网

Java 如何在渲染中重复运行代码?(libgdx)

Java 如何在渲染中重复运行代码?(libgdx),java,android,libgdx,Java,Android,Libgdx,我一直在想运行这个代码: 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

我一直在想运行这个代码:

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);
}
package com.mygdx.Papermadness;
导入java.util.ArrayList;
导入com.badlogic.gdx.ApplicationListener;
导入com.badlogic.gdx.gdx;
导入com.badlogic.gdx.InputAdapter;
导入com.badlogic.gdx.graphics.Color;
导入com.badlogic.gdx.graphics.GL20;
导入com.badlogic.gdx.graphics.Texture;
导入com.badlogic.gdx.graphics.g2d.Sprite;
导入com.badlogic.gdx.graphics.g2d.SpriteBatch;
导入com.badlogic.gdx.graphics.glutils.shaperender;
导入com.badlogic.gdx.graphics.glutils.shaperender.ShapeType;
导入com.badlogic.gdx.math.Rectangle;
导入com.badlogic.gdx.math.Vector2;
公共类Papermadness扩展InputAdapter实现ApplicationListener{
SpriteBatch huisBatch;
纹理结构;
喷雾批;
雪碧雪碧;
SpriteBatch SpriteBatch;
雪碧乌斯普林特;
纹理、纹理;
浮动滚动定时器=0.0f;
浮动huisVelocity=200f;
浮点数=1900;
浮点数huisX=0;
浮点数1=1900;
浮动huisX1=903;
私人精灵回族;
private ArrayList huisArray=new ArrayList();
矩形边界;
玩家;
纸张;
Shaperender sr;
@凌驾
公共void create(){
玩家=新玩家(新向量2(50100),新向量2(100100));
纸张=新纸张(新矢量2(Gdx.input.getX(),Gdx.graphics.getHeight()-Gdx.input.getY()),新矢量2(50,50));
sr=新的ShaperEnder();
spriteBatch=新spriteBatch();
huisBatch=新的SpriteBatch();
huisTexture=新纹理(“huis.png”);
huisSprite=新雪碧(huisTexture);
spriteTexture=新纹理(“b2.png”);
spriteTexture.setWrap(TextureWrap.Repeat,TextureWrap.Repeat);
精灵=新精灵(精灵纹理);
setSize(Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
批次=新的SpriteBatch();
}
@凌驾
公共无效呈现(){
glClearColor(0,0,0,1);
Gdx.gl.glClear(GL20.gl\u颜色\u缓冲\u位);
float delta=Gdx.graphics.getDeltaTime();
scrollTimer+=Gdx.graphics.getDeltaTime();
如果(滚动计时器>2f)
滚动定时器=0.0f;
sprite.setV(滚动定时器+2);
sprite.setV2(滚动计时器);
player.update();
paper.update();
spriteBatch.begin();
雪碧画(雪碧画);
spriteBatch.end();
对于(int i=0;i如果(huisArray.get(i).getY()不要忘记清除屏幕

@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;
    }
}
例如:

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;
    }
}
private float huisVelocity=…您所需的速度在这里;
@凌驾
公共无效呈现(){
//清除屏幕缓冲区
glClearColor(0,0,0,1);
Gdx.gl.glClear(GL20.gl\u颜色\u缓冲\u位);
float delta=Gdx.graphics.getDeltaTime();
//启动绘图批处理
huisBatch.begin();
huisBatch.draw(huisSprite、huisX、huisY);//SpriteBatch.draw需要浮点而不是整数
huisBatch.draw(huisSprite、huisX1、huisY1);//用于x和y位置
//结束绘图批处理
huisBatch.end();
//使用速度和增量更新位置
huisY-=huisVelocity*三角洲;
huisY1-=huisVelocity*三角洲;
//如果达到最低点,则再次从顶部开始

如果(huisY,谢谢,但我想你误解了我的意思。我实际上是不想干扰房子的电流,但在1秒钟后,另一栋房子被画出来,并跟随前一栋房子直到它消失(在屏幕下)。我在添加这一行时出现此错误:“private List huisArray=new ArrayList;”这一行有多个标记-令牌上的语法错误,构造错误-观察点:Papermadness[access and modification]-huisArrayHi,我通过添加“()'在结尾处,并使'private List'='private ArrayList'。这是否正确?现在我收到另一条错误消息,在添加'if(huisArray.get(0)<600){addNewHuis();}时说:'运算符<未定义参数类型Sprite,int''是的,很抱歉有几处输入错误。因为我没有你的完整代码,我只是在没有测试的情况下键入了它。你是对的,数组列表的构造函数应该在它“…=new ArrayList();”之后加上括号。另外,在if语句中,我们要计算y位置,所以它应该是'if(huisArray.get(0).getY()<600)“。我已在答案中进行了这些编辑。感谢您发布代码。当代码崩溃时,您的日志猫指示什么?我注意到您有huisBatch.begin(),但没有huisBatch.end()。正如我前面提到的,SpriteBatch.begin()必须始终与SpriteBatch.end()配对。当您调用SpriteBatch.begin()时,则收集以下所有SpriteBatch绘图调用而不提交到GPU。当您调用SpriteBatch.end()时,以前收集的所有SpriteBatch绘图调用都会一次性提交到GPU。如果您不调用SpriteBatch.end(),这些SpriteBatch绘图调用将永远不会提交到GPU。更糟糕的是,SpriteBatch.begin()将在每次渲染过程中重复调用,而不调用end()。因此,请您编辑代码以显示它应该设置在哪里?我现在有点困惑哈哈
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;
    }
}
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();

}