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

  • SEARCH
  • Home
  • 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 6542269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:12:19+00:00 2026-05-25T11:12:19+00:00

I have two images moving across the screen, one is a ball and one

  • 0

I have two images moving across the screen, one is a ball and one is a man.
What I want to happen is when the user touches the image of the man, the ball drops.

My problem is I cannot seem to add an onclick/ontouch event and get it to work.

I’m not implementing it properly, can anyone help please?

I have included the 3 classes below. Greg is the man and the ball is named ball 🙂

TestAnimationActivity.java

 package com.test.firstAnimation;

    import android.app.Activity;
    import android.os.Bundle;

    public class TestAnimationActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(new MyAnimationView(this));
       }
    }

Sprite.java

package com.test.firstAnimation;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class Sprite extends View implements OnClickListener{

     private static int gregCoordX = 410; // the x coordinate at the canvas for greg
     private Bitmap img; // the image of Greg
     private Bitmap img2; // the image of pointer 
     private static int gregCoordY = 125; // the y coordinate at the canvas for greg
     private static int pointCoordX = 10;
     private static int pointCoordY = 10;
     private static int count = 1;
     private static int ballSpeed = 25;
     private static boolean goingRight = false;
     private static boolean goingLeft = true;
     private static boolean pointerGoingRight = false;
     private static boolean pointerGoingLeft = true;


    public Sprite(Context context, int drawable) {

        super(context);

        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
        img = BitmapFactory.decodeResource(context.getResources(), drawable);
        img2 = (BitmapFactory.decodeResource(context.getResources(), drawable));
        count++;
    }

    public static int getCount() {
        return count;
    }

    void setX(int newValue) {
        gregCoordX = newValue;
    }

    public static int getX() {
        return gregCoordX;
    }

    public static int getY() {
        return gregCoordY;
    }

    public static int getBX() {
        return pointCoordX;
    }

    public static int getBY() {
        return pointCoordY;
    }

    public Bitmap getBitmap() {
        return img;
    }

    public Bitmap getImg2() {
        return img2;
    }

    public static void dropBall()
    {
        pointCoordY++;
    }

    public static void moveBall(int x) {

           // check the borders
            //if more than ten go right
            //if ten go left
            //if more than 250 go left
            if (x <= 10 && pointerGoingLeft)
            {
            pointCoordX = pointCoordX + ballSpeed;
            pointerGoingRight = true;
            pointerGoingLeft = false;
            }
            else if (x >= 410 && pointerGoingRight)
            {
                pointCoordX = pointCoordX - ballSpeed;
                pointerGoingLeft = true;
                pointerGoingRight = false;
            }
            else if (pointerGoingRight)
                pointCoordX = pointCoordX + ballSpeed;
            else
                pointCoordX = pointCoordX - ballSpeed;

            if(MyAnimationView.ballDropping == true)
            {
                while (pointCoordY<gregCoordY)
                    dropBall();
            }
    }

    public static void moveGreg(int x) {

        if (x <= 10 && goingLeft)
        {
        gregCoordX = gregCoordX + count;
        goingRight = true;
        goingLeft = false;
        }
        else if (x >= 410 && goingRight)
        {
        gregCoordX = gregCoordX - count;
        goingLeft = true;
        goingRight = false;
        }
        else if (goingRight)
        gregCoordX = gregCoordX + count;
        else
        gregCoordX = gregCoordX - count;
}

    @Override
    public void onClick(View arg0) {
        dropBall();
    }
}

MyAnimationView.java

package com.test.firstAnimation;

import android.content.Context;
import android.graphics.Canvas;
import android.view.View;

public class MyAnimationView extends View{

    private Sprite greg;
    private Sprite ball;
    private int xCoOr;
    private int ballXCoOr;
    public static boolean ballDropping;

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

        ballDropping = false;
        greg = new Sprite(context,R.drawable.greg);
        ball = new Sprite(context, R.drawable.ball);

        OnClickListener gregClicked = new OnClickListener() {
            public void onClick(View v) {
            ballDropping = true;
            }
        };
        greg.setOnClickListener(gregClicked);
        }

    @Override protected void onDraw(Canvas canvas) {

     canvas.drawColor(0xFFFFFFFF);                                   //white background      

     ballXCoOr = Sprite.getBX();  
     xCoOr = Sprite.getX();
     Sprite.moveGreg(xCoOr);                                         //move ball left or right depending
     Sprite.moveBall(ballXCoOr);

     if(ballDropping == true)
     {
         Sprite.dropBall();
     }

     canvas.drawBitmap(greg.getBitmap(), xCoOr, Sprite.getY(), null);
     canvas.drawBitmap(ball.getBitmap(), ballXCoOr, Sprite.getBY(), null);
     invalidate();
     }
}

Thanks in advance, I’ve been stuck for days!

Ben

  • 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-25T11:12:19+00:00Added an answer on May 25, 2026 at 11:12 am
        float touched_x, touched_y;
        boolean touched = false;
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            // TODO Auto-generated method stub
            touchCounter++;
            touched_x = event.getX();
            touched_y = event.getY();
    
            int action = event.getAction();
            switch (action) {
            case MotionEvent.ACTION_DOWN:
                touched = true;
                break;
            case MotionEvent.ACTION_MOVE:
                touched = true;
                break;
            case MotionEvent.ACTION_UP:
                touched = false;
                break;
            case MotionEvent.ACTION_CANCEL:
                touched = false;
                break;
            case MotionEvent.ACTION_OUTSIDE:
                touched = false;
                break;
            default:
            }
            return true; // processed
        }
    

    Then;

        if (touched) {
            //control here
        }
    

    touched_x, touched_y are coordinates of the point that is clicked on the screen. You can compare Greg’s coordinates and these coordinates. If same, then do what you wanna do.

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

Sidebar

Related Questions

I have two images side-by-side. When the user hovers over an image, it should
I have three images that I want the user to move around the screen.
I have two images that I want adjacent but they appear one above the
I have two images - 1)a rectangular one, displaying the actual content and 2)a
I have two images. One that is normal, and another that is more colourised.
I have two images in the background of my html doc. One positioned top
I have two images wrapped in divs, one with caption while another without: <div
I have two images that I want to be positioned directly on top of
i have two views and one viewcontroller: one contains UIImageView with image, second have
I am developing an android game. in this i have two moving images on

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.