Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8730879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:06:43+00:00 2026-06-13T09:06:43+00:00

I am new with AndEngine. I just created a scene with many spawned sprites

  • 0

I am new with AndEngine. I just created a scene with many spawned sprites that come from above screen height in landscape mode. Now I want to remove a sprite when I touch on it. Problem is, when I touched the screen the most recent sprite is removed and I can’t remove the tapped sprite individually.

Here is my code:

//======== TimerHandler for collision detection and cleaning up ======
IUpdateHandler detect = new IUpdateHandler() {
    @Override
    public void reset() {
    }

    @Override
    public void onUpdate(float pSecondsElapsed) {

        targets = targetLL.iterator();
        while (targets.hasNext()) {
            _target = targets.next();

            if (_target.getY() >= cameraHeight) {
                // removeSprite(_target, targets);
                tPool.recyclePoolItem(_target);
                targets.remove();
                Log.d("ok", "---------Looop Inside-----");
                // fail();
                break;

            }
            // i don't know whether below code is valid for this postion
            mMainScene.setOnSceneTouchListener(new IOnSceneTouchListener() {
            @Override
                public boolean onSceneTouchEvent(Scene arg0,
                        TouchEvent pSceneTouchEvent) {

                    try {
                        // i can't use this two
                        final float touchX = pSceneTouchEvent.getX();
                        final float touchY = pSceneTouchEvent.getY();

                        if (pSceneTouchEvent.getAction() == TouchEvent.ACTION_DOWN) {
                            //call to remove sprite
                            removeSprite(_target, targets);
                        }
                    } catch (ArrayIndexOutOfBoundsException e) {
                        e.printStackTrace();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return true;
                }
            });
        }
        targetLL.addAll(TargetsToBeAdded);
        TargetsToBeAdded.clear();

    }
};

code for adding target:

/** adds a target at a random location and let it move along the y-axis */
public void addTarget() {
    Random rand = new Random();

    int minX = mTargetTextureRegion.getWidth();
    int maxX = (int) (mCamera.getWidth() - mTargetTextureRegion.getWidth());
    int rangeX = maxX - minX;
    Log.d("----point----", "minX:" + minX + "maxX:" + maxX + "rangeX:"
            + rangeX);

    int rX = rand.nextInt(rangeX) + minX;
    int rY = (int) mCamera.getHeight() + mTargetTextureRegion.getHeight();

    Log.d("---Random x----", "Random x" + rX + "Random y" + rY);

    //HERE I USE POOL TO CREATE NEW SPRITE
    //tPool is object of TargetsPool Class
    target = tPool.obtainPoolItem();
    target.setPosition(rX, rY);

    target.animate(100);
    mMainScene.attachChild(target, 1);
    mMainScene.registerTouchArea(target);

    int minDuration = 2;
    int maxDuration = 32;
    int rangeDuration = maxDuration - minDuration;
    int actualDuration = rand.nextInt(rangeDuration) + minDuration;

    // MoveXModifier mod = new MoveXModifier(actualDuration, target.getX(),
    // -target.getWidth());

    MoveYModifier mody = new MoveYModifier(actualDuration,
            -target.getHeight(), cameraHeight + 10);
    target.registerEntityModifier(mody.deepCopy());
    TargetsToBeAdded.add(target);

}

code for remove sprite when touched:

// ==============the method to remove sprite=====================
public void removeSprite(final AnimatedSprite _sprite, Iterator it) {
    runOnUpdateThread(new Runnable() {

        @Override
        public void run() {
            _target = _sprite;
            mMainScene.detachChild(_target);
        }
    });
    it.remove();
}

I use GenericPool to create new sprite.

I am not sure where I have to write code for the specific touched sprite and remove it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:06:45+00:00Added an answer on June 13, 2026 at 9:06 am

    ohh.. I found the solution here.

    I implemented an IOnAreaTouchListener in BaseGameActivity class. So, my class declaration looks like:

    public class CrazyMonkeyActivity extends BaseGameActivity implements
        IOnAreaTouchListener
    

    and my override method looks like:

    @Override
    public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
            final ITouchArea pTouchArea, final float pTouchAreaLocalX,
            final float pTouchAreaLocalY) {
    
        if (pSceneTouchEvent.isActionDown()) {
            this.removeSprite((AnimatedSprite) pTouchArea);
            return true;
        }
    
        return false;
    }
    

    And remove Sprite methods should be like:

    // ==============the method to remove spirit=====================
    public void removeSprite(final AnimatedSprite target) {
        runOnUpdateThread(new Runnable() {
            @Override
            public void run() {
                mMainScene.detachChild(target);
                mMainScene.unregisterTouchArea(target);
                System.gc();
            }
        });
    }
    

    To create continuous Spawn Sprite I use a time handler inside this method. Below method should be called inside onLoadScene() method:

    /** a Time Handler for spawning targets Sprites, triggers every 2 second */
    private void createSpriteSpawnTimeHandler() {
        TimerHandler spriteTimerHandler;
        float mEffectSpawnDelay = 2f;
    
        spriteTimerHandler = new TimerHandler(mEffectSpawnDelay, true,
                new ITimerCallback() {
                    @Override
                    public void onTimePassed(TimerHandler pTimerHandler) {
                            //position for random sprite
                        final float xPos = MathUtils.random(30.0f,
                                (cameraWidth - 30.0f));
                        final float yPos = MathUtils.random(30.0f,
                                (cameraHeight - 30.0f));
                        //below method call for new sprite
                        addTarget(xPos, yPos);
    
                    }
    
                });
        getEngine().registerUpdateHandler(spriteTimerHandler);
    }
    

    And addTarget() looks like:

    public void addTarget(float xPos, float yPos) {
        Random rand = new Random();
        Log.d("---Random x----", "Random x" + xPos + "Random y" + yPos);
    
        target = new AnimatedSprite(xPos, yPos, mTargetTextureRegion);
        target.animate(100);
        mMainScene.attachChild(target);
        mMainScene.registerTouchArea(target);
    
        int minDuration = 2;
        int maxDuration = 32;
        int rangeDuration = maxDuration - minDuration;
        int actualDuration = rand.nextInt(rangeDuration) + minDuration;
    
        MoveYModifier mody = new MoveYModifier(actualDuration,
                -target.getHeight(), cameraHeight + 10);
        target.registerEntityModifier(mody.deepCopy());
    
    
    }
    

    Hope this might help others too!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im new to android dev and using andengine and ive just come across a
I just copied an example from AndEngine main page, but the app is always
I've just installed andengine from 0. I had to spend 2 days on it.
I am using AndEngine to add sprites to a scene. The sprites are all
1) What is the importance of andEngine.registerUpdateHandler(new FPSLogger()) ? 2) is this just for
I've downloaded AndEngine sources from github and installed new Eclipse with ADT 20.0 (as
I am using this TimerHandler in andEngine to spawn sprites at certain times.. mScene.registerUpdateHandler(new
I am new in AndEngine. I have import the andEngine.jar file in eclipse. But
I am new to Android development and facing a problem while using AndEngine. I
New to PHP and MySQL, have heard amazing things about this website from Leo

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.