Monday, 9 April 2012

Object List - Picking Objects Up

We now know which Sprite was picked, which object that belongs to and where the pointer was when this happened, we now have enough information to actually pick up the object.

There is one more piece of information - actually two pieces of information  - we need to work out to continue.

We need to know how far from the centre of the sprite the pointer was when it picked the sprite.  This tells us which part of the sprite the pointer picked up.

We need two more variables, which will be called thisPickOffsetX# and thisPickOffsetY#.  These are set to zero with the others just before the do command.

thisPickOffsetX# = 0
thisPickOffsetY# = 0

Just after thisObjectPicked =, we start yet another if/endif loop, which checks that the object picked was not zero.

if thisObjectPicked > 0
   thisPickOffsetX# = PointWX() - getSpriteXByOffset( thisSpritePicked )
   thisPickOffsetY# = PointWY() - getSpriteYByOffset( thisSpritePicked )
endif

Inside this loop we calculate the offset values.  You can do this either way around, pointer - sprite as I have here or sprite - pointer, as long as the command that repositions the sprite matches.

We use getSpriteXByOffset() and getSpriteYByOffset as we will be repositioning by offset.  You can just as easily use getSpriteX() and getSpriteY() as long as you reposition by X and Y.

The whole loop now looks like this
All we have to do now is to move it, but that is handled in another loop.

The entire loop shown above is what happens when the pointer is first pressed.

There are two other loops which come after this, one which handles what happens when the pointer is released - checked using getPointerReleased() and one for the time in between - checked using getPointerState().

These come before the last endif above.  So the outer structure looks like this;

if getPointerPressed() > 0
   // this is the code we have now shown above
elseif getPointerReleased() > 0
   // What you do when the pointer is released
elseif getPointerState() > 0
   // What you do while the pointer is being held down
endif

You can also have a fourth one for what happens if the pointer is not pressed and was not just released, but it is unusual to do anything in that case.

No comments:

Post a Comment