Drag and Drop Images

Problems with Canvas, OpenGL, etc...

Drag and Drop Images

Postby hansmoolman » Sun Mar 06, 2011 12:17 pm

This is a repost I have from another section on this website but I figured it would be more suited to this section.

Can anyone please help. I am currently writing a Android game where the player should be able to touch and drag a "Barrier" accross the screen. I can drag the barrier (bitmap) accross the screen, but the trouble is when I add more than one barrier and drag one, they all drag together and to the same location. I simply want to be able to click and drag one barrier at a time.

I have included my code for the MainGamePanel as well as for my barrier class. Can anyone see where I am going wrong and point me in the right direction please. Sorry for all the code I included, but I thought Id include what I have to be thourough.

My MainGamePanel class, runs a Thread

Code: Select all
public class MainGamePanel extends SurfaceView implements SurfaceHolder.Callback, SensorEventListener {
   
   // Initialising the Main Thread
   private MainThread thread;
   
   // 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);
     
           
      // 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:
         for (int i = 0; i < barrier.length; i++)
         {
            if (barrier[i] == barrier[0])
            {
               if (x > barrier[0].getX() && x < barrier[0].getX() + 32
                     && y > barrier[0].getX() && y < barrier[0].getY() + 32)
               {
                 
               }
            } // end if
            else if (barrier[i] == barrier[1])
            {
               if (x > barrier[1].getX() && x < barrier[1].getX() + 32
                     && y > barrier[1].getX() && y < barrier[1].getY() + 32)
               {
                 
               }
            //} // end else if
            else if (barrier[i] == barrier[2])
            {
               if (x > barrier[2].getX() && x < barrier[2].getX() + 32
                     && y > barrier[2].getX() && y < barrier[2].getY() + 32)
               {
                 
               }
            //} // end else if
         //} // end for
         break;
     
         
      case MotionEvent.ACTION_MOVE: // Touch-drag with the Barrier
      // Move the Barrier the same as the finger
     
         for (int i = 0; i < 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
                                                // ******** THESE LINES CRASH THE PROGRAM WHEN FINGER RELEASED
         barrier[1].setTouched(false);
         barrier[2].setTouched(false);
         barrier[3].setTouched(false);
         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()
   {
                        // Nothing here for the barrier
   } // end update

   
   public void surfaceCreated(SurfaceHolder holder)
   {
      // The surface is created - Start the Game Loop
      thread.setRunning(true);
      thread.start();
   }
}


And my Barrier Class

Code: Select all
public class Barrier extends GameObject {
   
   // The Bitmap image of the Barrier
   private Bitmap bitmap;
   
   // The Barriers X and Y coordinates
   private int x, y;
   
   // The Barrier is touched
   private boolean touched;
   
   // My PhysiX Class
   private PhysiX physiX;   
   
   // Constructor
   public Barrier(Bitmap bitmap, int x, int y)
   {
      this.bitmap = bitmap;
      this.x = x;
      this.y = y;
      this.physiX = new PhysiX();
   }
   
   // Getters and Setters
   public Bitmap getBitmap()
   {
      return bitmap;
   }
   public void setBitmap(Bitmap bitmap)
   {
      this.bitmap = bitmap;
   }
   
   public int getX()
   {
      return x;
   }
   public void setX(int x)
   {
      this.x = x;
   }
   
   public int getY()
   {
      return y;
   }
   public void setY(int y)
   {
      this.y = y;
   }
   
   public PhysiX getPhysiX()
   {
      return physiX;
   }
   public void setPhysiX(PhysiX physiX)
   {
      this.physiX = physiX;
   }
   
   public boolean isTouched()
   {
      return touched;
   }
   public void setTouched(boolean touched)
   {
      this.touched = touched;
   }
   
   // Draw method
   public void draw(Canvas canvas)
   {
      canvas.drawBitmap(bitmap, x - (bitmap.getWidth() / 2), y - (bitmap.getHeight() / 2), null);
   }
   
   // Update method
   public void update()
   {
      if (touched)
      {
         x += (physiX.getXv() * physiX.getxDirection());
         y += (physiX.getYv() * physiX.getyDirection());
      }
   }
   
   /**Handles the MotionEvent.ACTION_DOWN event.
    * If the event happens on the
    * bitmap surface then the touched state is set to true otherwise to false
    */
   public void handleActionDown(int eventX, int eventY)
   {
      if (eventX >= (x - bitmap.getWidth() / 2) && (eventX <= (x + bitmap.getWidth() / 2)))
      {
         if ( eventY >= (y - bitmap.getHeight() / 2) && (eventY <= (y - bitmap.getHeight() / 2)))
         {
            // The Barrier is touched
            setTouched(true);
         }
         else
            setTouched(false);
      }
      else
      {
         setTouched(false);
      } // end else
   }
}
hansmoolman
Freshman
Freshman
 
Posts: 2
Joined: Sat Mar 05, 2011 9:58 pm

Top

Re: Drag and Drop Images

Postby raj_J2ME » Thu Mar 10, 2011 1:22 pm

You must take the array for the X and Y values for the each barrier. And then on touch event you have to assign the values to the array of the barrier which was touched or dragged. I am able to view the code in which you have assign the same X and Y values to each of them.
Thanks with Regards,
Raj - The Kernel
raj_J2ME
Master Developer
Master Developer
 
Posts: 311
Joined: Tue Feb 15, 2011 7:40 am
Location: The Capital of India,

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: Google [Bot] and 2 guests