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 4100166
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:31:53+00:00 2026-05-20T20:31:53+00:00

I’m creating some little sprites with a bmp inside the constructor of another view

  • 0

I’m creating some little sprites with a bmp inside the constructor of another view as background. But when I click the sprites, that have their onClickListener, the view I’m clicking is the background view.

Some code:

My constructor:

public ToposGameView(Context context) {
    super(context);

    moles = new ArrayList<MoleSprite>();
    setFocusable(true);
    setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.i(tag, "Click en GameView");

        }
    });

    gameLoopThread = new GameLoopThread(this);

    int id = 0;
    for(int x = 0; x<3; x++){
        for(int y = 0; y<4 ; y++){

            MoleSprite mole = new MoleSprite(this, x*WIDTH/3, HEIGHT/6+y*HEIGHT/6, FRONT);
            mole.setId(id);
            id++;
            moles.add(mole);
        }
    }

    holder = getHolder();
    holder.addCallback(new Callback() {

        @Override
        public void surfaceDestroyed(SurfaceHolder arg0) {
            boolean retry=true;
            gameLoopThread.setRunning(false);
            while(retry){
                try{
                    gameLoopThread.join();
                    retry=false;

                }catch(InterruptedException i){

                }
            }
        }

        @Override
        public void surfaceCreated(SurfaceHolder arg0) {
            gameLoopThread.setRunning(true);

            gameLoopThread.start();

        }

        @Override
        public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2,
                int arg3) {
            // TODO Auto-generated method stub

        }
    });

}

Sprite’s constructor:

public MoleSprite(ToposGameView gameView, int x, int y, int direction) {
    super(gameView.getContext()); //TODO
    bmp = BitmapFactory.decodeResource(getResources(), R.drawable.bad1);
    this.width = bmp.getWidth() / BMP_COLUMNS;      
    this.height = bmp.getHeight() / BMP_ROWS;

    this.x=x;
    this.y= y;

    this.setClickable(true);
    this.setFocusable(true);
    this.setFocusableInTouchMode(true);
    setOnClickListener(this);
    setOnTouchListener(this);
    //I don't know what else to do!
    }

It’s my first question, please ask me for more info.

Edit1:

public class MoleSprite extends View implements OnClickListener, OnTouchListener{

public class ToposGameView extends SurfaceView{
  • 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-05-20T20:31:53+00:00Added an answer on May 20, 2026 at 8:31 pm

    Your mixing Surface drawing with View drawing. Surfaces are great for rapid asynchronous redraws (such as games), but they are not View containers. That is, there is no addView method on SurfaceView that is required for events and layouts.

    As such, you will need to implement onClick handling on your SurfaceView and manually implement the detection of which Sprite is being clicked (e.g., is the click event within the sprite’s x/y/width/height bounding box).

    Also, it doesn’t make sense for MoleSprite to extend View if used this way. I expect you’ll want to make your own Sprite base class to support the bounding boxes and drawing callbacks. You can also add the onClick(..) interface to the Spirte base class, but you’ll probably also want to keep separate lists for drawable/moveable/clickable items to optimize your list iterations as you develop your game.

    Good luck!

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

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I have just tried to save a simple *.rtf file with some websites and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a bunch of posts stored in text files formatted in yaml/textile (from

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.