1
votes

I am very new to andEngine, I want to add sprites on screen and let them move and zoom on finger touch. Right now i am able to add multiple sprites on scene , and they can be dragged on touch.

Here is my code:

public class Main extends SimpleBaseGameActivity {

    @Override

    private Camera camera;
    private BitmapTextureAtlas mBitmapTextureAtlas;
    private ITextureRegion mFaceTextureRegion;
    private ITextureRegion mFaceTextureRegion2;

    private static final int CAMERA_WIDTH = 800;
    private static final int CAMERA_HEIGHT = 480;

    @Override
    public EngineOptions onCreateEngineOptions() {
            camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
            EngineOptions engineOptions = new EngineOptions(true,
                            ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(
                                            CAMERA_WIDTH, CAMERA_HEIGHT), camera);
            return engineOptions;
    }

    @Override
    protected void onCreateResources() {
            BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");

            this.mBitmapTextureAtlas = new BitmapTextureAtlas(
                            this.getTextureManager(), 1024, 1600, TextureOptions.NEAREST);

            // background
            // this.background = new Sprite(0, 0,
            // BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas,
            // this, "ui_ball_1.png", 0, 0, 1, 1),
            // this.getVertexBufferObjectManager());
            this.mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory
                            .createFromAsset(this.mBitmapTextureAtlas, this,
                                            "ui_ball_1.png", 0, 0);
            this.mFaceTextureRegion2 = BitmapTextureAtlasTextureRegionFactory
                            .createFromAsset(this.mBitmapTextureAtlas, this,
                                            "ui_ball_1.png", 0, 0);
            this.mBitmapTextureAtlas.load();
            // this.mEngine.getTextureManager().loadTexture(this.mBitmapTextureAtlas);
            /*
             * this.mBitmapTextureAtlas = new
             * BitmapTextureAtlas(this.getTextureManager(), 32, 32,
             * TextureOptions.BILINEAR); this.mFaceTextureRegion =
             * BitmapTextureAtlasTextureRegionFactory
             * .createFromAsset(this.mBitmapTextureAtlas, this, "ui_ball_1.png", 0,
             * 0); this.mBitmapTextureAtlas.load();
             */
    }

    @Override
    protected Scene onCreateScene() {

            /*
             * this.scene = new Scene(); this.scene.attachChild(this.background);
             * this.scene.setBackground(new Background(0.09804f, 0.6274f, 0.8784f));
             * return this.scene;
             */
            this.mEngine.registerUpdateHandler(new FPSLogger());

            final Scene scene = new Scene();
            scene.setBackground(new Background(0.09804f, 0.6274f, 0.8784f));

            final float centerX = (CAMERA_WIDTH - this.mFaceTextureRegion
                            .getWidth()) / 2;
            final float centerY = (CAMERA_HEIGHT - this.mFaceTextureRegion
                            .getHeight()) / 2;

            final Sprite face = new Sprite(centerX, centerY,
                            this.mFaceTextureRegion, this.getVertexBufferObjectManager()) {
                    @Override
                    public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
                                    final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
                            this.setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2,
                                            pSceneTouchEvent.getY() - this.getHeight() / 2);
                            return true;
                    }
            };
            face.setScale(2);
            scene.attachChild(face);
            scene.registerTouchArea(face);

            final Sprite face2 = new Sprite(0, 0, this.mFaceTextureRegion2,
                            this.getVertexBufferObjectManager()) {
                    @Override
                    public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
                                    final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
                            this.setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2,
                                            pSceneTouchEvent.getY() - this.getHeight() / 2);
                            return true;
                    }
            };

            face2.setScale(2);
            scene.attachChild(face2);
            scene.registerTouchArea(face2);

            scene.setTouchAreaBindingOnActionDownEnabled(true);
            return scene;

    }

}

Now i want to zoom each sprite on touch, but unable to find such method like setPosition available to move sprite to a specific position. Can anyone help me in achieving this without affecting the current functionality. Any help would be appreciated, may be in form of code or some direction/method to do this. Thanks in advance :)

1
I don't know why you said,"unable to find such method like setPosition…", but in your code, you did call this methods in the face/face2 sprite's override onAreaTouched(), didn't it. If you want to zoom sprites, also do face and/or face2.setScale() in onAreaTouched().正宗白布鞋

1 Answers

0
votes

you can use a EntityModifier to make your effect:

 @Override
      public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
                        final float pTouchAreaLocalX, final float pTouchAreaLocalY) {

                this.setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2,
                                pSceneTouchEvent.getY() - this.getHeight() / 2);
                this.clearEntityModifiers();
                this.RegisterEntityModifier(new ScaleModifier(1f,2f,4f));
                 //1f = time to convert the scale of sprite of 2f to 4f
                 //2f = initial scale
                 //4f = finish scale

                 //when you dont touch the sprite back to normal scale
                if(event.getAction()== MotionEvent.ACTION_UP) {
                  this.clearEntityModifiers();
                  this.RegisterEntityModifier(new ScaleModifier(1f,4f,2f));
                  }
                 //you also can work with "MotionEvent.ACTION_DOWN" and
                 //MotionEvent.ACTION_MOVE
                return true;
        }