There appears to be a bug in the sprite detection (bug report [link]http://code.google.com/p/agk/issues/detail?id=163[/link] )
Essentially getting real world coordinates returns sprites with the same screen coordinates, even if the world sprites are nowhere near.
Here's a little something which demonstrates the effect
// Using a screen Res of 480 x 800 ( portrait ), black background, yellow text size 16
setOrientationAllowed ( 1 , 1 , 0 , 0 )
setVirtualResolution ( 480 , 800 )
setClearColor ( 0 , 0 , 0 )
setPrintSize ( 16 )
setPrintColor ( 255 , 255 , 192 )
// Setup Sprite to Mask lower Portion of Screen - the text will be on top of this, the object sprites behind
maskSprite = myCreateSpriteAndSetup ( 0 + 240 , 490 + 155 , 480 , 310 , 0 , 128 , 255 , 20 )
fixSpriteToScreen ( maskSprite , 1 )
// Setup a grid of sprites of random size 8 to 32 spaced at 64 Pixel Intervals
dim spriteGrid [ 15 , 15 ]
for x = 0 to 15
xx = x * 64 - 168
for y = 0 to 15
yy = y * 64 + 8
// Set Size Randomly
thisSizeX = random ( 8 , 32 )
thisSizeY = random ( 8 , 32 )
halfX = thisSizeX / 2
halfY = thisSizeY / 2
// if sprite world coords are within masks screen coordinate range, set to red otherwise set to green
if xx > ( 0 - halfX ) and xx < ( 480 + halfX ) and yy > ( 490 - halfY ) and yy < ( 800 + halfY )
thisRed = 255
thisGrn = 0
else
thisRed = 0
thisGrn = 255
endif
// Create Size and Colour Sprites
spriteGrid [ x , y ] = myCreateSpriteAndSetup ( xx , yy , thisSizeX , thisSizeY , thisRed, thisGrn, 0 , 30 )
next y
next x
// Global Variables for tracking movement
global screenPosX , screenPosY , PointDownX , PointDownY , spriteHit
do
if GetRawKeyState ( 27 ) = 1 then exit
// Scan the pointer and check for clicks and drags and stuff
myScanPointerPos ( )
// Dont print over sprite area, go down to blue mask area
for i=0 to 30
print ( " " )
next i
// Print a brief explanation of the problem
print ( " 1) Drag on the screen to move around.")
print ( " 2) Click on a Sprite to change its colour." )
print ( " 3) Clicking the mask changes no colour." )
print ( " " )
print ( " Green sprites respond as they should." )
print ( " Red sprites (which start under the mask) and" )
print ( " the space between them, will return the sprite" )
print ( " ID of the mask, changing its colour." )
print ( " " )
// Print some stuff showing variables
print ( " Screen Offset : " + str ( screenPosX ) + " , " + str ( screenPosY ) )
print ( " Pointer Pos (screen): " + str ( PointDownX ) + " , " + str ( PointDownY ) )
print ( " Pointer Pos (world) : " + str ( screenToWorldX ( PointDownX ) , 0 ) + " , " + str ( screenToWorldY ( PointDownY ) , 0 ) )
print ( " " )
print ( " Mask Sprite ID : " + str ( maskSprite ) + " (Area under text)")
print ( " Object Sprite IDs : " + str ( spriteGrid [ 0 , 0 ] ) + " to " + str ( spriteGrid [ 15 , 15 ] ) )
printc ( " Sprite hit ID : " + str ( spriteHit ) )
if spriteHit = maskSprite
print (" (mask)")
else
print ( " " )
endif
print ( " " )
print ( " Mask Sprite coords : " + myGetSpriteCoords ( maskSprite ) )
if spriteHit > 0
print ( " Sprite hit coords : " + myGetSpriteCoords ( spriteHit ) )
else
print ( " ")
endif
sync ( )
loop
end
// Function creates a blank sprite, sets basic parameters and returns its ID
function myCreateSpriteAndSetup ( thisPosX, thisPosY , thisSizeX , thisSizeY, thisRed, thisGrn, thisBlu, thisDepth )
thisHalfX = thisSizeX / 2
thisHalfY = thisSizeY / 2
returnVal = createSprite ( 0 )
setSpriteSize ( returnVal , thisSizeX , thisSizeY )
setSpriteOffset ( returnVal , thisHalfX , thisHalfY )
setSpritePositionbyOffset ( returnVal , thisPosX , thisPosY )
setSpriteColor ( returnVal , thisRed, thisGrn, thisBlu , 255 )
setSpriteDepth ( returnVal , thisDepth )
endfunction returnVal
// Function returns sprite coords in a string in format "x1,y1 to x2,y2"
function myGetSpriteCoords ( thisSprite )
thisX = getSpriteX ( thisSprite )
thisY = getSpriteY ( thisSprite )
thisWidth = getSpriteWidth ( thisSprite )
thisHeight = getSpriteHeight ( thisSprite )
returnVal$ = str ( thisX ) + "," + str ( thisY ) + " to " + str ( thisX + thisWidth ) + "," + str ( thisY + thisHeight )
endfunction returnVal$
// Function gets the pointer state and position
function myScanPointerPos ( )
thisDown = getPointerPressed ( )
thisState = getPointerState ( )
thisX = getPointerX ( )
thisY = getPointerY ( )
// if the pointer was just pressed, note its position and check if a sprite was hit
if thisDown
PointDownX = thisX
PointDownY = thisY
// Check for hittage using the world conversion calls
spriteHit = getSpriteHit ( screenToWorldX ( thisX ) , screentoWorldY ( thisY ) )
// if a sprite was hit and the pointer was above the masked area then change the colour of the sprite that was hit.
// so in theory it should be impossible to change the colour of the mask
if spriteHit > 0 and thisY < 490
setSpriteColor ( spriteHit , random ( 128 , 240 ) , random ( 128 , 240 ) , random ( 128 , 240 ) , 255 )
endif
endif
// the rest just deals with dragging the screen around in a rough and ready fashion
// If the pointer is currently pressed...
if thisState
// work out the distance moved since the last time this was checked
pointMoveX = thisX - PointDownX
pointMoveY = thisY - PointDownY
// if the distance is some (in any direction)
if pointMoveX <> 0 or pointMoveY <> 0
// Reset the point down position
PointDownX = thisX
PointDownY = thisY
// move the screen the corresponding amount in the opposite direction - 'cos drag works that way...
// ...when you drag the screen one way, we really actually move the viewport the opposite way
screenPosX = screenPosX - pointMoveX
screenPosY = screenPosY - pointMoveY
// set the view offset to the new position
setViewOffset ( screenPosX , ScreenPosY )
endif
endif
endfunction
Needs project screen to be at least 480x800 resolution as it was designed to test on my phone
(Project attached for the
copyingly challenged )