【发布时间】:2018-06-09 07:13:19
【问题描述】:
运行时错误使我的应用程序崩溃。它在制作矩形数组列表后开始。我该如何解决?
“在附加的堆栈跟踪中获取了资源,但从未释放”
package com.mygdx.game;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Intersector;
import com.badlogic.gdx.math.Rectangle;
import org.w3c.dom.css.Rect;
import java.util.ArrayList;
import java.util.Random;
public class coingame extends ApplicationAdapter {
SpriteBatch batch;
int i=0;
int bomb2=0;
ArrayList<Integer> bombposition2=new ArrayList<Integer>();
Texture bomb;
int d=0;
Rectangle rectangle3;
int coin2=0;
int k=0;
int n=0;
Texture coin;
ArrayList<Integer> bombposition=new ArrayList<Integer>();
ArrayList<Rectangle> rectangle=new ArrayList<Rectangle>();
float velocity=0;
float manposition=0;
ArrayList<Integer> coinposition=new ArrayList<Integer>();
ArrayList<Rectangle> rectangle2=new ArrayList<Rectangle>();
ArrayList<Integer> coinposition2=new ArrayList<Integer>();
float gravity=0.2f;
int c=0;
Texture a;
Texture[] b;
@Override
public void create () {
batch = new SpriteBatch();
bomb=new Texture("bomb.png");
rectangle3=new Rectangle();
a=new Texture("bg.png");
coin =new Texture("coin.png");
b=new Texture[4];
b[0]=new Texture("frame-1.png");
b[1]=new Texture("frame-2.png");
b[2]=new Texture("frame-3.png");
b[3]=new Texture("frame-4.png");
manposition=Gdx.graphics.getHeight();
}
@Override
public void render () {
batch.begin();
batch.draw(a, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
if(coin2<100)
{
coin2=coin2+1;
}
else
{
coin2=0;
n=new Random().nextInt(Gdx.graphics.getHeight());
coinposition.add(Gdx.graphics.getWidth());
coinposition2.add(k);
}
if(bomb2<200)
{
bomb2=bomb2+1;
}
else
{
bomb2=0;
k=new Random().nextInt(Gdx.graphics.getHeight());
bombposition.add(Gdx.graphics.getWidth());
bombposition2.add(k);
}
rectangle.clear();
for(i=0;i<coinposition.size();i++)
{
batch.draw(coin,coinposition.get(i),coinposition2.get(i));
coinposition.set(i,coinposition.get(i)-4);
rectangle.add(new Rectangle(coinposition.get(i),coinposition2.get(i),coin.getWidth(),coin.getHeight()));
}
rectangle2.clear();
for(i=0;i<bombposition.size();i++)
{
batch.draw(bomb,bombposition.get(i),bombposition2.get(i));
bombposition.set(i,bombposition.get(i)-8);
//rectangle2.add(new Rectangle(bombposition.get(i),bombposition2.get(i),bomb.getHeight(),bomb.getWidth()));
}
if(Gdx.input.justTouched()==true)
{
velocity=-10;
gravity=0;
}
if(d<8)
{
d=d+1;
}
else {
d=0;
if (c < 3) {
c = c + 1;
} else {
c = 0;
}
}
velocity=velocity+gravity;
gravity=gravity+0.2f;
manposition=manposition-velocity;
if(manposition<0||manposition==0)
{
manposition=0;
}
batch.draw(b[c],Gdx.graphics.getWidth()/2-b[c].getWidth()/2,manposition);
//rectangle3=new Rectangle(Gdx.graphics.getWidth()/2-b[c].getWidth()/2,manposition,b[c].getWidth(),b[c].getHeight());
/*batch.draw(b[1],Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/2);
batch.draw(b[2],Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/2);
batch.draw(b[3],Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/2);*/
for(i=0;i<rectangle.size();i++)
{
if(Intersector.overlaps(rectangle3,rectangle2.get(i)))
{
Gdx.app.log("this is a","this is a");
}
}
batch.end();
}
@Override
public void dispose () {
batch.dispose();
}
}
【问题讨论】: