We have the pointer fields in the display UDT
pointDX# pointDY# pointWX# pointWY#and we have the test variables, which keep track of what has been selected.
thisSpritePicked = 0 thisObjectPicked = 0 thisPickOffsetX# = 0 thisPickOffsetY# = 0It would be better if these were all in the same place.
We could add the variables to the display global, but that is really intended for display related information.
Also, in the case of a phone or tablet, unless it has a separate mouse and pointer feature, the program doesn't know where the pointer is until the user touches the screen.
A better solution would be to use a separate global for all the touch related information.
For this, a new UDT is created called touchType.
type touchType pointDX# pointDY# pointWX# pointWY# spritePicked objectPicked pickOffsetX# pickOffsetY# endtypeThe "picked" code previously at the start of the program is now removed and instead we use fields in a new global variable called touch which is set up in the Initialise() function.
global touch as touchType touch.pointDX# = 0.0 touch.pointDY# = 0.0 touch.pointWX# = 0.0 touch.pointWY# = 0.0 touch.spritePicked = 0 touch.objectPicked = 0 touch.pickOffsetX# = 0.0 touch.pickOffsetY# = 0.0Now all references to the touch fields in the display UDT are changed to use the touch UDT.
The following code in LoopStart().
display.pointDX# = getPointerX() display.pointDY# = getPointerY() display.pointWX# = ScreentoWorldX(PointDX()) display.pointWY# = ScreentoWorldY(PointDY())Now becomes
touch.pointDX# = getPointerX() touch.pointDY# = getPointerY() touch.pointWX# = ScreentoWorldX(PointDX()) touch.pointWY# = ScreentoWorldY(PointDY())And the existing return functions
function PointDX() endfunction display.pointDX# function PointDY() endfunction display.pointDY# function PointWX() endfunction display.pointWX# function PointWY() endfunction display.pointWY#Retain their names, but now return values from the touch global variable, rather than the display one.
function PointDX() endfunction touch.pointDX# function PointDY() endfunction touch.pointDY# function PointWX() endfunction touch.pointWX# function PointWY() endfunction touch.pointWY#Hopefully this demonstrates the benefit of using such functions. All existing calls remain unaltered, while the workings under the hood - so to speak - are re-jigged.
Of course we need some more return functions for the new fields, these are.
function SpritePicked() endfunction touch.spritePicked function ObjectPicked() endfunction touch.objectPicked function PickOffsetX() endfunction touch.pickOffsetX# function PickOffsetY() endfunction touch.pickOffsetY#Any existing references to the old variables are changed to access the new fields (if writing) or these functions (if reading).
The following code in the elseif portion of the getPointerPressed() loop.
thisSpritePicked = getSpriteHit( PointWX() , PointWY() ) if thisSpritePicked > 0 thisObjectPicked = FindWorkObjectBySprite( thisSpritePicked ) if thisObjectPicked > 0 thisPickOffsetX# = PointWX() - getSpriteXByOffset( thisSpritePicked ) thisPickOffsetY# = PointWY() - getSpriteYByOffset( thisSpritePicked ) endif endifNow becomes
touch.spritePicked = getSpriteHit( PointWX() , PointWY() ) if SpritePicked() > 0 touch.objectPicked = FindWorkObjectBySprite( SpritePicked() ) if ObjectPicked() > 0 touch.pickOffsetX# = PointWX() - getSpriteXByOffset( SpritePicked() ) touch.pickOffsetY# = PointWY() - getSpriteYByOffset( SpritePicked() ) endif endifThe following code in the getPointerReleased() loop.
if thisObjectPicked > 0 thisX# = PointWX() - thisPickOffsetX# thisY# = PointWY() - thisPickOffsetY# HideHighLight() if WorldToScreenY( thisY#) >= getTextY( panelText() ) DelWorkObject( thisObjectPicked ) else setSpritePositionByOffset( thisSpritePicked , thisX# , thisY# ) setSpriteDepth( thisSpritePicked , DEPTH_WORK - 1 ) thisObjectUnder = WorkObjectIsOver( thisObjectPicked ) if thisObjectUnder > 0 // This is where the merge objects code goes endif endif endif thisSpritePicked = 0 thisObjectPicked = 0 thisPickOffsetX# = 0.0 thisPickOffsetY# = 0.0Becomes;
if ObjectPicked() > 0 thisX# = PointWX() - PickOffsetX() thisY# = PointWY() - PickOffsetY() HideHighLight() if WorldToScreenY( thisY#) >= getTextY( panelText() ) DelWorkObject( ObjectPicked() ) else setSpritePositionByOffset( SpritePicked() , thisX# , thisY# ) setSpriteDepth( SpritePicked() , DEPTH_WORK - 1 ) thisObjectUnder = WorkObjectIsOver( ObjectPicked() ) if thisObjectUnder > 0 // This is where the merge objects code goes endif endif endif touch.spritePicked = 0 touch.objectPicked = 0 touch.pickOffsetX# = 0.0 touch.pickOffsetY# = 0.0And the following code in the getPointerState() portion.
if thisObjectPicked > 0 thisX# = PointWX() - thisPickOffsetX# thisY# = PointWY() - thisPickOffsetY# setSpritePositionByOffset( thisSpritePicked , thisX# , thisY# ) setSpriteDepth( thisSpritePicked , DEPTH_PANEL - 5 ) if WorldToScreenY( thisY#) >= getTextY( panelText() ) HighlightSprite( thisSpritePicked , FRAME_TRASHCAN ) else thisObjectUnder = WorkObjectIsOver( thisObjectPicked ) if thisObjectUnder > 0 HighlightSprite( WorkObjectSprite( thisObjectUnder ) , FRAME_HIGHLIGHT ) else HideHighLight() endif endif endifBecomes;
if thisObjectPicked > 0 thisX# = PointWX() - PickOffsetX() thisY# = PointWY() - PickOffsetY() setSpritePositionByOffset( SpritePicked() , thisX# , thisY# ) setSpriteDepth( SpritePicked() , DEPTH_PANEL - 5 ) if WorldToScreenY( thisY#) >= getTextY( panelText() ) HighlightSprite( SpritePicked() , FRAME_TRASHCAN ) else thisObjectUnder = WorkObjectIsOver( ObjectPicked() ) if thisObjectUnder > 0 HighlightSprite( WorkObjectSprite( thisObjectUnder ) , FRAME_HIGHLIGHT ) else HideHighLight() endif endif endifOf course our test code needs to be updated by changing.
printc( "Sprite Picked: ") printc( str( thisSpritePicked )) printc( ", Object Picked: ") printc( str( thisObjectPicked )) printc( ", Over : " ) print( str( WorkObjectIsOver( thisObjectPicked ) ))To
printc( "Sprite Picked: ") printc( str( SpritePicked() )) printc( ", Object Picked: ") printc( str( ObjectPicked() )) printc( ", Over : " ) print( str( WorkObjectIsOver( ObjectPicked() ) ))And everything is back in working order.
Shame we removed our test sprites.
No comments:
Post a Comment