1
\$\begingroup\$

In my app i need to add multiple sprite and add event to them. What i am doing now is using individual event listener for each and every sprite which is a kind of mess.

buttonS[0] = new Sprite(x_pos += 0, y_pos, orangeNumImage[0], this.getVertexBufferObjectManager()) {
        @Override
        public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
            if(pSceneTouchEvent.isActionDown()) {
                //Perform Some task
                }
                return true;
            } else {
                //Perform Some task
                return false;
            }
        }
    };


    buttonS[1] = new Sprite(x_pos += 115, y_pos, orangeNumImage[1], this.getVertexBufferObjectManager()) {
        @Override
        public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
            if(pSceneTouchEvent.isActionDown()) {               
                //Perform Some task
                }
                return true;
            } else {
                return false;
            }
        }
    };

I was searching to handle this event with just one event listener and make the code shorter.

In normal android (without andengine) we could use something like this (by using android:onclick to areaTouched in button):-

public void areaTouched(View view) {
    Button nextclick = (Button) findViewById(R.id.button_nextClick);
    Button b = (Button)view;
    int check = Integer.parseInt(b.getText().toString());
    if (check == i){
          ........
    }
}
\$\endgroup\$
1
\$\begingroup\$

Extend the Sprite class and add a method to add you own TouchListener and a method to set some user data. Or implement the touch logic in your new Sprite child class by overriding the onAreaTouched method. I don't think there is any other mechanism in AndEngine that would allow you to register a touch listener to Sprites.

\$\endgroup\$
  • \$\begingroup\$ This concurs with what I tried to describe in the AndEngine forum. \$\endgroup\$ – Jesse J Sep 12 '14 at 19:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.