User42590
User42590

Reputation: 2521

Attach Sprite on another Sprite ANDENGINE

I am working on andengine and i have two sprite one is plate and the other is an apple . My plate sprite move form point 1 to point 2 and my apple sprite is jumping up and down.

Now i want to make apple jump on plate. I tried it with attched child apple with plate but the apple not place on the plate. Apple place below the plate i used zindex but its not working.

Actually problem is to move apple and plate at the same time. Any help would be appriciated. I am stuck with that that why this is happening and what will be solution .Here is my code:

 plateDisplay = new Sprite( 250, 300, this.plate, this.getVertexBufferObjectManager());

 appleDisplay = new Sprite( 250, 140, this.apple, this.getVertexBufferObjectManager());

 plateDisplay.registerEntityModifier(new LoopEntityModifier(new PathModifier(20, path, EaseLinear.getInstance())));

 appleDisplay.registerEntityModifier(new LoopEntityModifier(new ParallelEntityModifier(new MoveYModifier(1, appleDisplay.getY(), 
            (appleDisplay.getY()+70), EaseBounceInOut.getInstance()))));

    this.appleDisplay.setZIndex(1);
    plateDisplay.setZIndex(0);
    plateDisplay.attachChild(this.appleDisplay);
    scene.attachChild(plateDisplay);

Upvotes: 3

Views: 1758

Answers (1)

Plastic Sturgeon
Plastic Sturgeon

Reputation: 12527

The issue you are having is that there are different coordinate systems for each object. The Plate sprite has its own X and Y in the scene coordinates. But when you add the apple to the plate object you are now using the plates local coordinates. So if the apple was on the scene's 50,50, when you add it to the plate, it will now be 50,50 as measured from the transform center point of the plate.

There are LocaltoScene and ScenetoLocal coordinate utilities in andengine to help you make this conversion. But underneath they are not super complex - they just add the transforms of all the nested sprites. Both utilites are part of the Sprite class, so you call them from the sprite in question. In your case probably

// Get the scene coordinates of the apple as an array.
float[] coodinates = [appleDisplay.getX(), appleDisplay.getY()];
// Convert the the scene coordinates of the apple to the local corrdinates of the plate.
float[] localCoordinates = plateDisplay.convertSceneToLocalCoordinates(coordinates);
// Attach and set position of apple
appleDisplay.setPosition(localCoordinates[0], localCoordintates[1]);
plateDisplay.attachChild(appleDisplay);

Upvotes: 5

Related Questions