Advertisement
Guest User

Orb

a guest
Nov 24th, 2011
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.66 KB | None | 0 0
  1. package biz.hireholly.tutorial.models;
  2.  
  3. import android.graphics.Bitmap;
  4. import android.graphics.Canvas;
  5.  
  6. public class Orb {
  7.  
  8.     private Bitmap bitmap;  //image
  9.     private int x;  //x coord
  10.     private int y;  //y coord
  11.     private boolean touched; //if orb is touched/picked up
  12.     //just to make things simpler to read
  13.     int halfBmpX = bitmap.getWidth() /2;
  14.     int halfBmpY = bitmap.getHeight() /2;
  15.            
  16.     public Orb(Bitmap bmp, int x, int y){
  17.         this.bitmap = bmp;
  18.         this.x = x;
  19.         this.y = y;
  20.     }
  21.    
  22.     public Bitmap getBitmap(){
  23.         return bitmap;
  24.     }
  25.     public void setBitmap(Bitmap bmp){
  26.         this.bitmap = bmp;
  27.     }
  28.     public int getX(){
  29.         return x;
  30.     }
  31.     public int getY(){
  32.         return y;
  33.     }
  34.     public void setX(int x){
  35.         this.x = x;
  36.     }
  37.     public void setY(int y){
  38.         this.y = y;
  39.     }
  40.    
  41.     public boolean isTouched(){
  42.         return touched;
  43.     }
  44.     public void setTouched(boolean touched){
  45.         this.touched = true;
  46.     }
  47.    
  48.     /**
  49.      * takes the bitmap it was instantiated with.
  50.      * Draws it to the canvas at the coordinates the orb is at in that moment.
  51.      * @param canvas
  52.      */
  53.     public void draw(Canvas canvas){
  54.         canvas.drawBitmap(bitmap, x-halfBmpX, y-halfBmpY, null );
  55.     }
  56.    
  57.     /**
  58.      * check if the events coordinates are inside the orb's bitmap.
  59.      * Used when the user presses down to see if they touched the orb
  60.      * @param eX
  61.      * @param eY
  62.      */
  63.     public void handleActionDown(int eX, int eY){
  64.         if(eX >= (x - halfBmpX) && (eX <= (x + halfBmpX))){ //checks if within width
  65.             if (eY >= (y - halfBmpY) && (eY <= (y + halfBmpY))){ //checks if within height
  66.                 //orb touched
  67.                 setTouched(true);
  68.             }
  69.             else{
  70.                 setTouched(false);
  71.             }
  72.         }
  73.         else{
  74.             setTouched(false);
  75.         }
  76.     }
  77. }
  78.  
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement