Monday 9 April 2012

Object List - Sprite Creation

Since each object on the work area will need a sprite to show it's image, the next function is the one to create them.

Objects are all the same size and the frame used will be based on the object Id,  so we can incorporate these into the function.

function CreateWorkObjectSprite( thisObject , thisX , thisY )
   if WorkObjectIsFree( thisObject ) = 0
      if WorkObjectSprite( thisObject ) = 0 then workObject[ thisObject ].sprite = createSprite( IconImage() )
      thisSprite = WorkObjectSprite( thisObject )
      fixSpriteToScreen( thisSprite , 0 )
      setSpriteSize( thisSprite , IconSize() , - 1)
      setSpriteOffset( thisSprite , iconSize() / 2.0 , iconSize() / 2.0 )
      setSpritePositionByOffset( thisSprite , thisX , thisY )
      setSpriteDepth( thisSprite , DEPTH_WORK - 1 )
      setSpriteAnimation( thisSprite , 64 , 64 , 252 )
      setSpriteFrame( thisSprite , WorkObjectID( thisObject ) )
   endif
endfunction

This first thing this function does it to check that the specified object has been allocated, it does this by calling the WorkObjectIsFree() function and checking the result is zero.

Because the WorkObjectIsFree() function checks if the object is valid for us, we don't need to do this separately (who said these checks were a waste?), and it returns
  • -1 if the object is invalid
  • 0 if the object is in use
  • 1 if the object is free
The function only proceeds if the result is zero - if the object is in use.

It then checks to see if the sprite field is zero, if it is it creates a sprite based on the image loaded earlier. If it is not zero, then the object already has a sprite so that one will be used.

You may recall that when we add an object with the AddWorkObject() function, we pass a sprite parameter.  Checking if a sprite has been used here allows us to reuse that sprite.

Next we copy the sprite reference into a local variable called thisSprite.  This is not actually needed but makes the code more readable, particularly for this Blog where the code doesn't always fit and wraps to a second line.

Then we have a new command, FixSpriteToScreen() set whether the sprite is fixed to the screen or not, in this case a value of zero indicates it is not.

AGK uses two sets of coordinates;
  • Screen coordinates - the ones we have used so far, which are based on the screen size and always have 0,0 at the top left corner of the screen
  • World coordinates - which are independent of the screen.
Our work area will be use world coordinates which allow it to be scrolled and zoomed in and out.  All objects using these coordinates will be moved and zoomed with this "world".

At the moment, both sets are aligned so we don't need to worry too much about this for the moment.

Next we set the size of the sprite, as we've done previously with setSpriteSize(), and then there is another new command, setSpriteOffset().

Each Sprite has an offset value for X and Y, which acts as the centre for rotation and which can also be used to position the sprite.  Normally this is the centre of the sprite so we set it here using iconSize() / 2.0 which will place it in the middle in both directions.

setSpritePositionByOffset() as it's names suggests, positions the sprite using it's offset position - in this case it's middle.  It uses the X and Y coordinates passed to the function as the position.

Next we set the depth to be 1 place in front of the workspace.

We use setSpriteAnimation() exactly the same as we did for the test sprite, it sets the size of the animation to 64 x 64 and indicates 252 frames as before.

And this time we set the sprite frame straight away, using the .objectID field from the array to indicate the frame number in the setSpriteFrame() command.

No comments:

Post a Comment