CodeTrasher, your problem is definately with declaring the variables. They must be global. Here is the full code working as a function...
rem
rem AGK Application
rem
rem Landscape App
SetDisplayAspect( 1024.0 / 600.0 )
rem XXX variables need to be declared as global if using outside the function
global mouseJoint
global rock
rem lower level
for s=1 to 4
spr = createSprite(0)
setSpriteVisible(spr,0)
setSpriteSize(spr,1,20)
setSpritePosition(spr,70 + s*5,80)
rem turn physics on!
setSpritePhysicsOn(spr,2)
if s<4
spr = createSprite(0)
setSpriteVisible(spr,0)
setSpriteSize(spr,5,1.5)
setSpritePosition(spr,70.5 + s*5,79.25)
rem turn physics on!
setSpritePhysicsOn(spr,2)
endif
next
rem obstacle
spr = createSprite(0)
setSpriteVisible(spr,0)
setSpriteSize(spr,2,15)
setSpritePosition(spr,50,85)
rem turn physics on!
setSpritePhysicsOn(spr,1)
rem circular object
spr = createSprite(0)
setSpriteVisible(spr,0)
setSpriteSize(spr,2,-1)
setSpritePosition(spr,5,95)
rem turn physics on!
setSpritePhysicsOn(spr,2)
setSpriteShapeCircle(spr,0,0,1)
rock = spr
rem turn on debugging
setPhysicsDebugOn()
rem A Wizard Did It!
do
mouseGrab()
Sync()
loop
function mouseGrab() rem XXX changed to function
rem get the pointer position
px# = getPointerX()
py# = getPointerY()
rem find out if the pointer/mouse has JUST been pressed
if getPointerPressed()=1
rem check position of rock
if getSpriteX(rock)<20 and getSpriteY(rock)>80
rem check if the "rock" is under the mouse
if getSpriteHitTest(rock,px#,py#)=1
rem it is so let's pick it up
mouseJoint = createMouseJoint(rock,px#,py#,10000)
endif
else
rem reset the "rock" position
setSpritePosition(rock,5,95)
rem stop the "rock" from moving
setSpritePhysicsVelocity(rock,0,0)
setSpritePhysicsAngularVelocity(rock,0)
endif
else
rem check if the mousejoint exists
if mouseJoint>0
rem check if the pointer/mouse is still being pressed
if getPointerState()=1
rem it is so let's update the position of the joint
rem so the "rock" will follow but only if the mouse
rem not too far from the start, in which case it is
rem thrown by deleting the joint!
if px#<20 and py#>80
rem update the mouse joint position
setJointMouseTarget(mouseJoint,px#,py#)
else
rem delete the mouse joint
deleteJoint(mouseJoint)
mouseJoint=0
endif
else
rem delete the mouse joint
deleteJoint(mouseJoint)
mouseJoint=0
endif
endif
endif
endfunction rem XXX endfunction
I made all the changes detailed in my first post and it works fine. I have rem'd with XXX so you can search for the changes made...