Monday 9 April 2012

Object List - Over Object

The act of combining two objects is going to require one object to be dropped onto another.
This will require knowing if one object is over another.  Which will need another function - this time called WorkObjectIsOver().

Before we get to it, lets change some lines in our print statements to test it works.  Change the line

print( str( thisObjectPicked ) )

to 

printc( str( thisObjectPicked ))

Which is just changing print to printc in case you didn't see it.  Now immediately after that, add the lines

printc( ", Over : " )
print( str( WorkObjectIsOver( thisObjectPicked ) ))

Which prints the result from the new function.

The new function looks like this.

function WorkObjectIsOver( thisWorkObject )
   thisSprite = WorkObjectSprite( thisWorkObject )
   if thisSprite > 0
      thisX# = getSpriteXByOffset( thisSprite )
      thisY# = getSpriteYByOffset( thisSprite )
      for thisObject = 1 to TopWorkObject()
         if thisObject <> thisWorkObject
            thisSprite = WorkObjectSprite( thisObject )
            if thisSprite > 0 and 
               diffX# = abs( getSpriteXByOffset( thisSprite ) - thisX# )
               diffY# = abs( getSpriteYByOffset( thisSprite ) - thisY# )
               if diffX# < iconSize() and diffY# < iconSize() then exit
            endif
         endif
      next thisObject
      if thisObject > TopWorkObject() then thisObject = 0
   else
      thisObject = -1
   endif
endfunction thisObject

Which will never fit across the blog page, so here's the cap of it for the layout.
To prevent confusion, this uses thisWorkObject as the variable which starts with the passed parameter - the object being moved - so that the loop can use thisObject which is the return value.

First up it makes sure the object passed is valid and has a sprite by calling WorkObjectSprite() and storing the results in the variable thisSprite.

If thisSprite is greater than zero, it then proceeds, otherwise if sets thisObject to -1 to indicate an error.

When proceeding, it then gets the centre of thisSprite and stores the coordinates in thisX# and thisY#

It then loops through the Work Object List;
  1. First it makes sure the objects are different.
  2. It reused the variable thisSprite to get the sprite of the loop object
  3. If the sprite is valid, it then gets the difference between the centres of the two
  4. If the difference is less than the icon width in both directions, it exits the loop
Only if it exits the loop prematurely will thisObject contain a valid reference, otherwise it is set to zero.

This finds the first object in the list that is in any way under the one being dragged.

We already have the test code in place so compile and run.
It only just fits on the screen, but you can see the results on the far right.

No comments:

Post a Comment