Android 发动机施加力
有人知道为什么applyforce只对我的一个精灵有效吗?另外,当我按下其他精灵时,它也会对一个单独的精灵施加力。nextTile方法效果很好Android 发动机施加力,android,andengine,Android,Andengine,有人知道为什么applyforce只对我的一个精灵有效吗?另外,当我按下其他精灵时,它也会对一个单独的精灵施加力。nextTile方法效果很好 在此处输入代码package com.martynnorman.jude /** *@作者尼古拉斯·格拉姆利奇 *@自11:54:51-03.04.2010 */ 公共类MainActivity扩展BaseGameActivity实现了OneaTouchListener{ // ======================================
在此处输入代码
package com.martynnorman.jude
/**
*@作者尼古拉斯·格拉姆利奇
*@自11:54:51-03.04.2010
*/
公共类MainActivity扩展BaseGameActivity实现了OneaTouchListener{
// ===========================================================
//常数
//===========================================================
private static final int CAMERA_WIDTH = 720;
private static final int CAMERA_HEIGHT = 480;
int centerX;
int centerY;
// ===========================================================
// Fields
// ===========================================================
private Camera mCamera;
private BitmapTextureAtlas mBitmapTextureAtlas;
private TiledTextureRegion mFaceTextureRegion;
private BitmapTextureAtlas mBitmapTextureAtlas2;
private TiledTextureRegion mFaceTextureRegion2;
Random random = new Random();
Ball sprite;
int scale;
Scene scene;
private BitmapTextureAtlas mFontTexture;
private Font mFont;
Text textcenter;
int t = 1;
Ball rgSprite[] = new Ball[10];
private PhysicsWorld mPhysicsWorld;
Body body;
int isTouched;
final Vector2 gravity2 = new Vector2(0, 20);
final Vector2 gravity = new Vector2(0, 0);
私有静态最终FixtureDef FIXTURE_DEF=PhysicsFactory.createFixtureDef(1,0.5f,0.5f)
//===========================================================
//建设者
// ===========================================================
// ===========================================================
//吸气剂和塞特
// ===========================================================
// ===========================================================
//超类/接口的方法/来自超类/接口的方法
// ===========================================================
@凌驾
公共引擎onLoadEngine(){
this.mCamera=新摄像头(0,0,摄像头宽度,摄像头高度);
返回新引擎(新引擎选项(true、ScreenOrientation.横向、新比率解决方案策略(CAMERA_WIDTH、CAMERA_HEIGHT)、this.mCamera));
}
@凌驾
公共void onLoadResources(){
this.mBitmapTextureAtlas=新的BitmapTextureAtlas(64,32,TextureOptions.BILINEAR_Premultiplylpha);
this.mFaceTextureRegion=BitMapTextureAtlastTextureRegionFactory.createTiledFromAsset(this.mbitMacTextureAtlas,this,“gfx/ball.png”,0,0,2,1);
this.mBitmapTextureAtlas2=新的BitmapTextureAtlas(64,32,TextureOptions.BILINEAR_Premultiplylpha);
this.mFaceTextureRegion2=BitmapTextureAtlastTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas2,this,“gfx/ball2.png”,0,0,2,1);
this.mFontTexture=新的BitmapTextureAtlas(256,256,TextureOptions.BILINEAR\u Premultiplylpha);
this.mFont=新字体(this.mFontTexture,Typeface.create(Typeface.DEFAULT,Typeface.BOLD),32,true,Color.BLACK);
this.mEngine.getTextureManager().loadTextures(this.mBitmapTextureAtlas,mBitmapTextureAtlas2);
this.mEngine.getFontManager().loadFont(this.mFont);
this.mEngine.getTextureManager().loadTexture(this.mFontTexture);
}
@凌驾
公共场景onLoadScene(){
createAETimeHandler(2);
/*最终文本文本中心=新文本(100,60,this.mFont,“toucted”,horizontallign.CENTER);
this.mFontTexture=新的BitmapTextureAtlas(256,256,TextureOptions.BILINEAR\u Premultiplylpha);
this.mFont=新字体(this.mFontTexture,Typeface.create(Typeface.DEFAULT,Typeface.BOLD),32,true,Color.BLACK)*/
最终场景=新场景();
场景。setOnAreaTouchListener(此);
场景.背景(新彩色背景(0.6274f,0.6274f,0.8784f));
MPPhysisWorld=新物理世界(新矢量2(0,SensorManager.GRAVITY\u EARTH),错误);
registerUpdateHandler(mpphysisWorld);
mpphysisworld.setGravity(重力);
最终形状地面=新矩形(0,摄像头高度-2,摄像头宽度,2);
final FixtureDef wallFixtureDef=PhysicsFactory.createFixtureDef(0,0.5f,0.5f);
physicFactory.createBoxBody(this.mpphysicsworld、ground、BodyType.StaticBody、wallFixtureDef);
场景。攻击儿童(地面);
左最终形状=新矩形(0,0,2,相机高度);
最终形状右=新矩形(相机宽度-2,0,2,相机高度);
最终形状屋顶=新矩形(0,0,相机宽度,2);
physicFactory.createBoxBody(this.mpphysicsworld,左,BodyType.StaticBody,wallFixtureDef);
physicFactory.createBoxBody(this.mpphysisWorld,右,BodyType.StaticBody,wallFixtureDef);
physicFactory.createBoxBody(this.mpphysicsworld,roof,BodyType.StaticBody,wallFixtureDef);
场景:attachChild(左);
场景。attachChild(右);
场景:attachChild(屋顶);
//scene.setOnSceneTouchListener((IOnSceneTouchListener)this);
//最终击球=新球(random.nextInt(600)+1,random.nextInt(400)+1,this.mfacetureregion.clone());
//最终击球2=新球(random.nextInt(600)+1,random.nextInt(400)+1,this.mfacetureregion2.clone());
//body=physicFactory.createBoxBody(this.mpphysisWorld,hit2,BodyType.DynamicBody,FIXTURE_DEF);
//场景。attachChild(hit2);
//hit2.设置刻度(2);
//场景。注册图查里亚(hit2);
//this.mpphysisworld.registerphysicconnector(新的physicconnector(hit2,body,true,true));
对于(int i=0;i// ===========================================================
// Constructors
// ===========================================================
// ===========================================================
// Getter & Setter
// ===========================================================
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
@Override
public Engine onLoadEngine() {
this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera));
}
@Override
public void onLoadResources() {
this.mBitmapTextureAtlas = new BitmapTextureAtlas(64, 32, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
this.mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "gfx/ball.png", 0, 0, 2, 1);
this.mBitmapTextureAtlas2 = new BitmapTextureAtlas(64, 32, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
this.mFaceTextureRegion2 = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas2, this,"gfx/ball2.png", 0, 0, 2, 1);
this.mFontTexture = new BitmapTextureAtlas(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
this.mFont = new Font(this.mFontTexture, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 32, true, Color.BLACK);
this.mEngine.getTextureManager().loadTextures(this.mBitmapTextureAtlas, mBitmapTextureAtlas2);
this.mEngine.getFontManager().loadFont(this.mFont);
this.mEngine.getTextureManager().loadTexture(this.mFontTexture);
}
@Override
public Scene onLoadScene() {
createAETimeHandler(2);
/* final Text textcenter = new Text(100, 60, this.mFont, "touched", HorizontalAlign.CENTER);
this.mFontTexture = new BitmapTextureAtlas(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
this.mFont = new Font(this.mFontTexture, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 32, true, Color.BLACK);*/
final Scene scene = new Scene();
scene.setOnAreaTouchListener(this);
scene.setBackground(new ColorBackground(0.6274f, 0.6274f, 0.8784f));
mPhysicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), false);
scene.registerUpdateHandler(mPhysicsWorld);
mPhysicsWorld.setGravity(gravity);
final Shape ground = new Rectangle(0, CAMERA_HEIGHT - 2, CAMERA_WIDTH, 2);
final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(0, 0.5f, 0.5f);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, ground, BodyType.StaticBody, wallFixtureDef);
scene.attachChild(ground);
final Shape left = new Rectangle(0, 0, 2, CAMERA_HEIGHT);
final Shape right = new Rectangle(CAMERA_WIDTH - 2, 0, 2, CAMERA_HEIGHT);
final Shape roof = new Rectangle(0, 0, CAMERA_WIDTH, 2);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, left, BodyType.StaticBody, wallFixtureDef);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, right, BodyType.StaticBody, wallFixtureDef);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, roof, BodyType.StaticBody, wallFixtureDef);
scene.attachChild(left);
scene.attachChild(right);
scene.attachChild(roof);
// scene.setOnSceneTouchListener((IOnSceneTouchListener) this);
//final Ball hit = new Ball(random.nextInt(600)+1, random.nextInt(400)+1, this.mFaceTextureRegion.clone());
//final Ball hit2 = new Ball(random.nextInt(600)+1, random.nextInt(400)+1, this.mFaceTextureRegion2.clone());
//body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, hit2, BodyType.DynamicBody, FIXTURE_DEF);
//scene.attachChild(hit2);
//hit2.setScale(2);
//scene.registerTouchArea(hit2);
//this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(hit2, body, true, true));
for (int i = 0; i < rgSprite.length; i++) {
rgSprite[i] = new Ball(random.nextInt(600)+1, 200, this.mFaceTextureRegion.clone());
}
mPhysicsWorld.setGravity(gravity2);
for (Ball sprite : rgSprite) {
body = PhysicsFactory.createCircleBody(this.mPhysicsWorld, sprite, BodyType.DynamicBody, FIXTURE_DEF);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(sprite, body, true, true));
scene.registerTouchArea(sprite);
scene.attachChild(sprite);
sprite.setScale(2);
}
//scene.attachChild(hit);
//hit.setScale(2);
//scene.registerTouchArea(hit);
return scene;
}
@Override
public void onLoadComplete() {
}
private void createAETimeHandler(float mEffectSpawnDelay)
{ TimerHandler spriteTimerHandler2;
this.getEngine().registerUpdateHandler(spriteTimerHandler2 = new TimerHandler(mEffectSpawnDelay, true, new ITimerCallback()
{
@Override
public void onTimePassed(final TimerHandler pTimerHandler)
{
/*if (isTouched == 1){
mFaceTextureRegion.setCurrentTileIndex(0);
isTouched = 2;
} */
}
}));
}
// ===========================================================
// Methods
// ===========================================================
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
@Override
public boolean onAreaTouched(TouchEvent pSceneTouchEvent, ITouchArea pTouchArea, float pTouchAreaLocalX,float pTouchAreaLocalY) {
if(this.mPhysicsWorld != null) {
if (pSceneTouchEvent.getAction() == MotionEvent.ACTION_DOWN) {
this.onOff((AnimatedSprite)pTouchArea);
}
} return false;
}
private void onOff(final AnimatedSprite ball) {
ball.nextTile();
body.applyForce(new Vector2(200,-1500), new Vector2(body.getWorldCenter()));
}