Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to add an OnClick event for an Android custom View
    primarykey
    data
    text
    <p>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. </p> <p>My problem is I cannot seem to add an onclick/ontouch event and get it to work.</p> <p>I'm not implementing it properly, can anyone help please?</p> <p>I have included the 3 classes below. Greg is the man and the ball is named ball :)</p> <p>TestAnimationActivity.java</p> <pre><code> 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)); } } </code></pre> <p>Sprite.java</p> <pre><code>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 &lt;= 10 &amp;&amp; pointerGoingLeft) { pointCoordX = pointCoordX + ballSpeed; pointerGoingRight = true; pointerGoingLeft = false; } else if (x &gt;= 410 &amp;&amp; pointerGoingRight) { pointCoordX = pointCoordX - ballSpeed; pointerGoingLeft = true; pointerGoingRight = false; } else if (pointerGoingRight) pointCoordX = pointCoordX + ballSpeed; else pointCoordX = pointCoordX - ballSpeed; if(MyAnimationView.ballDropping == true) { while (pointCoordY&lt;gregCoordY) dropBall(); } } public static void moveGreg(int x) { if (x &lt;= 10 &amp;&amp; goingLeft) { gregCoordX = gregCoordX + count; goingRight = true; goingLeft = false; } else if (x &gt;= 410 &amp;&amp; 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(); } } </code></pre> <p>MyAnimationView.java</p> <pre><code>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(); } } </code></pre> <p>Thanks in advance, I've been stuck for days!</p> <p>Ben</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload