Friday, 6 April 2012

Checking The Panel Control

In order to move the panel, we need to check two things;
  • Has the pointer been pressed?
  • Was the pointer over the panel title when this happened?
The first is checked using the getPointerPressed() command, which returns a 1 if the mouse button is pressed in windows or the screen is touched on a tablet or phone.

It reports only new presses and is valid only for the current loop.  After that, it will return 0 even if the button is held down or the screen continually touched after that.

This is done using and if / endif loop and as it needs to be checked regularly, the code is added inside the main loop - between do and loop.

if getPointerPressed() > 0


endif

All code between the if command and the endif command will be run only when the pointer is initially pressed.

Within this loop, we have a second check to see if the pointer was in the right place and since the panel can move, it's best to check against the panel components rather than using screen position.  That way we only need one check for both open and close.

We can find the top of the text using the getTextY() command and the top of the panel using the getSpriteY() command.  We don't care about the pointer X position as we want a click anywhere across the screen to count

What we need to know is if the pointer was between these, which it will be if it is greater than or equal to (>=)  the Text Y position and less than (<) the Sprite Y position.

if getPointerY() >= getTextY( panelText ) and getPointerY() < getSpriteY( panelSprite )

endif

This is called a nested loop, one loop inside another.  Hopefully you can see how indenting code between loops helps you to follow where loops start and end.
The command in the very middle of this nest of loops;

panelOpen = 1 - panelOpen

Changes the state of the panel from open ( 1 ) to closed ( 0 ) and back again.  It is this variable which indicates if the panel SHOULD be open or closed.

Next up is the code to open and close the panel.

No comments:

Post a Comment