Monday 9 April 2012

Enhanced Dragging 2

Now we have a function that can identify the object under the one being dragged, we can use this to show the user which object will get dropped onto.  This is almost identical to how we used the Trashcan.

In fact the function we use - ShowObjectUnder(), is a copy of the ShowTrashCan() function with a different frame number - we will combine these two shortly.

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

The way we call the function is slightly different, since in this case the one we want to highlight is at the bottom, not the one being dragged.

After the place where ShowTrashCan() is called in the main loop, change the lines.

else
   HideTrashCan()
endif

to

else
   thisObjectUnder = WorkObjectIsOver( thisObjectPicked )
   if thisObjectUnder > 0
      ShowObjectUnder( WorkObjectSprite( thisObjectUnder )  )
   else
      HideTrashCan()
   endif
endif

So if the Trashcan is not shown, it calls the WorkObjectIsOver() function and stores the result in thisObjectUnder.

If thisObjectUnder is greater than zero - ie there is an object under the one being dragged, then it gets it sprite reference using WorkObjectSprite() and uses it as a parameter for ShowObjectUnder().

The way this is done - with one function call inside the parameter bracket of another is called a compound statement and eliminates the need to use a variable to store the result of one and pass it to another.

The further we go in the Blog, the more advanced the methods will be.

We could compounded this further with a statement such as

ShowObjectUnder(WorkObjectSprite(WorkObjectIsOver(thisObjectPicked)))

But we don't want to push it too much just yet and besides it's starting to sound like a tongue twister or a line from AirPlane.

And after all that babble, here's what you get.
You can't do too much with it as the depths are all shot because we don't put the object down yet.

No comments:

Post a Comment