UnderwareDESIGN

PlayBASIC => Resources => Source Codes => Topic started by: kevin on March 29, 2008, 08:22:25 AM

Title: Drag Sprites
Post by: kevin on March 29, 2008, 08:22:25 AM
 Drag Sprites With Mouse

This example creates a simple sprite scene and then lets you drag sprites around the sprite with the mouse.


[pbcode]

   ; Load the windows Arial font into font #1 (the default font)
   ; with a 32 point size
   LoadFont "arial",1,32,0


   ; create an array to hold our created images
   Dim MyImages(10)


   ; Create the 10 images
   For lp=1 to 10
      MyImages(lp)=MakeCard(100,200,"Image"+str$(lp))
   next




   MaxSprites=30
   ; create A bunch of spirtes that use any
   ; of our previouslp created images

   ; create 30 sprites
      For lp=1 to MaxSprites

         ; initialize this sprite
         CreateSprite lp


         ; assign this sprite any image from our cache
         ; created above.
         img=RndRange(1,GetArrayElements(MyImages(),1))
         SpriteIMage lp,Img

         ; Position upon the screen
         PositionSprite lp,Rnd(GetScreenWidth()),Rnd(GetScreenHeight())

         ; CenterSprite hhandles
         CenterSpriteHandle lp

         ; Set sprite to a Rotate draw mode
         SpriteDrawMode lp,2

         ; Set sprite collision mode to rotated rect.
         SpriteCollisionMode lp,1  

      next


   // Defined Selection modes
   Constant MouseSelectionMode_None         =0
   Constant MouseSelectionMode_Dragging   =1
   

   // Delcare a type to hold the selection values together
   Type tMouseSelect
         Mode

         Sprite      ; Index of Sprite being dragged
         OffsetX      ; Offset Handle on the dragged sprite
         OffsetY      ; Offset Handle on the dragged sprite
      
   EndType
   
   Dim Selection as tMouseSelect
   
   // Init the Selction mode to NONE
   Selection.Mode=MouseSelectionMode_None




   SetFps 60

   Do

      ; Clear the Screen to a dark green colour
      Cls rgb(1,80,0)
   


      ; Run through and spin the sprites
      
      HitSprite=0

         For Spr=1 to MaxSprites
         
               ; reset the sprites draw mode   
               SpriteDrawMode Spr,2
      
         next



         ; Process Mouse Selection and Dragging

         ;Get the mouses Current X & Y position
         MX=mouseX()
         My=mouseY()
         
         Select Selection.Mode
            
               ; ------------------------------------------------------------------------
               case MouseSelectionMode_None
               ; ------------------------------------------------------------------------

                  // Check if the mouse button was pressed ?
                  if LeftMousebutton()=true
   
                     ; If yes, now we check if the mouse ius over a sprite ?
                     HitSprite=ClickedSprite(Mx,MY)
                     if HitSprite>0
               

                        // user click a sprite, so lets tag it as the one we'll drag
                        Selection.Sprite=HitSprite
                        Selection.OffsetX=GetSpriteX(HitSprite)-mx
                        Selection.OffsetY=GetSpriteY(HitSprite)-my

                        // Set the selction mode to drag mode
                        Selection.Mode=MouseSelectionMode_Dragging
                     endif
                  endif               
                  
                  
               ; ------------------------------------------------------------------------
               case MouseSelectionMode_Dragging  // Mouse Dragging a sprite                           
               ; ------------------------------------------------------------------------

                  ; Is the use still holding the mouse button ?                              
                  if LeftMousebutton()=true

                        ; If yes, we position the dragged sprite relative to the mouse pointer
                        ThisSprite=Selection.Sprite
                        xpos=Selection.OffsetX+mx
                        ypos=Selection.Offsety+my

                        ; Set the Sprites POsition
                        PositionSprite ThisSprite,Xpos,Ypos                     

                           
                  else

                        ; If the mouse was released, then we restore the mouse
                        ; selection mode back to NONE.  So we can select again
                        Selection.Mode               =MouseSelectionMode_None                           
                  endif
            
         EndSelect



      ; DRaw All of the created sprites to the screen
      DrawAllSprites   


      ; refresh the screen so we can see the changes      
      Sync
   loop



Function ClickedSprite(Xpos,Ypos)
   HitSprite=0
   Spr=GetFirstSprite()
   While Spr>0
      If PointHitSprite(xpos,ypos,Spr)
         HitSprite=Spr         
      endif
      Spr=GetNextSprite(spr)
   endwhile
EndFunction HitSprite




Function MakeCard(Width,Height,CardName$)
      ; create a blank image with the requird width, height
      img=NewImage(width,height)
   
      ; tell pB to start drawing to it
      RenderToIMage img   

      ; Fill the Card in with a colour backdrop
      c1=rndRgb()
      c2=rndRgb()

      ; Draw the shaded box as the backdrop
      ShadeBox 0,0,Width,height,c1,c2,c1,c2

      centertext Width/2,Height/2,CardName$   
   
      rendertoscreen
   
      PrepareFXimage Img

EndFunction Img

[/pbcode]


Related Examples

  * Drag Rectangles/ GUI Selection (http://www.underwaredesign.com/forums/index.php?topic=3647.0)




Title: Re: Thanks for the help everyone
Post by: jsoares on March 29, 2008, 06:58:15 PM
Thanks to all those who replied to help me load playing cards and manipulate them onscreen. I've succeeded in loading an image, now I'll work on how to mouse click and move it.

Best,

John Soares
(email address removed)