Monday 9 April 2012

Enhanced Dragging

In order to use the new icons just provided, I'm going to update the drag routines we just did.
This next bit of code goes immediately after the setSpriteDepth() command at the end of the getPointerState() loop.

if WorldToScreenY( thisY#)  >= getTextY( panelText() )
   ShowTrashCan( thisSpritePicked )
else
   HideTrashCan()
endif

This checks to see if we are attempting to drag the object onto the Panel, by converting the Y coordinate of the middle of the sprite from it's world position to a screen coordinate and then checks this against the Y position of the top of the Panel Text.

If the object is below that point (i.e Y is greater than the top of the text), it calls a function called ShowTrashCan() and passes the reference of the sprite being dragged to it.

If it is not, it calls a function called HideTrashCan() - in case the last time it checked, it showed the trashcan.

It's not hard to guess what these functions do.

For this to work, firstly we need another place to store a new Sprite.  Again, this will be in the Display UDT, which now looks like this.

type displayType
   physWidth#
   physHeight#
   virtWidth#
   virtHeight#
   iconSize#
   textHeight#
   lastFPS#
   iconImage
   pointDX#
   pointDY#
   pointWX#
   pointWY#
   highSprite
endtype

This needs to be initialised with the other display fields at the start of the program.

display.highSprite = 0

This also needs a return function at the end of the program.

function HighSprite()
endfunction display.highSprite

The HideTrashCan() function is the smallest, so we'll get that out of the way.

function HideTrashCan()
   if HighSprite() > 0
      if getSpriteExists( HighSprite() ) then deleteSprite( HighSprite() )
      display.highSprite = 0
   endif
endfunction

This simply checks if the value in the .highSprite field is greater than zero.  If so it checks if it is assigned to a sprite and if so it deletes that sprite.  It then sets the value to zero.

The ShowTrashCan() function takes a sprite reference as it's parameter and pops a trashcan in front of it.

function ShowTrashCan( thisSprite )
   if thisSprite > 0
      if getSpriteExists ( thisSprite )
         HideTrashCan()
         display.highSprite = cloneSprite( thisSprite )
         setSpriteFrame( HighSprite() , 252 )
         setSpriteDepth( HighSprite() , getSpriteDepth( HighSprite() ) - 1)
      endif
   endif
endfunction

It checks to see if the parameter passed is non-zero.  If so, it checks that a sprite exists with that reference.

If the sprite exists, it then calls the HideTrashCan() function to delete any sprite that may already exist and then clones the sprite passed using the cloneSprite() command, storing the reference of the clone in the .highsprite field.

Using cloneSprite(), duplicates all the parameters of an existing sprite such as position, offset, size, animation and depth and is much easier than creating a new sprite and copying the parameters individually.

It then changes the frame of the clone to 252, which is the trashcan, and sets its depth to one on front of what it was - positioning the trashcan in front of the sprite originally passed.

Now if you drag an object over the panel or it's title, you get this.
These new functions look like this in the program.

No comments:

Post a Comment