伙计们是 AndEngine 的新手。我想使用物理处理程序并浏览了andengine的示例。所以在我的例子中,我有一个“球”精灵,我可以通过触摸来移动它,还有一个“方形”动画精灵,我希望它会在墙壁上反弹。但它只是离开屏幕。帮帮我...
package com.Escapo;
import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.handler.physics.PhysicsHandler;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.Scene.IOnSceneTouchListener;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;
import android.view.Display;
public class EscapoActivity extends BaseGameActivity {
private Camera mCamera;
private Scene mMainScene;
//main ball variables
private BitmapTextureAtlas mBitmapTextureAtlas;
private TextureRegion mPlayerTextureRegion;
private Sprite player;
//square
private BitmapTextureAtlas mBitmapTextureAtlas2;
private static final float DEMO_VELOCITY = 100.0f;
private TiledTextureRegion mFaceTextureRegion;
private static int cameraWidth;
private static int cameraHeight;
private Sprite square;
@Override
public void onLoadComplete() {
}
@Override
public Engine onLoadEngine() {
final Display display = getWindowManager().getDefaultDisplay();
cameraWidth = display.getWidth();
cameraHeight = display.getHeight();
mCamera = new Camera(0, 0, cameraWidth, cameraHeight);
return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE,
new RatioResolutionPolicy(cameraWidth, cameraHeight), mCamera));
}
@Override
public void onLoadResources() {
mBitmapTextureAtlas = new BitmapTextureAtlas(512, 512,
TextureOptions.BILINEAR_PREMULTIPLYALPHA);
//square
mBitmapTextureAtlas2 = new BitmapTextureAtlas(512, 512,
TextureOptions.BILINEAR_PREMULTIPLYALPHA);
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
mPlayerTextureRegion = BitmapTextureAtlasTextureRegionFactory
.createFromAsset(this.mBitmapTextureAtlas, this, "ball.png",
0, 0);
//square
this.mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas2, this, "square.png", 0, 0, 1, 1);
mEngine.getTextureManager().loadTextures(mBitmapTextureAtlas,mBitmapTextureAtlas2);
}
@Override
public Scene onLoadScene() {
mEngine.registerUpdateHandler(new FPSLogger());
mMainScene = new Scene();
mMainScene
.setBackground(new ColorBackground(0.09804f, 0.6274f, 0.8784f));
final int PlayerX =(int) ((mCamera.getWidth() - mPlayerTextureRegion
.getWidth()) / 2);
final int PlayerY = (int) ((mCamera.getHeight() - mPlayerTextureRegion
.getHeight()) / 2);
player = new Sprite(PlayerX, PlayerY, mPlayerTextureRegion) {
public boolean onAreaTouched(TouchEvent pSceneTouchEvent,float pTouchAreaLocalX, float pTouchAreaLocalY) {
this.setPosition(pSceneTouchEvent.getX()- this.getWidth() / 2, pSceneTouchEvent.getY()- this.getHeight() / 2);
return true;
}
};
mMainScene.registerTouchArea(player);
mMainScene.setTouchAreaBindingEnabled(true);
mMainScene.attachChild(player);
//square
final Ball ball = new Ball(2, 2, this.mFaceTextureRegion);
final PhysicsHandler physicsHandler = new PhysicsHandler(ball);
ball.registerUpdateHandler(physicsHandler);
physicsHandler.setVelocity(DEMO_VELOCITY, DEMO_VELOCITY);
mMainScene.getLastChild().attachChild(ball);
return mMainScene;
}
private static class Ball extends AnimatedSprite {
private final PhysicsHandler mPhysicsHandler;
public Ball(final float pX, final float pY, final TiledTextureRegion pTextureRegion) {
super(pX, pY, pTextureRegion);
this.mPhysicsHandler = new PhysicsHandler(this);
this.registerUpdateHandler(this.mPhysicsHandler);
}
@Override
protected void onManagedUpdate(final float pSecondsElapsed) {
if(this.mX < 0) {
this.mPhysicsHandler.setVelocityX(DEMO_VELOCITY);
} else if(this.mX + this.getWidth() > cameraWidth) {
this.mPhysicsHandler.setVelocityX(-DEMO_VELOCITY);
}
if(this.mY < 0) {
this.mPhysicsHandler.setVelocityY(DEMO_VELOCITY);
} else if(this.mY + this.getHeight() > cameraHeight) {
this.mPhysicsHandler.setVelocityY(-DEMO_VELOCITY);
}
super.onManagedUpdate(pSecondsElapsed);
}
}
}
我刚刚尝试集成移动球示例http://code.google.com/p/andengineexamples/source/browse/src/org/anddev/andengine/examples/MovingBallExample.java 我哪里出错了?