0

In my game,I have array of coins appearing on a particular action.Once I touch the coins,coins have to move off smoothly(like flying) to one corner of the screen,one by one. I am creating and drawing coin array like this:

private Coin coins[] = new Coin[10];//coin array

for(int i=0;i<coins.length;i++) {
    coins[i]=objectFactory.createCoin();//creating object array of coins
}

draw coins

 for(int i=0;i<coins.length;i++) {
    coinTexture = coinAnimation.getKeyFrame(animationTime, true);
    batch.draw(coinTexture,coins[i].getX(), coins[i].getY());
}

for detecting touch on coin:

if(Gdx.input.isTouched()) {
        Vector3 touchPos = new Vector3();
        touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
         game.camera.unproject(touchPos);
         for(int i=0;i<coins.length;i++){
             Rectangle textureBounds=new Rectangle(coins[i].getX(),coins[i].getY(),coins[i].getWidth(),coins[i].getHeight());    
             if(textureBounds.contains(touchPos.x,touchPos.y)) {
                 System.out.println("u touched the coin!!!!!!!!");
             }
        }
}

I now want to use Universal TweenEngine to tween it on touch to the corner of the screen.I am completely new to the concept of tween engine. I am not able to find any helpful documentation on how to achieve this affect with tween engine.Any help would be appreciated.

Abhishek Aryan
  • 19,936
  • 8
  • 46
  • 65
Niranjana
  • 514
  • 5
  • 23

1 Answers1

1

Add tween engine in your project, inject these dependency in core module

compile 'com.github.arcnor:universal-tween-engine:6.3.4'
compile 'com.github.arcnor:universal-tween-engine:6.3.4:sources'

These artifacts are available in https://jitpack.io so add this repo in project.

repositories {
    maven { url "https://jitpack.io" }
}

Create CoinAccessor

public class CoinAccessor implements TweenAccessor<Coin> {

    public static final int POS_XY = 1;
    public static final int CPOS_XY = 2;

    @Override
    public int getValues(Coin target, int tweenType, float[] returnValues) {
        switch (tweenType) {
            case POS_XY:
                returnValues[0] = target.getX();
                returnValues[1] = target.getY();
                return 2;

            case CPOS_XY:
                returnValues[0] = target.getX() + target.getWidth()/2;
                returnValues[1] = target.getY() + target.getHeight()/2;
                return 2;

            default: assert false; return -1;
        }
    }

    @Override
    public void setValues(Coin target, int tweenType, float[] newValues) {
        switch (tweenType) {
            case POS_XY: target.setPosition(newValues[0], newValues[1]); 
            break;

            case CPOS_XY: target.setPosition(newValues[0] - target.getWidth()/2, newValues[1] - target.getHeight()/2); 
            break;

            default: assert false;
        }
    }
}

Register CoinAccessor with Coin and update TweenManager.

When there is a proper touch on Coin

Tween.to(coin[i], CoinAccessor.POS_XY, 0.8f).target(targetX,targetY).start(tweenManager);

EDIT

For registration there is static method registerAccessor for the same.

Tween.registerAccessor(Coin.class, new CoinAccessor());

Call update() method of TweenManager in your game's update/render method to update tween manager.

TweenManager tweenManager = new TweenManager();
tweenManager.update(dt);   // this call should be in render/update method 
Abhishek Aryan
  • 19,936
  • 8
  • 46
  • 65