Android 应用程序运行时粒子爆炸长度(时间)减少
编辑:我能够通过将爆炸存储到ArrayList中来解决这个问题,但是我不确定这是否是一个好的做法。让arraylist变得越来越大可以吗?游戏将在arraylist超过200之前结束,但我只想练习最好的风格 我目前正在为android制作一个非常非常简单的游戏(有史以来的第一个项目),在这个游戏中,你有一堆圆圈在屏幕上移动,当你点击它们时,它们会“爆炸”。我实现这一点的方式是,一旦它记录到你在圆坐标内点击,粒子就会在你点击的位置生成,并在随机方向上“爆炸” 我遇到的问题是,粒子的寿命随着应用程序的运行而缩短,尽管变量是一个常量。最初的几次轻敲是完美的,它们持续了正确的时间。当你继续玩的时候,爆炸变得越来越短,直到它们只是闪光。我不确定这是什么原因造成的 感谢您的帮助Android 应用程序运行时粒子爆炸长度(时间)减少,android,Android,编辑:我能够通过将爆炸存储到ArrayList中来解决这个问题,但是我不确定这是否是一个好的做法。让arraylist变得越来越大可以吗?游戏将在arraylist超过200之前结束,但我只想练习最好的风格 我目前正在为android制作一个非常非常简单的游戏(有史以来的第一个项目),在这个游戏中,你有一堆圆圈在屏幕上移动,当你点击它们时,它们会“爆炸”。我实现这一点的方式是,一旦它记录到你在圆坐标内点击,粒子就会在你点击的位置生成,并在随机方向上“爆炸” 我遇到的问题是,粒子的寿命随着应用程序
public class Explosion {
private static final String TAG = Explosion.class.getSimpleName();
public static final int STATE_ALIVE = 0; //at least 1 particle is alive
public static final int STATE_DEAD = 1; //all particles are dead
private Particle[] particles; //particles in the explosion
private int x, y; //the explosion's origin
private int state; //whether it's still active or not
public Explosion(int particleNr, int x, int y) {
Log.d(TAG, "Explosion created at " + x + "," + y);
this.state = STATE_ALIVE;
this.particles = new Particle[particleNr];
for (int i = 0; i < this.particles.length; i++) {
Particle p = new Particle(x, y);
this.particles[i] = p;
}
}
public Particle[] getParticles() {
return particles;
}
public void setParticles(Particle[] particles) {
this.particles = particles;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public void setState(int state) {
this.state = state;
}
public boolean isAlive() {
return this.state == STATE_ALIVE;
}
public boolean isDead() {
return this.state == STATE_DEAD;
}
public void update(Rect container) {
if (this.state != STATE_DEAD) {
boolean isDead = true;
for (int i = 0; i < this.particles.length; i++) {
if (this.particles[i].isAlive()) {
this.particles[i].update(container);
isDead = false;
}
}
if (isDead)
this.state = STATE_DEAD;
}
}
public void draw(Canvas canvas) {
for(int i = 0; i < this.particles.length; i++) {
if (this.particles[i].isAlive()) {
this.particles[i].draw(canvas);
}
}
}
公共类爆炸{
私有静态最终字符串标记=Explosion.class.getSimpleName();
public static final int STATE_ALIVE=0;//至少有1个粒子处于活动状态
public static final int STATE_DEAD=1;//所有粒子都是死的
私有粒子[]粒子;//爆炸中的粒子
private int x,y;//爆炸的起源
private int state;//是否仍处于活动状态
公共爆炸(内部粒子、内部x、内部y){
Log.d(标签“在“+x+”、“+y”处产生的爆炸);
this.state=state\u ALIVE;
this.particles=新粒子[particleNr];
对于(int i=0;i
}
^爆炸级
public class Particle {
public static final int STATE_ALIVE = 0;
public static final int STATE_DEAD = 1;
public static final int DEFAULT_LIFETIME = 500000;
public static final int MAX_DIMENSION = 4;
public static final int MAX_SPEED = 3;
private int mState;
private float mWidth;
private float mHeight;
private float mX,mY;
private double mXVel, mYVel;
private int mAge;
private int mLifetime;
private int mColor;
private Paint mPaint;
public Particle(int x, int y) {
mX = x;
mY = y;
mState = Particle.STATE_ALIVE;
mWidth = rndInt(1,MAX_DIMENSION);
mHeight = mWidth;
mLifetime = DEFAULT_LIFETIME;
mAge = 0;
mXVel = (rndDbl(0, MAX_SPEED * 2) - MAX_SPEED);
mYVel = (rndDbl(0, MAX_SPEED * 2) - MAX_SPEED);
//smoothing out the diagonal speed
if (mXVel * mXVel + mYVel * mYVel > MAX_SPEED * MAX_SPEED) {
mXVel *= .7;
mYVel *= .7;
}
mColor = Color.argb(255, 255, 255, 255);
mPaint = new Paint(mColor);
}
public int getState() {
return mState;
}
public void setState(int state) {
mState = state;
}
public float getWidth() {
return mWidth;
}
public void setWidth(float width) {
mWidth = width;
}
public float getHeight() {
return mHeight;
}
public void setHeight(float height) {
mHeight = height;
}
public float getX() {
return mX;
}
public void setX(float x) {
mX = x;
}
public float getY() {
return mY;
}
public void setY(float y) {
mY = y;
}
public double getXVel() {
return mXVel;
}
public void setXv(double xVel) {
mXVel = xVel;
}
public double getYVel() {
return mYVel;
}
public void setYv(double yVel) {
mYVel = yVel;
}
public int getAge() {
return mAge;
}
public void setAge(int age) {
mAge = age;
}
public int getLifetime() {
return mLifetime;
}
public void setLifetime(int lifetime) {
mLifetime = lifetime;
}
public int getColor() {
return mColor;
}
public void setColor(int color) {
mColor = color;
}
private int rndInt(int min, int max) {
return (int) (min + Math.random() * (max - min + 1));
}
private double rndDbl(double min, double max) {
return min + (max - min) * Math.random();
}
public boolean isAlive() {
return mState == STATE_ALIVE;
}
public boolean isDead() {
return mState == STATE_DEAD;
}
public void update(Rect container) {
// update with collision
if (this.isAlive()) {
if (mX <= container.left || mX >= container.right - mWidth) {
mXVel *= -1;
}
// Bottom is 480 and top is 0 !!!
if (mY <= container.top || mY >= container.bottom - mHeight) {
mYVel *= -1;
}
}
update();
}
public void update() {
if (mState != STATE_DEAD) {
mX += mXVel;
mY += mYVel;
//extract alpha
int a = mColor >>> 24;
//fade by 2
a -= 2;
//if transparency is reached then particle dies
if (a <= 0) {
mState = STATE_DEAD;
} else {
//set new alpha
mColor = (mColor & 0x00ffffff) + (a << 24);
mPaint.setAlpha(a);
mAge++;
}
//particle reached end of its lifetime
if (mAge >= mLifetime) {
mState = STATE_DEAD;
}
}
}
public void draw(Canvas canvas) {
mPaint.setColor(mColor);
canvas.drawRect(mX, mY, mX + mWidth, mY + mHeight, mPaint);
}
公共类粒子{
公共静态最终int状态_ALIVE=0;
公共静态最终int状态_DEAD=1;
公共静态最终int默认_生存期=500000;
公共静态最终int MAX_维度=4;
公共静态最终int最大速度=3;
私人国家;
私人浮动宽度;
私人浮动mhweight;
私人浮动mX,我的;
私人双mXVel,mYVel;
私人智力法师;
私人时间;
私人内特彩色;
私人油漆;
公共粒子(整数x,整数y){
mX=x;
mY=y;
mState=Particle.STATE\u ALIVE;
mWidth=rndInt(1,最大尺寸);
mHeight=mWidth;
mLifetime=默认的生命周期;
mAge=0;
mXVel=(rndDbl(0,最大速度*2)-最大速度);
mYVel=(rndDbl(0,最大速度*2)-最大速度);
//平滑对角线速度
如果(mXVel*mXVel+mYVel*mYVel>MAX_SPEED*MAX_SPEED){
mXVel*=0.7;
mYVel*=0.7;
}
mColor=Color.argb(255、255、255、255);
mPaint=新油漆(mColor);
}
public int getState(){
返回mState;
}
公共无效设置状态(int状态){
mState=状态;
}
公共浮点getWidth(){
返回mWidth;
}
公共空白设置宽度(浮动宽度){
mWidth=宽度;
}
公众浮标高度(){
返回mhweight;
}
公共空间设置高度(浮动高度){
mHeight=高度;
}
公共浮点getX(){
返回mX;
}
公共无效集合x(浮动x){
mX=x;
}
公共浮球{
归还我的;
}
公共无效setY(浮动y){
mY=y;
}
公共双getXVel(){
返回mXVel;
}
公共无效设置XV(双X层){
mXVel=xVel;
}
公共双getYVel(){
返回mYVel;
}
公共无效设置yv(双yVel){
mYVel=yVel;
}
公共整数getAge(){
返回法师;
}
公共无效设置(整数){
法师=年龄;
}
公共int getLifetime(){
返回时间;
}
公共void setLifetime(int lifetime){
mLifetime=寿命;
}
public int getColor(){
返回mColor;
}
公共void setColor(int-color){
mColor=颜色;
}
私有整数rndInt(最小整数,最大整数){
返回值(int)(min+Math.random()*(max-min+1));
}
专用双rndDbl(双最小值,双最大值){
返回min+(max-min)*Math.random();
}
公共生活{
返回mState==STATE\u ALIVE;
}
公共布尔ISDED(){
返回mState==STATE\u DEAD;
}
公共void更新(Rect容器){
//冲突更新
if(this.isAlive()){
if(mX=container.right-mWidth){
mXVel*=-1;
}
//底部为480,顶部为0!!!
如果(mY=容器底部-mHweight){
mYVel*=-1;
}
}
更新();
}
公共无效更新(){
如果(mState!=状态为“死”){
mX+=mXVel;
mY+=mYVel;
//提取α
int a=mColor>>>24;
//衰减2
a-=2;
//如果达到透明度,则粒子消亡
如果(a开始诊断这个问题,我会做下面的事情。基本上,我会验证第1点和第2点,没有什么
public void updatePhysics() {
int pWidth = BitmapFactory.decodeResource(getResources(),R.drawable.bullet).getWidth();
for (int i = 0; i < pummels.size(); i++) {
//checks collision with left side wall
//changes direction if it collides
pummels.get(i).update();
if (pummels.get(i).getSpeed().getxDirection() == Speed.DIRECTION_LEFT && pummels.get(i).getX() - pWidth / 2 <= 0) {
totalHp--;
setHP("HP: " + String.valueOf(totalHp));
pummels.remove(i);
}
if (pummels.size() == 0) {
for (int j = 0; j < 10; j++) {
pummels.add(j, new Pummel(BitmapFactory.decodeResource(getResources(), R.drawable.bullet), 850, (int) (Math.random() * 200) + 80));
}
}
if (explosion != null && explosion.isAlive()) {
explosion.update(getHolder().getSurfaceFrame());
}
}
}
@Override
public void run() {
Log.d(TAG, "Starting game loop");
long beginTime;
long timeDiff;
int sleepTime = 0;
int framesSkipped;
while(mRunning) {
Canvas c = null;
try {
c = mSurfaceHolder.lockCanvas();
synchronized (mSurfaceHolder) {
beginTime = System.currentTimeMillis();
framesSkipped = 0;
mGamePanel.updatePhysics();
mGamePanel.render(c);
timeDiff = System.currentTimeMillis() - beginTime;
sleepTime = (int)(FRAME_PERIOD - timeDiff);
/if (sleepTime > 0) {
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
}
}
while (sleepTime <= 0 && framesSkipped < MAX_FRAME_SKIPS) {
mGamePanel.updatePhysics();
sleepTime += FRAME_PERIOD;
framesSkipped++;
}
if (framesSkipped > 0) {
Log.d(TAG, "Skipped:" + framesSkipped);
}
}
} finally {
if (c != null) {
mSurfaceHolder.unlockCanvasAndPost(c);
}
}
}