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...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | 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() |
Right, sorry, must be rusty. Sorted...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | 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() |
views:1357 update:2011/10/4 8:06:35