Hi all guys,
I really need some help. i have to draw 3 or more lines and apply to each one the physic body as dynamic. I handled this creating a line with line:append then i add the line to a group and i do display.save(). So i have my line saved as a image.png, now i have to load this image and create the outline to create the physic body.
The problem is that this works only for the first time sometimes for the second but never for the third, which is always the same as the second one. The log console show me 2 errors:
-ImageIO: CGImageReadCreateDataWithMappedFile ‘open’ failed ‘/Users/Max/Library/Application Support/Corona Simulator/drawCurve-9A23AC0D5032F5785D485572D0F2DDE4/tmp/line.png’
error = 2 (No such file or directory)
-ImageIO: CGImageReadGetBytesAtOffset : *** ERROR *** CGImageSource was created with data size: 1041 - current size is only: 817
I d like to know if it is a logic problem and how to solve it. Thank you very much for your patience. I m a newbe
Here it is my code:
local physics = require(“physics”)
physics.start()
physics.setDrawMode( ‘hybrid’ )
physics.setGravity( 0,9.8)
local line
local group = {}
local i = 1
–creating gorund
local ground = display.newImage( “platform.png” )
ground.y = display.contentHeight
ground.surfaceType= “superbounce”
ground.rotation = 0
physics.addBody( ground, “static”, { friction=0.3, bounce=0.5} )
–[[creating ball
local circle = display.newImage( “ball.png” )
circle.x=50
circle.y=30
physics.addBody( circle, “dynamic”, {radius=25, density=0.3, friction=0.3, bounce=0.5} )
]]
local function myTouchListener( event )
local x=event.x
local y=event.y
if event.phase == “began” then
group[i] = display.newGroup()
line = display.newLine(x, y, x, y)
line:setStrokeColor( 0, 0, 0 )
line.strokeWidth = 5
line:setStrokeColor( 1, 0, 0 )
elseif event.phase == “moved” then
line:append( x, y)
--circles are to not make the line sharp cornered
local myCircle = display.newCircle( x, y, 2 )
myCircle:setFillColor( 0 )
group[i]:insert(myCircle)
elseif event.phase == “ended” then
--creating a new image.png from the just drawn line
group[i]:insert( line )
local baseDir = system.TemporaryDirectory
local bounds = group[i].contentBounds
xGroup=bounds.xMax-bounds.xMin
yGroup=bounds.yMax-bounds.yMin
group[i].width = xGroup * display.contentScaleX
group[i].height = yGroup * display.contentScaleY
display.save( group[i], “line.png”, baseDir )
--now that we saved the image we destroy the group and then we load it to apply the physics
group[i]:removeSelf()
local imageFile = “line.png”
local imageOutline = graphics.newOutline( 2, imageFile ,baseDir )
local image = display.newImage( imageFile,baseDir )
image.anchorX=0
image.anchorY=0
image.x = bounds.xMin
image.y = bounds.yMin
physics.addBody( image,“dynamic” ,{ outline=imageOutline, density=2, bounce=0.5, friction=0.1 } )
--destroying the image file to prepare the next line to be stored as image.png
i = i+1
local reason = os.remove( system.pathForFile( “line.png”, baseDir ) )
end
return true
end
Runtime:addEventListener(“touch”,myTouchListener)