Dragging an image

All your problems with Audio, Video and Images.

Dragging an image

Postby hansmoolman » Sat Mar 05, 2011 10:20 pm

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: Dragging an image

Postby lockon_stratos » Wed Mar 16, 2011 8:29 am

hi hansmoolman,

try this code, i just modified some of your code in MainGamePanel class. let me know if this code works, i have not yet tested it that's why im not sure if this code will work, i just based this code on my previous project which also has dragging capability.

Also, make sure that the width and height of the barrier is 32 to make sure that there is no issue on detecting the collision of x and y pointer and your barriers position.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class MainGamePanel extends SurfaceView implements SurfaceHolder.Callback, SensorEventListener {
  2.    
  3.    // Initialising the Main Thread
  4.    private MainThread thread;
  5.    
  6.    // Initialising the Barrier
  7.    private Barrier barrier[] = new Barrier[3];
  8.  
  9.    // The Main Game Panel
  10.    public MainGamePanel(Context context) {
  11.       super(context);
  12.      
  13.       // Adding a call-back (this) to the surfaceHolder to intercept events
  14.       getHolder().addCallback(this);
  15.      
  16.            
  17.       // The starting coordinates of the Barrier
  18.       int x = 30;
  19.       int y = 270;
  20.       barrier[0] = new Barrier(BitmapFactory.decodeResource(getResources(), R.drawable.blue_barrier), x, y);
  21.       barrier[1] = new Barrier(BitmapFactory.decodeResource(getResources(), R.drawable.green_barrier), x + 15, y);
  22.       barrier[2] = new Barrier(BitmapFactory.decodeResource(getResources(), R.drawable.pink_barrier), x + 30, y);  
  23.      
  24.       // Create the Game Loop Thread
  25.       thread = new MainThread(getHolder(), this);
  26.      
  27.       // Make the GamePanel focusable so it can handle events
  28.       setFocusable(true);  
  29.    }
  30.    
  31.  
  32.    // Handles the touch events
  33.    public boolean onTouchEvent(MotionEvent event)
  34.    {
  35.       int eventAction = event.getAction();
  36.      
  37.       int x = (int)event.getX();
  38.       int y = (int)event.getY();
  39.         int index = 0;
  40.      
  41.       switch (eventAction)
  42.       {
  43.       // Touch down so check if finger is on Barrier
  44.       case MotionEvent.ACTION_DOWN:
  45.  
  46.         if (x > barrier[0].getX() && x < barrier[0].getX() + 32
  47.                      && y > barrier[0].getX() && y < barrier[0].getY() + 32)
  48.                {
  49.                   index = 0;
  50.                }
  51.         if (x > barrier[1].getX() && x < barrier[1].getX() + 32
  52.                      && y > barrier[1].getX() && y < barrier[1].getY() + 32)
  53.                {
  54.                   index = 1;
  55.                }
  56.         if (x > barrier[2].getX() && x < barrier[2].getX() + 32
  57.                      && y > barrier[2].getX() && y < barrier[2].getY() + 32)
  58.                {
  59.                   index = 2;
  60.                }
  61.          break;
  62.      
  63.          
  64.       case MotionEvent.ACTION_MOVE: // Touch-drag with the Barrier
  65.       // Move the Barrier the same as the finger
  66.         barrier[index].setX(x);
  67.         barrier[index].setY(y);
  68.       case MotionEvent.ACTION_UP:
  69.          // Finger no longer on Barrier
  70.                                                 // ******** THESE LINES CRASH THE PROGRAM WHEN FINGER RELEASED
  71.          barrier[1].setTouched(false);
  72.          barrier[2].setTouched(false);
  73.          barrier[3].setTouched(false);
  74.          break;
  75.       }
  76.       return true;
  77.    }
  78.    
  79.  
  80.    // Render - Draws the Game Item Bitmaps to the screen
  81.    public void render(Canvas canvas)
  82.    {
  83.        // Set the background to white
  84.       canvas.drawColor(Color.WHITE);
  85.                  
  86.       barrier[0].draw(canvas);
  87.       barrier[1].draw(canvas);
  88.       barrier[2].draw(canvas);  
  89.    }
  90.    
  91.    // Update
  92.    // This is the Game's update method
  93.    // It iterates through all the Objects and calls their update() methods (if they have one)
  94.    public void update()
  95.    {
  96.                         // Nothing here for the barrier
  97.    } // end update
  98.  
  99.    
  100.    public void surfaceCreated(SurfaceHolder holder)
  101.    {
  102.       // The surface is created - Start the Game Loop
  103.       thread.setRunning(true);
  104.       thread.start();
  105.    }
  106.  
  107. }
  108.  
Parsed in 0.044 seconds, using GeSHi 1.0.8.4


Best,
Lets go to snipe!
User avatar
lockon_stratos
Freshman
Freshman
 
Posts: 7
Joined: Wed Mar 16, 2011 5:00 am

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 15 guests