vince88
vince88

Reputation: 3299

Only want one sprite to animate, instead they all do. Andengine

I'm creating an android app using Andengine. One part of the app requires users to select a few sprites from a group of sprites on the screen, which causes the selected sprites to turn a different color (ie, moving to the next tile). I declared them all as animated sprites and I'm using the same texture for each one. The problem is that once I select a sprite, every sprite moves to the next tile, not just the one I selected. How do I make just the one sprite change?

Here's where I setup the textures and whatnot:

private Texture mGreenTextureAtlas;
private  TiledTextureRegion mGreenBallFaceTextureRegion;

@Override
        public void onLoadResources() {
                /* Textures. */
                ...
                this.mGreenTextureAtlas = new Texture(32, 32, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
                        ...
                TextureRegionFactory.setAssetBasePath("gfx/");

                /* TextureRegions. */

                        ...
                this.mGreenBallFaceTextureRegion =  TextureRegionFactory.createTiledFromAsset(this.mGreenTextureAtlas, this, "green_ball.png", 0, 16, 2, 1); // 64x32
                this.mEngine.getTextureManager().loadTextures(this.mCueTextureAtlas, this.mGreenTextureAtlas , this.mBackgroundTexture, this.mPocketTexture);
        }

Here's where I actually create the sprites and apply the textures:

face = new AnimatedSprite(pX, pY, this.mGreenBallFaceTextureRegion);
body = PhysicsFactory.createCircleBody(this.mPhysicsWorld, face, BodyType.DynamicBody, FIXTURE_DEF);
encapsed = new Encapsulator(body, face, Encapsulator.AVOID_BALL, mFaceCount);
ballsList.add(encapsed);

I encapsulate each sprite, it's body, and some other data into an object that I made, and then add that object into an ArrayList.

Here is the onTouch event handler.

@Override
        public boolean onAreaTouched( final TouchEvent pSceneTouchEvent, final ITouchArea pTouchArea,final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
            if(pSceneTouchEvent.isActionDown()) {
                final AnimatedSprite face = (AnimatedSprite) pTouchArea;

                for(int i=0; i<ballsList.size(); i++)
                {
                    if(face.equals(ballsList.get(i).animatedFace))
                    {
                        ballsList.get(i).toggleType(face);
                        System.out.println("Ball " + ballsList.get(i).id + " is now " + ballsList.get(i).type);
                    }                       
                }               

                return true;
            }
            return false;
        }

Finally, here is the toggleType method in the Encapsulator class that's responsible for moving to the next tile:

public void toggleType(AnimatedSprite face)
    {
        if(this.type == AVOID_BALL)
        {
            this.type = HIT_BALL;
            face.nextTile();
        }
        else if(this.type == HIT_BALL)
        {
            this.type = AVOID_BALL;
            face.setCurrentTileIndex(0);
        }
    }

Sorry if this is a bit long-winded. Any help is appreciated.

Upvotes: 1

Views: 1480

Answers (1)

vince88
vince88

Reputation: 3299

I did some more googling and came across a solution. I had to use the textureregion.clone() method when creating the sprites. I found the solution at this link:

http://www.andengine.org/forums/development/two-sprites-sharing-the-same-tiledtextureregion-t4339.html

Upvotes: 2

Related Questions