我是android游戏开发的新手。我遵循一个示例并想在我的游戏中添加部分系统作为背景,但我遇到了性能问题。谁能告诉我如何提高性能andengine
?
public class NewFile extends SimpleBaseGameActivity implements
IAccelerationListener {
private static int CAMERA_WIDTH = 800;
private static int CAMERA_HEIGHT = 480;
private static int MODE = 0;
CountDownTimer spriteScaleTimer;
private int mNumPart = 10;
private float mSpdInitial = 150.0f;
private float mSpdParticle = mSpdInitial;
private float mSpdIncr = 125.0f / mNumPart;
boolean hide = false;
float previousY, previousX;;
boolean isMoving;
Entity backgroundLayer;
Scene scene;
private Sprite mBall;
AnimatedSprite physicsSprite;
private PhysicsWorld physicsWorld;
private TextureRegion mPartReg;
// ///exlposion in sprite///
private BitmapTextureAtlas texExplode, mBallTexture;
private TiledTextureRegion regExplode, mBallTextureRegion;
private AnimatedSprite sprExplode;
private static int SPR_COLUMN = 4;
private static int SPR_ROWS = 4;
private static int SPR_COLUMN1 = 1;
private static int SPR_ROWS1 = 1;
private BitmapFont bitmapfont;
private Text textChances;
private Text textTimer;
// Camera mCamera;
private int width, height;
SmoothCamera mCamera;
@Override
public EngineOptions onCreateEngineOptions() {
// TODO Auto-generated method stub
ObjectClass.count = 0;
final DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.getDefaultDisplay().getMetrics(displayMetrics);
width = displayMetrics.widthPixels;
height = displayMetrics.heightPixels;
if (width > height) {
CAMERA_WIDTH = 800;
CAMERA_HEIGHT = 480;
// Mode =1 = landscape mode
MODE = 1;
this.mCamera = new SmoothCamera(0f, 0f, (float) CAMERA_WIDTH,
(float) CAMERA_HEIGHT, 10, 10, 1.0f);
return new EngineOptions(true, ScreenOrientation.LANDSCAPE_SENSOR,
new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT),
mCamera);
} else {
CAMERA_WIDTH = 480;
CAMERA_HEIGHT = 800;
// Mode =0 = portrait mode
MODE = 0;
this.mCamera = new SmoothCamera(0f, 0f, (float) CAMERA_WIDTH,
(float) CAMERA_HEIGHT, 10, 10, 1.0f);
return new EngineOptions(true, ScreenOrientation.PORTRAIT_SENSOR,
new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT),
mCamera);
}
}
@Override
protected void onCreateResources() {
// TODO Auto-generated method stub
try {
this.bitmapfont = new BitmapFont(this.getTextureManager(),
this.getAssets(), "afnt/BitmapFont.fnt");
this.bitmapfont.load();
BitmapTextureAtlas mPart = new BitmapTextureAtlas(
this.getTextureManager(), 21, 21,
TextureOptions.BILINEAR_PREMULTIPLYALPHA);
this.mPartReg = BitmapTextureAtlasTextureRegionFactory
.createFromAsset(mPart, this, "afx/particle.png", 0, 0);
mEngine.getTextureManager().loadTexture(mPart);
// menu items end//
mBallTexture = new BitmapTextureAtlas(this.getTextureManager(), 78,
79, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
mBallTextureRegion = BitmapTextureAtlasTextureRegionFactory
.createTiledFromAsset(mBallTexture, this.getAssets(),
"afx/ball.png", 0, 0, SPR_COLUMN1, SPR_ROWS1);
mBallTexture.load();
texExplode = new BitmapTextureAtlas(this.getTextureManager(), 204,
204, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
regExplode = BitmapTextureAtlasTextureRegionFactory
.createTiledFromAsset(texExplode, this.getAssets(),
"afx/explosion_transparent.png", 0, 0, SPR_COLUMN,
SPR_ROWS);
texExplode.load();
} catch (Exception e) {
Debug.e(e);
}
}
public Scene gameScene() {
this.mEngine.registerUpdateHandler(new FPSLogger(1f));
scene = new Scene();
Entity forgroundLayer = new Entity();
textTimer = new Text(CAMERA_WIDTH - 210, 10, this.bitmapfont,
"Time-00", new TextOptions(HorizontalAlign.CENTER),
this.getVertexBufferObjectManager());
forgroundLayer.attachChild(textTimer);
Log.e("Deepak", "COUNT-" + ObjectClass.count);
textChances = new Text(CAMERA_WIDTH - 370, 10, this.bitmapfont,
"Score-" + ObjectClass.count, new TextOptions(
HorizontalAlign.CENTER),
this.getVertexBufferObjectManager());
forgroundLayer.attachChild(textChances);
// Create our physics world
this.physicsWorld = new FixedStepPhysicsWorld(60, new Vector2(0, 0),
false);
// Register our physics world to update our scene
scene.registerUpdateHandler(this.physicsWorld);
// Enable the accelerometer and bind our own onAccelerometerChanged()
// method as listener for events
this.mEngine.enableAccelerationSensor(this, this);
mBall = new Sprite(CAMERA_WIDTH / 2, CAMERA_HEIGHT / 2,
this.mBallTextureRegion, getVertexBufferObjectManager());
scene.attachChild(mBall);
try {
// Create walls around our scene
createWalls();
// Create the moving body
createPhysicsBody();
} catch (Exception e) {
// TODO: handle exception
}
/* The actual collision-checking. */
scene.registerUpdateHandler(new IUpdateHandler() {
@Override
public void reset() {
}
@Override
public void onUpdate(final float pSecondsElapsed) {
if (physicsSprite.hasParent()) {
if (physicsSprite.collidesWith(mBall)) {
try {
removeBall(physicsSprite, mBall);
} catch (Exception e) {
// TODO: handle exception
}
}
}
}
});
scene.attachChild(forgroundLayer);
Timer();
return scene;
}
@Override
protected Scene onCreateScene() {
return gameScene();
}
// Removes a ball (sprite) and associated body
private void removeBall(final Sprite ball, final Sprite ball2) {
final PhysicsConnector ballPhysicsConnector = this.physicsWorld
.getPhysicsConnectorManager().findPhysicsConnectorByShape(ball);
this.mEngine.runOnUpdateThread(new Runnable() {
@Override
public void run() {
if (ballPhysicsConnector != null) {
float px = ball.getX();
float py = ball.getY();
float sx = ball2.getX();
float sy = ball2.getY();
float mx = (px + sx) / 2;
float my = (py + sy) / 2;
physicsWorld
.unregisterPhysicsConnector(ballPhysicsConnector);
ballPhysicsConnector.getBody().setActive(false);
physicsWorld.destroyBody(ballPhysicsConnector.getBody());
scene.unregisterTouchArea(ball);
scene.detachChild(ball);
System.gc();
hide = true;
sprExplode = new AnimatedSprite(mx, my, regExplode,
getVertexBufferObjectManager());
scene.attachChild(sprExplode);
sprExplode.animate(70, false, new IAnimationListener() {
@Override
public void onAnimationStarted(
AnimatedSprite pAnimatedSprite,
int pInitialLoopCount) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationFrameChanged(
AnimatedSprite pAnimatedSprite,
int pOldFrameIndex, int pNewFrameIndex) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationLoopFinished(
AnimatedSprite pAnimatedSprite,
int pRemainingLoopCount, int pInitialLoopCount) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationFinished(
AnimatedSprite pAnimatedSprite) {
// TODO Auto-generated method stub
scene.detachChild(sprExplode);
spriteScaleTimer.cancel();
gameToast("Play again!!");
scene.reset();
try {
ObjectClass.count++;
textChances.setText("Score-"
+ ObjectClass.count);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
System.gc();
createPhysicsBody();
Timer();
}
});
}
System.gc();
}
});
}
public void RemoveOnTimeUp(final Sprite ball) {
final PhysicsConnector ballPhysicsConnector = this.physicsWorld
.getPhysicsConnectorManager().findPhysicsConnectorByShape(ball);
this.mEngine.runOnUpdateThread(new Runnable() {
@Override
public void run() {
if (ballPhysicsConnector != null) {
physicsWorld
.unregisterPhysicsConnector(ballPhysicsConnector);
ballPhysicsConnector.getBody().setActive(false);
physicsWorld.destroyBody(ballPhysicsConnector.getBody());
scene.unregisterTouchArea(ball);
scene.detachChild(ball);
hide = true;
gameToast("Time up Play again!!");
scene.reset();
// ++count;
System.gc();
createPhysicsBody();
Timer();
System.gc();
}
}
});
}
public void Timer() {
this.runOnUiThread(new Runnable() {
public void run() {
spriteScaleTimer = new CountDownTimer(20000, 1000) {
public void onTick(long millisUntilFinished) {
textTimer.setText("Time-" + millisUntilFinished / 1000);
// spriteScaleTimer.
// put scaling here
}
public void onFinish() {
RemoveOnTimeUp(physicsSprite);
System.gc();
// Max scale size.
}
}.start();
}
});
}
public void gameToast(final String msg) {
this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(NewFile.this, msg, Toast.LENGTH_SHORT).show();
}
});
}
private void createWalls() {
final IAreaShape ground = new Rectangle(0, CAMERA_HEIGHT - 2,
CAMERA_WIDTH, 2, getVertexBufferObjectManager());
final IAreaShape roof = new Rectangle(0, 0, CAMERA_WIDTH, 2,
getVertexBufferObjectManager());
final IAreaShape left = new Rectangle(0, 0, 2, CAMERA_HEIGHT,
getVertexBufferObjectManager());
final IAreaShape right = new Rectangle(CAMERA_WIDTH - 2, 0, 2,
CAMERA_HEIGHT, getVertexBufferObjectManager());
final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(0,
0.5f, 1f);
PhysicsFactory.createBoxBody(this.physicsWorld, ground,
BodyType.StaticBody, wallFixtureDef);
PhysicsFactory.createBoxBody(this.physicsWorld, roof,
BodyType.StaticBody, wallFixtureDef);
PhysicsFactory.createBoxBody(this.physicsWorld, left,
BodyType.StaticBody, wallFixtureDef);
PhysicsFactory.createBoxBody(this.physicsWorld, right,
BodyType.StaticBody, wallFixtureDef);
this.scene.attachChild(ground);
this.scene.attachChild(roof);
this.scene.attachChild(left);
this.scene.attachChild(right);
}
private void createPhysicsBody() {
// Create the sprite
physicsSprite = new AnimatedSprite(0f, 0f, mBallTextureRegion,
getVertexBufferObjectManager());
// Create the physics body
final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(1,
0.5f, 0.5f);
final Body body = PhysicsFactory.createCircleBody(this.physicsWorld,
physicsSprite, BodyType.DynamicBody, wallFixtureDef);
// Add the sprite to the scene
this.scene.attachChild(physicsSprite);
// Link the sprite and the body
this.physicsWorld.registerPhysicsConnector(new PhysicsConnector(
physicsSprite, body, true, true));
}
@Override
public void onAccelerationAccuracyChanged(AccelerationData pAccelerationData) {
// TODO Auto-generated method stub
}
final int con = 10;
private float mGravityX;
private float mGravityY;
private final Vector2 mTempVector = new Vector2();
@Override
public void onAccelerationChanged(AccelerationData pAccelerationData) {
// TODO Auto-generated method stub
this.mGravityX = pAccelerationData.getY() * 2;
this.mGravityY = pAccelerationData.getX() * 2;
if (this.mGravityX > con)
this.mGravityX = con;
if (this.mGravityY > con)
this.mGravityY = con;
if (this.mGravityX < con * (-1))
this.mGravityX = con * (-1);
if (this.mGravityY < con * (-1))
this.mGravityY = con * (-1);
this.mTempVector.set(this.mGravityX, this.mGravityY);
this.physicsWorld.setGravity(this.mTempVector);
}
private void initPartical1() {
// Init the particle system
final SpriteParticleSystem particleSystem = new SpriteParticleSystem(
new PointParticleEmitter(NewFile.CAMERA_WIDTH / 2,
NewFile.CAMERA_HEIGHT / 2), 10 * mNumPart,
10 * mNumPart, mNumPart, this.mPartReg,
this.getVertexBufferObjectManager());
// Dirt color
particleSystem
.addParticleInitializer(new ColorParticleInitializer<Sprite>(
0.2450f, 0.0705f, 0.5745f));
// They will shrink
particleSystem.addParticleModifier(new ScaleParticleModifier<Sprite>(0,
2, 0, 1));
particleSystem.addParticleModifier(new ColorParticleModifier<Sprite>(0,
1, 0, 1, 0, 1, 0, 1));
final Random generator = new Random();
// Init the particles with random in the angle
particleSystem
.addParticleInitializer(new IParticleInitializer<Sprite>() {
@Override
public void onInitializeParticle(Particle<Sprite> pParticle) {
// Create particles between 225º - 315º
int ang = generator.nextInt(361);// + 10;
mSpdParticle -= mSpdIncr;
float fVelocityX = (float) (Math.cos(Math
.toRadians(ang)) * mSpdParticle);
float fVelocityY = (float) (Math.sin(Math
.toRadians(ang)) * mSpdParticle);
pParticle.getPhysicsHandler().setVelocity(
fVelocityX * 2, fVelocityY * 2);
pParticle.getPhysicsHandler().setAcceleration(
(fVelocityX * (50 / 100.0f)),
(fVelocityY * (75 / 100.0f)));
}
});
mSpdParticle = mSpdInitial;
backgroundLayer.attachChild(particleSystem);
// Remove the particles from the scene
mEngine.registerUpdateHandler(new TimerHandler(2, new ITimerCallback() {
@Override
public void onTimePassed(final TimerHandler pTimerHandler) {
backgroundLayer.detachChild(particleSystem);
// backgroundLayer.reset();
System.gc();
}
}));
}
@Override
public void onBackPressed() {
System.gc();
System.exit(0);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
if (MODE == 0) {
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
scene.setScale(1);
scene.setPosition(0, 0);
} else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
scene.setScaleY(1.6f);
scene.setScaleX(0.6f);
scene.setPosition(120, -240);
}
} else if (MODE == 1) {
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
scene.setScaleY(0.6f);
scene.setScaleX(1.6f);
scene.setPosition(-240, 120);
} else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
scene.setScale(1);
scene.setPosition(0, 0);
}
}
}
}