Got rusty

Sorry everyone for this, but I cannot figure out why this code is not working. It should draw a rounded rectangle and direct all touch events back to the display group created on line 39.

This would not have been a problem 6 months ago…

[lua]require (“physics”)

physics.start()
physics.setDrawMode( “hybrid” ) – Uncomment in order to show in hybrid mode
physics.setGravity( 0, 10)

physics.start()

function createGlobalAnchor()
anchor = display.newCircle( -100, -100, 10 )
anchor.alpha = 0
physics.addBody( anchor, “static” )
end

function main()
display.setStatusBar( display.HiddenStatusBar )
createGlobalAnchor()
Runtime:addEventListener( “touch”, createNewBat )
end

local function angleBetween ( srcObj, dstObj )
local xDist = dstObj.x-srcObj.x ; local yDist = dstObj.y-srcObj.y
local angleBetween = math.deg( math.atan( yDist/xDist ) )
if (srcObj.x < dstObj.x) then
angleBetween = angleBetween+90
else
angleBetween = angleBetween-90
end
return angleBetween
end

local function distanceBetween( point1, point2 )
local xfactor = point2.x-point1.x ; local yfactor = point2.y-point1.y
local distanceBetween = math.sqrt((xfactor*xfactor) + (yfactor*yfactor))
return distanceBetween
end

function createNewBat( event )
local batgrp = display.newGroup()
batgrp.start = event
batgrp.ending = event

function batgrp:newBat()
local rect = display.newRoundedRect( batgrp, batgrp.start.x-25, batgrp.start.y-25, 50, 50, 25 )
rect.strokeWidth = 12
–rect:setStrokeColor( 0, 255, 0 )
–rect:setFillColor( 0, 0, 0 )
rect.x, rect.y = batgrp.start.x+(batgrp.ending.x-batgrp.start.x)/2, batgrp.start.y+(batgrp.ending.y-batgrp.start.y)/2
local angle = angleBetween( batgrp.start, batgrp.ending )
rect.rotation = angle
physics.addBody( rect, “static” )
end

function batgrp:touch( event )
print(event.x,event.y)
if (event.phase == “moved”) then
batgrp[1]:removeSelf()
batgrp.ending = event
batgrp:newBat()
else
display.getCurrentStage():setFocus( nil )
end
end
print(event.x,event.y)
batgrp:newBat( event, event )
display.getCurrentStage():setFocus( batgrp )
batgrp:addEventListener( “touch”, batgrp )

return true
end

main()[/lua]

Thanks,

Matt. [import]uid: 8271 topic_id: 15021 reply_id: 315021[/import]

Right, sorry, must be rusty. Sorted…

[lua]require (“physics”)

physics.start()
physics.setDrawMode( “hybrid” ) – Uncomment in order to show in hybrid mode
physics.setGravity( 0, 10)

physics.start()

function createGlobalAnchor()
anchor = display.newCircle( -100, -100, 10 )
anchor.alpha = 0
physics.addBody( anchor, “static” )
end

function main()
display.setStatusBar( display.HiddenStatusBar )
createGlobalAnchor()
Runtime:addEventListener( “touch”, createNewBat )
end

local function angleBetween ( srcObj, dstObj )
local xDist = dstObj.x-srcObj.x ; local yDist = dstObj.y-srcObj.y
local angleBetween = math.deg( math.atan( yDist/xDist ) )
if (srcObj.x < dstObj.x) then
angleBetween = angleBetween+90
else
angleBetween = angleBetween-90
end
return angleBetween
end

local function distanceBetween( point1, point2 )
local xfactor = point2.x-point1.x ; local yfactor = point2.y-point1.y
local distanceBetween = math.sqrt((xfactor*xfactor) + (yfactor*yfactor))
return distanceBetween
end

function createNewBat( event )
local batgrp = display.newGroup()
batgrp.start = event
batgrp.ending = event

function batgrp:newBat()
local rect = display.newRoundedRect( batgrp, batgrp.start.x-25, batgrp.start.y-25, 50, 50, 25 )
rect.strokeWidth = 12
–rect:setStrokeColor( 0, 255, 0 )
–rect:setFillColor( 0, 0, 0 )
rect.x, rect.y = batgrp.start.x+(batgrp.ending.x-batgrp.start.x)/2, batgrp.start.y+(batgrp.ending.y-batgrp.start.y)/2
local angle = angleBetween( batgrp.start, batgrp.ending )
rect.rotation = angle
physics.addBody( rect, “static” )
end

function batgrp:touch( event )
print(event.x,event.y)
if (event.phase == “moved”) then
batgrp[1]:removeSelf()
batgrp.ending = event
batgrp:newBat()
else
display.getCurrentStage():setFocus( nil )
end
return true
end
print(event.x+event.y)
batgrp:newBat( event, event )
display.getCurrentStage():setFocus( batgrp )
batgrp:addEventListener( “touch”, batgrp )

return true
end

main()[/lua] [import]uid: 8271 topic_id: 15021 reply_id: 55506[/import]