出于某种原因,我的所有精灵都开始显示为黑框,而之前它们不是。这是我的代码。我只用前 2 个精灵让它运行良好,但是当我添加其他精灵时,它们在我的 android 设备上变成了黑盒。任何帮助将不胜感激。
public class GameHabeebActivity extends SimpleBaseGameActivity implements IAccelerationListener, IOnSceneTouchListener, IOnAreaTouchListener {
// ===========================================================
// Constants
// ===========================================================
// ===========================================================
// Fields
// ===========================================================
private BitmapTextureAtlas mBitmapTextureAtlas;
private TextureRegion mBoxFaceTextureRegion;
private TextureRegion mCircleFaceTextureRegion;
private TextureRegion mBaseballTextureRegion;
private TextureRegion mTomatoeTextureRegion;
private TextureRegion mBatTextureRegion;
private TextureRegion mCanTextureRegion;
private TextureRegion mBottleTextureRegion;
private int mFaceCount = 0;
private PhysicsWorld mPhysicsWorld;
private float mGravityX;
private float mGravityY;
private int CAMERA_WIDTH;
private int CAMERA_HEIGHT;
private Scene mScene;
private int itemscale=2;
private int whichface=0;
// ===========================================================
// Constructors
// ===========================================================
// ===========================================================
// Getter & Setter
// ===========================================================
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
@Override
public EngineOptions onCreateEngineOptions() {
Toast.makeText(this, "! HIT THE OBJECTS TO KEEP THEM IN THE AIR !", Toast.LENGTH_LONG).show();
final Display display = getWindowManager().getDefaultDisplay();
CAMERA_WIDTH = display.getWidth();
CAMERA_HEIGHT = display.getHeight();
final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
}
@Override
public void onCreateResources() {
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 600, 170, TextureOptions.BILINEAR);
this.mBoxFaceTextureRegion = (TextureRegion) BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "face_box_tiled.png", 0, 0); // 64x32
this.mCircleFaceTextureRegion = (TextureRegion) BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "face_circle_tiled.png", 64, 0); // 64x32
this.mBaseballTextureRegion= (TextureRegion) BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "Baseball.png", 128, 0);//50x50
this.mTomatoeTextureRegion= (TextureRegion) BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "tomatoe.png", 178, 0);//50x46
this.mBatTextureRegion= (TextureRegion) BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "Baseball_bat.png", 228, 0);//261x36
this.mCanTextureRegion= (TextureRegion) BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "can.png", 489, 0);//50x83
this.mBottleTextureRegion= (TextureRegion) BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "bottle.png", 539, 0);//50x170
}
@Override
public Scene onCreateScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
this.mPhysicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), false);
this.mScene = new Scene();
this.mScene.setBackground(new Background(1, 1, 1));
this.mScene.setOnSceneTouchListener(this);
final VertexBufferObjectManager vertexBufferObjectManager = this.getVertexBufferObjectManager();
final Rectangle roof = new Rectangle(0, 0, CAMERA_WIDTH, 2, vertexBufferObjectManager);
final Rectangle left = new Rectangle(0, 0, 2, CAMERA_HEIGHT, vertexBufferObjectManager);
final Rectangle right = new Rectangle(CAMERA_WIDTH - 2, 0, 2, CAMERA_HEIGHT, vertexBufferObjectManager);
final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(0, 0.5f, 0.5f);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, roof, BodyType.StaticBody, wallFixtureDef);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, left, BodyType.StaticBody, wallFixtureDef);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, right, BodyType.StaticBody, wallFixtureDef);
this.mScene.attachChild(roof);
this.mScene.attachChild(left);
this.mScene.attachChild(right);
this.mScene.registerUpdateHandler(this.mPhysicsWorld);
this.mScene.setOnAreaTouchListener(this);
createitemstimehandler();
return this.mScene;
}
@Override
public boolean onAreaTouched( final TouchEvent pSceneTouchEvent, final ITouchArea pTouchArea,final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
if(pSceneTouchEvent.isActionDown()) {
final Sprite face = (Sprite) pTouchArea;
this.jumpFace(face);
return true;
}
return false;
}
//add objects by touch
@Override
public boolean onSceneTouchEvent(final Scene pScene, final TouchEvent pSceneTouchEvent) {
/*if(this.mPhysicsWorld != null) {
if(pSceneTouchEvent.isActionDown()) {
this.addFace(pSceneTouchEvent.getX(), pSceneTouchEvent.getY());
return true;
}
}*/
return false;
}
@Override
public void onAccelerationAccuracyChanged(final AccelerationData pAccelerationData) {
}
@Override
public void onAccelerationChanged(final AccelerationData pAccelerationData) {
this.mGravityX = pAccelerationData.getX();
this.mGravityY = pAccelerationData.getY();
final Vector2 gravity = Vector2Pool.obtain(this.mGravityX, this.mGravityY);
this.mPhysicsWorld.setGravity(gravity);
Vector2Pool.recycle(gravity);
}
@Override
public void onResumeGame() {
super.onResumeGame();
this.enableAccelerationSensor(this);
}
@Override
public void onPauseGame() {
super.onPauseGame();
this.disableAccelerationSensor();
}
// ===========================================================
// Methods
// ===========================================================
private void addFace(final float pX, final float pY) {
this.mFaceCount++;
TextureRegion whichtexture = null;
if(whichface==0)
{
whichtexture=mBoxFaceTextureRegion;
}
if(whichface==1)
{
whichtexture=mCircleFaceTextureRegion;
}
if(whichface==2)
{
whichtexture=mBaseballTextureRegion;
}
if(whichface==3)
{
whichtexture= mBatTextureRegion;
}
if(whichface==4)
{
whichtexture=mTomatoeTextureRegion;
}
if(whichface==5)
{
whichtexture=mCanTextureRegion;
}
if(whichface==6)
{
whichtexture=mBottleTextureRegion;
}
final Sprite face;
final Body body;
final FixtureDef objectFixtureDef = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);
//add object
face = new Sprite(pX, pY, whichtexture, this.getVertexBufferObjectManager());
face.setScale(itemscale);
body = PhysicsFactory.createCircleBody(this.mPhysicsWorld, face, BodyType.DynamicBody, objectFixtureDef);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face, body, true, true));
face.setUserData(body);
this.mScene.registerTouchArea(face);
this.mScene.attachChild(face);
whichface++;
if(whichface>6)
{
whichface=0;
}
}
private void jumpFace(final Sprite face) {
final Body faceBody = (Body)face.getUserData();
final Vector2 velocity = Vector2Pool.obtain(this.mGravityX * -3, this.mGravityY * -3);
faceBody.setLinearVelocity(velocity);
Vector2Pool.recycle(velocity);
}
private void createitemstimehandler() {
TimerHandler itemTimerHandler;
float mEffectSpawnDelay = 1f;
itemTimerHandler = new TimerHandler(mEffectSpawnDelay, true,
new ITimerCallback() {
@Override
public void onTimePassed(TimerHandler pTimerHandler) {
addFace(CAMERA_HEIGHT,CAMERA_WIDTH/2);
jumpFace ((Sprite)mScene.getLastChild());
}
});
getEngine().registerUpdateHandler(itemTimerHandler);
}
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}