News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

Drag Sprites

Started by kevin, March 29, 2008, 08:22:25 AM

Previous topic - Next topic

kevin

 Drag Sprites With Mouse

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


PlayBASIC Code: [Select]
   ; 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
Login required to view complete source code



Related Examples

  * Drag Rectangles/ GUI Selection





jsoares

#1
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)