Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid - Moving a single image
    primarykey
    data
    text
    <p>Can anyone help please.</p> <p>Im writing a small Android game where the player is able to select a "barrier" and drag it accross the screen with their finger. I have the barriers drawn on the screen and I am able to drag it accross the screen. My problem however is when I add more than 1 barrier, eg 3 barriers, and drag a barrier accross the screen, they all drag and they all drag to the same position. That is to say they all lie on top of each other, making it look as though there is only 1 barrier.</p> <p>Here is my code, can anyone please tell me where I am going wrong/explain where I am going wrong.</p> <pre><code>public class MainGamePanel extends SurfaceView implements SurfaceHolder.Callback, SensorEventListener { // Initialising the Barrier private Barrier barrier[] = new Barrier[3]; // The Main Game Panel public MainGamePanel(Context context) { super(context); // Adding a call-back (this) to the surfaceHolder to intercept events getHolder().addCallback(this); // Creating the Game items // The starting coordinates of the Barrier int x = 30; int y = 270; barrier[0] = new Barrier(BitmapFactory.decodeResource(getResources(), R.drawable.blue_barrier), x, y); barrier[1] = new Barrier(BitmapFactory.decodeResource(getResources(), R.drawable.green_barrier), x + 15, y); barrier[2] = new Barrier(BitmapFactory.decodeResource(getResources(), R.drawable.pink_barrier), x + 30, y); // Create the Game Loop Thread thread = new MainThread(getHolder(), this); // Make the GamePanel focusable so it can handle events setFocusable(true); } // Handles the touch events public boolean onTouchEvent(MotionEvent event) { int eventAction = event.getAction(); int x = (int)event.getX(); int y = (int)event.getY(); switch (eventAction) { // Touch down so check if finger is on Barrier case MotionEvent.ACTION_DOWN: if (x &gt; barrier[0].getX() &amp;&amp; x &lt; barrier[0].getX() + 8 &amp;&amp; y &gt; barrier[0].getX() &amp;&amp; y &lt; barrier[0].getY() + 8) { barrier[0].isTouched(); } else if (x &gt; barrier[1].getX() &amp;&amp; x &lt; barrier[1].getX() + 8 &amp;&amp; y &gt; barrier[1].getX() &amp;&amp; y &lt; barrier[1].getY() + 8) { barrier[1].isTouched(); } else if (x &gt; barrier[2].getX() &amp;&amp; x &lt; barrier[2].getX() + 8 &amp;&amp; y &gt; barrier[2].getX() &amp;&amp; y &lt; barrier[2].getY() + 8) { barrier[2].isTouched(); } break; // Touch-drag with the Barrier case MotionEvent.ACTION_MOVE: // Move the Barrier the same as the finger for (int i = 0; i &lt; barrier.length; i++) { if (barrier[i] == barrier[0]) { barrier[0].setX(x); barrier[0].setY(y); } // end if else if (barrier[i] == barrier[1]) { barrier[1].setX(x); barrier[1].setY(y); } else if (barrier[i] == barrier[2]) { barrier[2].setX(x); barrier[2].setY(y); } // end else if } // end for break; case MotionEvent.ACTION_UP: // Finger no longer on Barrier - Do Nothing break; } return true; } // Render - Draws the Game Item Bitmaps to the screen public void render(Canvas canvas) { // Set the background to white canvas.drawColor(Color.WHITE); barrier[0].draw(canvas); barrier[1].draw(canvas); barrier[2].draw(canvas); } // Update // This is the Game's update method // It iterates through all the Objects and calls their update() methods (if they have one) public void update() { } // end update </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    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