和发动机施加力

发布于 2024-11-27 02:42:17 字数 7405 浏览 0 评论 0原文

有谁知道为什么 applyforce 只适用于我的精灵之一?此外,当我按下其他精灵时,它也会对单个精灵施加力。 nextTile 方法工作正常。

在此处输入代码package com.martynnorman.jude;

/** *@作者尼古拉斯·格拉姆利奇 * @自 11:54:51 - 03.04.2010 */ 公共类 MainActivity 扩展 BaseGameActivity 实现 IOnAreaTouchListener { // ================================================== =========== // 常量 // ================================================== ===========

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);

// ===========================================================
// 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()));
}

}

does anyone know why applyforce only works on one of my sprites? Also when I press the other sprites it also applies force to the one individual sprite. The nextTile method works fine.

enter code herepackage com.martynnorman.jude;

/**
* @author Nicolas Gramlich
* @since 11:54:51 - 03.04.2010
*/
public class MainActivity extends BaseGameActivity implements IOnAreaTouchListener {
// ===========================================================
// Constants
// ===========================================================

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);

private static final FixtureDef FIXTURE_DEF = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);

// ===========================================================
// 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()));
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只是在用心讲痛 2024-12-04 02:42:17

这是因为您只有一个 Body 变量,并且它会被循环覆盖。创建所有精灵后,您只知道最后一个精灵的主体。

您必须将力施加到与您单击的精灵关联的主体上。

It is because you only have one Body-variable, and it gets overwritten by the loop. When all sprites have been created you only know the body of the last sprite.

You must apply the force to the body associated with the sprite you click.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文