Monday, 9 April 2012

Object List - Picking Objects

With the pointer routines tidy , we can now continue with picking up our objects.

The first thing to do is decide if we are working from the panel or the work area, which we do within the loop that occurs when the pointer is pressed - we don't care at the moment if the pointer isn't pressed.

Currently that section looks like this
Within the outer loop is the inner loop which checks the pointer Y position, we extend this by inserting the following code before the first endif.

elseif PointDY() < getTextY( panelText() )
   thisSpriteHit = getSpriteHit( PointWX() , PointWY() )
   if thisSpritePicked > 0
      thisObjectPicked = FindWorkObjectBySprite( thisSpritePicked )

   endif

Which is yet another extension of the if/else/endif loop structure.  Elseif adds another comparison which is used if the first one fails.

The first one (which was already there) checked if the pointer was over the object text line.  if it is not, this new one checks if it is above (i.e. the Y value is less than) - therefore it is over the workspace.

The complete loop now looks like this
It's not finished, but I just wanted to be clear about the layout.  This is where those indents start to show their value.

The new code uses the getSpriteHit() command as before, but this time uses the world coordinate function call so that we wont need to change it later.  It stores the result in variable thisSpritePicked.

It then checks if this is no-zero, in which case it starts another loop to retrieve the object.

The first command in this loop gets a value for variable thisObjectPicked by calling a function called FindWorkObjectBySprite(), which doesn't exist yet. This is next on our list.

function FindWorkObjectBySprite( thisSprite )
   for thisObject = 1 to TopWorkObject()
      if WorkObjectSprite( thisObject ) = thisSprite then exit
   next thisObject
   if thisObject > TopWorkObject() then thisObject = 0
endfunction thisObject

This loops through the object list checking the sprite of each one to see if it matches the one hit.  If it does it exists the loop.

If the loop exits without the sprite being found then thisObject will be greater than TopWorkObject() and so will get set to zero - indicating no object found.

This function does not check if the sprite value is non-zero (which was done before the call) so it can be used to find any objects with a zero sprite reference if needed.

Also this function does not check if the object is free, so could theoretically return an object which has been marked as deleted, however since the DelWorkObject() function clears the sprite field when an object is deleted, this wont happen.

It is quicker to do one comparison here (does the sprite match) than to do two (does the sprite match and is the object free).

We can check the function by adding the following lines to just before the do command

thisSpritePicked = 0
thisObjectPicked = 0

and the following lines to the print statements inside the loop

printc( "Sprite Picked: ")
printc( str( thisSpritePicked ))
printc( ", Object Picked: ")
print( str( thisObjectPicked ))

Which should show.

No comments:

Post a Comment