Friday, August 17, 2012

Emo Framework Tutorial Example 2 - Click and Drag a Certain sprite

Here's another example of moving a certain sprite that you desired. Imagine like a moving a chess piece in a board.


 class PickupSprite {  
      sprite1 = null;  
      sprite2 = null;  
      selected_sprite = null;  
      function onload() {  
           print("Loading PickupSprit");       
      }  
      function onDispose() {  
           print("Exiting...");  
      }  
      function onMotionEvent(mevent) {  
           local id = mevent.getPointerId();  
           local action = mevent.getAction();  
           if(!sprite1) {  
                sprite1 = emo.Sprite("private.png");  
                sprite1.load();  
           }   
           if (!sprite2 ) {  
                sprite2 = emo.Sprite("captain.png");  
                sprite2.load();  
                sprite2.move(100,100);  
           }  
           if(action == MOTION_EVENT_ACTION_DOWN || action == MOTION_EVENT_ACTION_POINTER_DOWN ) {  
                print("Clicked at x: " + mevent.getX() + " y: " + mevent.getY());  
                if(sprite1.contains(mevent.getX(), mevent.getY()) ) {  
                     selected_sprite = sprite1;  
                }  
                else if(sprite2.contains(mevent.getX(), mevent.getY()) ) {  
                     selected_sprite = sprite2;  
                }  
           } else if(action == MOTION_EVENT_ACTION_UP ||   
                          action == MOTION_EVENT_ACTION_POINTER_UP ||  
           action == MOTION_EVENT_ACTION_CANCEL ||  
           action == MOTION_EVENT_ACTION_OUTSIDE ) {  
       print("Released");  
                selected_sprite = null;       
           }  
           if ( selected_sprite ) {  
                print(selected_sprite.getName());  
                handleTouch(selected_sprite, mevent);  
           }  
      }  
      function handleTouch(sprite, mevent) {  
     local action = mevent.getAction();  
     if (action == MOTION_EVENT_ACTION_DOWN || action == MOTION_EVENT_ACTION_POINTER_DOWN) {  
     } else if (action == MOTION_EVENT_ACTION_MOVE) {  
       sprite.moveCenter(mevent.getX(), mevent.getY());  
     } else if (action == MOTION_EVENT_ACTION_UP ||  
           action == MOTION_EVENT_ACTION_CANCEL ||  
           action == MOTION_EVENT_ACTION_OUTSIDE ||  
           action == MOTION_EVENT_ACTION_POINTER_UP) {  
     }  
      }  
 }  

No comments: