Friday 6 April 2012

Moving the Panel

The variable panelOpen indicates if the panel should be open ( 1 ) or closed ( 0 ), the next routine checks to see if it where it is supposed to be.

This uses the extended version of the if / endif loop which is the if / else / endif loop.

if panelOpen = 1

else

endif

Here there are two loops, but only one will ever run.  If the variable PanelOpen contains a 1, the first loop will run, otherwise the second will.

Within each of these loops, we have a second check to see if the panel is where its supposed to be.

If the panel is supposed to be open ( first loop ) then the panel sprite should be high enough to be able to see the whole panel, that is the Y position of the sprite should be the screen height minus the panel height.  If not, we want the inner loop to run;

if getSpriteY( panelSprite ) > ( physHeight# -  panelHeight# )

endif

If the panel is supposed to be closed (second loop) then the panel sprite should be off the bottom of the screen, that is the Y position of the sprite should be the screen height.  If it's not, we want the inner loop to run;

if getSpriteY( panelSprite ) < physHeight#

endif

In each case, if the sprite is not there, both it and the title text should be moved.

In the first loop it the objects should be moved up - by subtracting 1 from their Y position;

setSpriteY( panelSprite , getSpriteY( panelSprite ) - 1 )
setTextY( panelText , getTextY( panelText ) - 1 )

In the second loop they should be moved down - by adding 1 to their Y position;

setSpriteY( panelSprite , getSpriteY( panelSprite ) + 1 )
setTextY( panelText , getTextY( panelText ) + 1 )

The whole loop looks like this;
With the panel closed, clicking the panel title will start to open it, the panel will move up until it is in position then it will stop.

With it open, clicking the title will start to close it, it will move down until it is off screen then it will stop.

While it is moving, clicking the title will make it change direction.

There have been a few changes, so as a check point - here is a complete listing of the program so far.

It is unformatted so should copy and and paste straight into the editor and compile without error.

// 6 April 2012 - tabs converted to 3 spaces
#constant PANEL_ROWS   3
#constant PANEL_COLS   4

physWidth# = getDeviceWidth()
physHeight# = getDeviceHeight()
setVirtualResolution( physWidth# , physHeight# )
virtWidth# = getVirtualWidth()
virtHeight# = getVirtualHeight()

iconSize# = physWidth# / PANEL_COLS
panelHeight# = iconSize# * PANEL_ROWS

panelOpen = 0

panelSprite = createSprite(0)
setSpriteSize( panelSprite , physWidth# , panelHeight# )
setSpritePosition( panelSprite , 0 , physHeight# )

panelText = createText( "Objects" )
textHeight# = physHeight# / 20.0
setTextSize( panelText , textHeight# )
setTextAlignMent( panelText , 1)
setTextPosition( panelText , physWidth# / 2.0 , physHeight# - textHeight# )

imageRef = loadImage( "finger.png" )
spriteRef = createSprite( imageRef )

do
   if getPointerPressed() > 0
      if getPointerY() >= getTextY( panelText ) and getPointerY() < getSpriteY( panelSprite )
         panelOpen = 1 - panelOpen
      endif
   endif

   if panelOpen = 1
      if getSpriteY( panelSprite ) > ( physHeight# - panelHeight# )
         setSpriteY( panelSprite , getSpriteY( panelSprite ) - 1 )
         setTextY( panelText , getTextY( panelText ) - 1 )
      endif
   else
      if getSpriteY( panelSprite ) < physHeight#
         setSpriteY( panelSprite , getSpriteY( panelSprite ) + 1 )
         setTextY( panelText , getTextY( panelText ) + 1 )
      endif
   endif

   setSpritePosition( spriteRef , getPointerX() , getPointerY() )
   printc( "Physical: " )
   print( str( physWidth# ) + " x " + str( physHeight# ) )
   printc( "Pointer: " )
   print( str( getPointerX() ) + " x " + str( getPointerY() ) )
   sync()
loop
//

No comments:

Post a Comment