I’m trying to make a level of my game with the water, so I’m using the code of Emilio Aguairre, but I have some problems with localGroup:insert( )… Do I have to post the code with my tries? [import]uid: 27760 topic_id: 28291 reply_id: 328291[/import]
You do need to post some code, yes, as well as the error/problem. [import]uid: 52491 topic_id: 28291 reply_id: 114288[/import]
Here’s the code ( localGroup5 because I’m working on the 5th level of my game)
[code]
–Level 5
module(…, package.seeall)
function new()
local localGroup5 = display.newGroup()
local physics = require(“physics”)
– Definire le variabili
local imgBuffer = {} – Image buffer
local w1= math.floor(display.contentWidth)
local h1= math.floor(display.contentHeight)
local refreshImage – Flag to handle when to draw
local line={} – Lines table
physics.start()
– Hide status bar
display.setStatusBar( display.HiddenStatusBar )
local bkg=display.newImage(“background.png”)
– Create the border to prevent the liquid to fall
borderCollisionFilter = { categoryBits = 1, maskBits = 6 } – collides with (4 & 2) only
borderBodyElement = { friction=0.1, bounce=0.1, filter=borderCollisionFilter }
– Create 200 balls that will serve as the liquid elements
local balls = {}
local ballsCollisionFilter = { categoryBits = 2, maskBits = 3 } – collides with (2 & 1) only
local ballsBody = { density=1.0, friction=0.0, bounce=0.1, radius=6, filter=ballsCollisionFilter }
local function spawn()
ballsBody.radius=math.random(3,6)
balls[#balls+1] = display.newCircle(160, 50,ballsBody.radius)
balls[#balls]:setFillColor(0,0,0,0) – To make the balls transparent add 0 as the fourth element.
balls[#balls].myName = “ball”
physics.addBody( balls[#balls], ballsBody )
end
local tm = timer.performWithDelay(10, spawn,200)
local circle=display.newImage(“bally.png”)
circle.x=display.contentWidth/2
circle.y=display.contentHeight/2
physics.addBody(circle, { density=1.0, friction=0.0, bounce=0.1, radius=15, filter=ballsCollisionFilter})
– Draw a set of lines (like flood fill) depending if the pixel in
– the image buffer is greater than a threshold.
function fill(img,w,h,y1,y2)
local threshold = 22 – Threshold (best results from 18 to 30)
local st = 0 – Flag to indicate if a start has being found
local x1,y1 – Start line
local x2,y2 – End line
local step = 1 – Step greater than 1 will lead to fast rendering
local yw
local idx
local startY, endY
startY = y1 or 0
endY = y2 or (h - 1)
– Traverse the rows of the image buffer
for y=startY,endY,step do
st = 0
yw = y*w
– Traverse the columns of the image buffer
for x=0,w-1,step do
idx = yw+x+1
– if item exist and is greater than threshold
if img[idx] and img[idx]>threshold then
– There is no start yet
if st == 0 then
st = 1
– Now we have a start
x1,y1 = x,y
x2,y2 = x,y
else
– Grow then end of line with the current pixel
x1,y1 = x,y
end
else
– Do we have a line?
if st == 1 then
– Yes draw it.
line[#line+1] = display.newLine(x1,y1,x2,y2)
line[#line].width = step
line[#line]:setColor( 0,152, 255, 100 ) – semi-transparent
– Clear the flag
st = 0
end
end
end
– Check if a missing line needs to be drawn
if st == 1 then
line[#line+1] = display.newLine(x1,y1,x2,y2)
line[#line].width = step
line[#line]:setColor( 0, 152, 255, 100 ) – semi-transparent
end
end
end
– Draw a circle mask inside a image buffer
local mask = {
0,0,0,0,0,0,0,1,2,2,2,2,1,0,0,0,0,0,0,0,
0,0,0,0,0,2,4,6,7,8,8,7,6,4,2,0,0,0,0,0,
0,0,0,0,4,6,9,11,12,13,13,12,11,9,6,4,0,0,0,0,
0,0,0,4,8,11,13,16,17,18,18,17,16,13,11,8,4,0,0,0,
0,0,4,8,11,15,18,20,22,23,23,22,20,18,15,11,8,4,0,0,
0,2,6,11,15,19,22,25,27,28,28,27,25,22,19,15,11,6,2,0,
0,4,9,13,18,22,26,29,32,33,33,32,29,26,22,18,13,9,4,0,
1,6,11,16,20,25,29,33,36,38,38,36,33,29,25,20,16,11,6,1,
2,7,12,17,22,27,32,36,40,43,43,40,36,32,27,22,17,12,7,2,
2,8,13,18,23,28,33,38,43,47,47,43,38,33,28,23,18,13,8,2,
2,8,13,18,23,28,33,38,43,47,47,43,38,33,28,23,18,13,8,2,
2,7,12,17,22,27,32,36,40,43,43,40,36,32,27,22,17,12,7,2,
1,6,11,16,20,25,29,33,36,38,38,36,33,29,25,20,16,11,6,1,
0,4,9,13,18,22,26,29,32,33,33,32,29,26,22,18,13,9,4,0,
0,2,6,11,15,19,22,25,27,28,28,27,25,22,19,15,11,6,2,0,
0,0,4,8,11,15,18,20,22,23,23,22,20,18,15,11,8,4,0,0,
0,0,0,4,8,11,13,16,17,18,18,17,16,13,11,8,4,0,0,0,
0,0,0,0,4,6,9,11,12,13,13,12,11,9,6,4,0,0,0,0,
0,0,0,0,0,2,4,6,7,8,8,7,6,4,2,0,0,0,0,0,
0,0,0,0,0,0,0,1,2,2,2,2,1,0,0,0,0,0,0,0
}
– Draw a mask circle over the image buffer
– pix - image buffer
– w - width of the image buffer
– h - height of the image buffer
function drawCircle(pix,w,h)
local y,x,ny,nx,index,maskIdx,y9,nyw
local ox – x of the physical object
local oy – y of the physical object
local floor = math.floor
local minY=h
local maxY=0
for i = 1,#balls do
ox = floor(balls[i].x)
oy = floor(balls[i].y)
if oy+10>maxY then maxY = oy+10 end
if oy-9 for y=-9,10 do
y9 =(y+9)20
ny = y+oy
nyw = nyw
for x=-9,10 do
nx = x+ox
if (nx>0 and nx<=w and ny>0 and ny<=h) then
index = nyw+nx+1
maskIdx = y9+x+10
if pix[index] then
pix[index] = (pix[index] + mask[maskIdx])
else
pix[index] = mask[maskIdx]
end
end
end
end
end
– Just draw lines from range [minY,maxY], so in worst case it
– will draw all screen.
return math.max(0,minY),math.min(maxY,h)
end
-------------------------------------------------------------------
– Callback to frame
-------------------------------------------------------------------
local onFrameUpdate = function( event )
– Redraw only if a collision has been detected
if refreshImage == 1 then
– Clear the buffer image
imgBuffer={}
– Remove all lines from display
for _,v in pairs(line) do
v.parent:remove( v )
end
line={}
– Draw in the buffer some dots corresponding to the same position
– of the physical objects
local y1,y2 = drawCircle(imgBuffer,w1,h1)
– Fill the objects in the display according to the objects in the
– image buffer.
fill(imgBuffer,w1,h1,y1,y2)
– Reset the flag
refreshImage = 0
end
end
-------------------------------------------------------------------
– Callback to global collision
-------------------------------------------------------------------
local function onGlobalCollision( event )
if (event.object1.myName~=nil and event.object2.myName~=nil) then
– A collision ended let’s refresh the images
if ( event.phase == “ended”) then
– Only refresh if at least we have a collision
refreshImage = 1
end
end
end
-------------------------------------------------------------------
– Callback to global accelerometer
-------------------------------------------------------------------
–local function onTilt( event )
– physics.setGravity( 10 * event.xGravity, -10 * event.yGravity )
–end
-------------------------------------------------------------------
– Group Insert
-------------------------------------------------------------------
localGroup5:insert(imgBuffer)
localGroup5:insert(line)
localGroup5:insert(bkg)
localGroup5:insert(balls)
localGroup5:insert(circle)
localGroup5:insert(refreshImage)
localGroup5:insert(thresold)
localGroup5:insert(st)
localGroup5:insert(x1, y1)
localGroup5:insert(x2, y2)
localGroup5:insert(step)
localGroup5:insert(yw)
localGroup5:insert(idx)
localGroup5:insert(startY, endY)
localGroup5:insert(y,x,ny,nx,index,maskIdx,y9,nyw)
localGroup5:insert(ox)
localGroup5:insert(oy)
localGroup5:insert(floor)
localGroup5:insert(minY)
localGroup5:insert(maxY)
localGroup5:insert(y1, y2)
-------------------------------------------------------------------
– Create event listeners
-------------------------------------------------------------------
Runtime:addEventListener( “enterFrame”, onFrameUpdate )
Runtime:addEventListener( “collision”, onGlobalCollision )
–Runtime:addEventListener( “accelerometer”, onTilt )
return localGroup5
end[/code]
[import]uid: 27760 topic_id: 28291 reply_id: 114595[/import]
Hey again,
You haven’t said what specific error you are getting yet - what is it? (You can copy and paste it from the terminal window.)
Peach
[import]uid: 52491 topic_id: 28291 reply_id: 114651[/import]
Well… the problem is that this code doesn’t run on the simulator… [import]uid: 27760 topic_id: 28291 reply_id: 114666[/import]
OK, but what error do you get in the terminal?
Are you running Corona with the terminal open? If you’re confused about this tell me and I will explain it to you - it’s how you errors and can then share them so you debug your code
[import]uid: 52491 topic_id: 28291 reply_id: 114723[/import]
I’m not running corona with the terminal open… I’m a bit confused. Can you explain me? [import]uid: 27760 topic_id: 28291 reply_id: 114753[/import]
No… no problem.
Here’s what is written on the terminal:
[mysql]
Last login: Mon Jul 9 14:29:25 on ttys001
iMac-di-Marco:~ Marco$ /Applications/CoronaSDK/Corona\ Terminal ; exit;
Copyright © 2009-2011 A n s c a , I n c .
Version: 2.0.0
Build: 2011.704
Copyright © 2009-2011 A n s c a , I n c .
Version: 2.0.0
Build: 2011.704
The file sandbox for this project is located at the following folder:
(/Users/Marco/Library/Application Support/Corona Simulator/JumpBall-BB174B105093DD111B5CA418295B5856)
WARNING: Failed to find image(bally.png)
Runtime error
/Users/Marco/Desktop/JumpBall 1.0/JumpBall/5.lua:54: attempt to index local ‘circle’ (a nil value)
stack traceback:
[C]: ?
/Users/Marco/Desktop/JumpBall 1.0/JumpBall/5.lua:54: in function ‘new’
…ers/Marco/Desktop/JumpBall 1.0/JumpBall/director.lua:36: in function ‘loadScene’
…ers/Marco/Desktop/JumpBall 1.0/JumpBall/director.lua:230: in function ‘changeScene’
…Users/Marco/Desktop/JumpBall 1.0/JumpBall/level1.lua:100: in function <…users>
?: in function <?:215>
Copyright © 2009-2011 A n s c a , I n c .
Version: 2.0.0
Build: 2011.704
The file sandbox for this project is located at the following folder:
(/Users/Marco/Library/Application Support/Corona Simulator/JumpBall-BB174B105093DD111B5CA418295B5856)
Runtime error
/Users/Marco/Desktop/JumpBall 1.0/JumpBall/5.lua:234: bad argument #-2 to ‘insert’ (Proxy expected, got nil)
stack traceback:
[C]: ?
[C]: in function ‘insert’
/Users/Marco/Desktop/JumpBall 1.0/JumpBall/5.lua:234: in function ‘new’
…ers/Marco/Desktop/JumpBall 1.0/JumpBall/director.lua:36: in function ‘loadScene’
…ers/Marco/Desktop/JumpBall 1.0/JumpBall/director.lua:230: in function ‘changeScene’
…Users/Marco/Desktop/JumpBall 1.0/JumpBall/level1.lua:100: in function <…users>
?: in function <?:215>
Copyright © 2009-2011 A n s c a , I n c .
Version: 2.0.0
Build: 2011.704
The file sandbox for this project is located at the following folder:
(/Users/Marco/Library/Application Support/Corona Simulator/JumpBall-BB174B105093DD111B5CA418295B5856)
Runtime error
/Users/Marco/Desktop/JumpBall 1.0/JumpBall/5.lua:234: bad argument #-2 to ‘insert’ (Proxy expected, got nil)
stack traceback:
[C]: ?
[C]: in function ‘insert’
/Users/Marco/Desktop/JumpBall 1.0/JumpBall/5.lua:234: in function ‘new’
…ers/Marco/Desktop/JumpBall 1.0/JumpBall/director.lua:36: in function ‘loadScene’
…ers/Marco/Desktop/JumpBall 1.0/JumpBall/director.lua:230: in function ‘changeScene’
…Users/Marco/Desktop/JumpBall 1.0/JumpBall/level1.lua:100: in function <…users>
?: in function <?:215>
[/mysql] [import]uid: 27760 topic_id: 28291 reply_id: 114852[/import] </…users></…users></…users>
OK, you’re referring to an image called “bally.png” in your code but apparently it isn’t your project folder - correct that and then run it again and see if you get any errors in the terminal. If you do, post them here. (It may very well be as simple as fixing the image.)
Let me know how you go.
Peach
[import]uid: 52491 topic_id: 28291 reply_id: 114906[/import]
Ok… I’ve fixed that error, but It doesn’t run on the simulator. Here’s what is written on the terminal:
[code]
Last login: Thu Jul 12 09:05:58 on ttys000
/Applications/CoronaSDK/Corona\ Terminal ; exit;
iMac-di-Marco:~ Marco$ /Applications/CoronaSDK/Corona\ Terminal ; exit;
Copyright © 2009-2011 A n s c a , I n c .
Version: 2.0.0
Build: 2011.704
Copyright © 2009-2011 A n s c a , I n c .
Version: 2.0.0
Build: 2011.704
The file sandbox for this project is located at the following folder:
(/Users/Marco/Library/Application Support/Corona Simulator/JumpBall-BB174B105093DD111B5CA418295B5856)
Runtime error
/Users/Marco/Desktop/JumpBall 1.0/JumpBall/5.lua:234: bad argument #-2 to ‘insert’ (Proxy expected, got nil)
stack traceback:
[C]: ?
[C]: in function ‘insert’
/Users/Marco/Desktop/JumpBall 1.0/JumpBall/5.lua:234: in function ‘new’
…ers/Marco/Desktop/JumpBall 1.0/JumpBall/director.lua:36: in function ‘loadScene’
…ers/Marco/Desktop/JumpBall 1.0/JumpBall/director.lua:230: in function ‘changeScene’
…Users/Marco/Desktop/JumpBall 1.0/JumpBall/level1.lua:100: in function <…users>
?: in function <?:215>
[/code]
[import]uid: 27760 topic_id: 28291 reply_id: 115095[/import] </…users>
Looks like a director issue. Code would help here. [import]uid: 52491 topic_id: 28291 reply_id: 115098[/import]
Here’s the code:
[code]
module(…, package.seeall)
function new()
local localGroup5 = display.newGroup()
local physics = require(“physics”)
– Definire le variabili
local imgBuffer = {} – Image buffer
local w1= math.floor(display.contentWidth)
local h1= math.floor(display.contentHeight)
local refreshImage – Flag to handle when to draw
local line={} – Lines table
physics.start()
– Hide status bar
display.setStatusBar( display.HiddenStatusBar )
local bkg=display.newImage(“background.png”)
– Create the border to prevent the liquid to fall
–borderCollisionFilter = { categoryBits = 1, maskBits = 6 } – collides with (4 & 2) only
–borderBodyElement = { friction=0.1, bounce=0.1, filter=borderCollisionFilter }
– Create 200 balls that will serve as the liquid elements
local balls = {}
local ballsCollisionFilter = { categoryBits = 2, maskBits = 3 } – collides with (2 & 1) only
local ballsBody = { density=1.0, friction=0.0, bounce=0.1, radius=6, filter=ballsCollisionFilter }
local function spawn()
ballsBody.radius=math.random(3,6)
balls[#balls+1] = display.newCircle(160, 50,ballsBody.radius)
balls[#balls]:setFillColor(0,0,0,0) – To make the balls transparent add 0 as the fourth element.
balls[#balls].myName = “ball”
physics.addBody( balls[#balls], ballsBody )
end
local tm = timer.performWithDelay(10, spawn,200)
local circle=display.newImage(“betterball4.png”)
circle.x=display.contentWidth/2
circle.y=display.contentHeight/2
physics.addBody(circle, { density=1.0, friction=0.0, bounce=0.1, radius=15, filter=ballsCollisionFilter})
– Draw a set of lines (like flood fill) depending if the pixel in
– the image buffer is greater than a threshold.
function fill(img,w,h,y1,y2)
local threshold = 22 – Threshold (best results from 18 to 30)
local st = 0 – Flag to indicate if a start has being found
local x1,y1 – Start line
local x2,y2 – End line
local step = 1 – Step greater than 1 will lead to fast rendering
local yw
local idx
local startY, endY
startY = y1 or 0
endY = y2 or (h - 1)
– Traverse the rows of the image buffer
for y=startY,endY,step do
st = 0
yw = y*w
– Traverse the columns of the image buffer
for x=0,w-1,step do
idx = yw+x+1
– if item exist and is greater than threshold
if img[idx] and img[idx]>threshold then
– There is no start yet
if st == 0 then
st = 1
– Now we have a start
x1,y1 = x,y
x2,y2 = x,y
else
– Grow then end of line with the current pixel
x1,y1 = x,y
end
else
– Do we have a line?
if st == 1 then
– Yes draw it.
line[#line+1] = display.newLine(x1,y1,x2,y2)
line[#line].width = step
line[#line]:setColor( 0,152, 255, 100 ) – semi-transparent
– Clear the flag
st = 0
end
end
end
– Check if a missing line needs to be drawn
if st == 1 then
line[#line+1] = display.newLine(x1,y1,x2,y2)
line[#line].width = step
line[#line]:setColor( 0, 152, 255, 100 ) – semi-transparent
end
end
end
– Draw a circle mask inside a image buffer
local mask = {
0,0,0,0,0,0,0,1,2,2,2,2,1,0,0,0,0,0,0,0,
0,0,0,0,0,2,4,6,7,8,8,7,6,4,2,0,0,0,0,0,
0,0,0,0,4,6,9,11,12,13,13,12,11,9,6,4,0,0,0,0,
0,0,0,4,8,11,13,16,17,18,18,17,16,13,11,8,4,0,0,0,
0,0,4,8,11,15,18,20,22,23,23,22,20,18,15,11,8,4,0,0,
0,2,6,11,15,19,22,25,27,28,28,27,25,22,19,15,11,6,2,0,
0,4,9,13,18,22,26,29,32,33,33,32,29,26,22,18,13,9,4,0,
1,6,11,16,20,25,29,33,36,38,38,36,33,29,25,20,16,11,6,1,
2,7,12,17,22,27,32,36,40,43,43,40,36,32,27,22,17,12,7,2,
2,8,13,18,23,28,33,38,43,47,47,43,38,33,28,23,18,13,8,2,
2,8,13,18,23,28,33,38,43,47,47,43,38,33,28,23,18,13,8,2,
2,7,12,17,22,27,32,36,40,43,43,40,36,32,27,22,17,12,7,2,
1,6,11,16,20,25,29,33,36,38,38,36,33,29,25,20,16,11,6,1,
0,4,9,13,18,22,26,29,32,33,33,32,29,26,22,18,13,9,4,0,
0,2,6,11,15,19,22,25,27,28,28,27,25,22,19,15,11,6,2,0,
0,0,4,8,11,15,18,20,22,23,23,22,20,18,15,11,8,4,0,0,
0,0,0,4,8,11,13,16,17,18,18,17,16,13,11,8,4,0,0,0,
0,0,0,0,4,6,9,11,12,13,13,12,11,9,6,4,0,0,0,0,
0,0,0,0,0,2,4,6,7,8,8,7,6,4,2,0,0,0,0,0,
0,0,0,0,0,0,0,1,2,2,2,2,1,0,0,0,0,0,0,0
}
– Draw a mask circle over the image buffer
– pix - image buffer
– w - width of the image buffer
– h - height of the image buffer
function drawCircle(pix,w,h)
local y,x,ny,nx,index,maskIdx,y9,nyw
local ox – x of the physical object
local oy – y of the physical object
local floor = math.floor
local minY=h
local maxY=0
for i = 1,#balls do
ox = floor(balls[i].x)
oy = floor(balls[i].y)
if oy+10>maxY then maxY = oy+10 end
if oy-9 for y=-9,10 do
y9 =(y+9)20
ny = y+oy
nyw = nyw
for x=-9,10 do
nx = x+ox
if (nx>0 and nx<=w and ny>0 and ny<=h) then
index = nyw+nx+1
maskIdx = y9+x+10
if pix[index] then
pix[index] = (pix[index] + mask[maskIdx])
else
pix[index] = mask[maskIdx]
end
end
end
end
end
– Just draw lines from range [minY,maxY], so in worst case it
– will draw all screen.
return math.max(0,minY),math.min(maxY,h)
end
-------------------------------------------------------------------
– Callback to frame
-------------------------------------------------------------------
local onFrameUpdate = function( event )
– Redraw only if a collision has been detected
if refreshImage == 1 then
– Clear the buffer image
imgBuffer={}
– Remove all lines from display
for _,v in pairs(line) do
v.parent:remove( v )
end
line={}
– Draw in the buffer some dots corresponding to the same position
– of the physical objects
local y1,y2 = drawCircle(imgBuffer,w1,h1)
– Fill the objects in the display according to the objects in the
– image buffer.
fill(imgBuffer,w1,h1,y1,y2)
– Reset the flag
refreshImage = 0
end
end
-------------------------------------------------------------------
– Callback to global collision
-------------------------------------------------------------------
local function onGlobalCollision( event )
if (event.object1.myName~=nil and event.object2.myName~=nil) then
– A collision ended let’s refresh the images
if ( event.phase == “ended”) then
– Only refresh if at least we have a collision
refreshImage = 1
end
end
end
-------------------------------------------------------------------
– Callback to global accelerometer
-------------------------------------------------------------------
–local function onTilt( event )
– physics.setGravity( 10 * event.xGravity, -10 * event.yGravity )
–end
-------------------------------------------------------------------
– Group Insert
-------------------------------------------------------------------
localGroup5:insert(imgBuffer)
localGroup5:insert(line)
localGroup5:insert(bkg)
localGroup5:insert(balls)
localGroup5:insert(circle)
localGroup5:insert(refreshImage)
localGroup5:insert(thresold)
localGroup5:insert(st)
localGroup5:insert(x1, y1)
localGroup5:insert(x2, y2)
localGroup5:insert(step)
localGroup5:insert(yw)
localGroup5:insert(idx)
localGroup5:insert(startY, endY)
localGroup5:insert(y,x,ny,nx,index,maskIdx,y9,nyw)
localGroup5:insert(ox)
localGroup5:insert(oy)
localGroup5:insert(floor)
localGroup5:insert(minY)
localGroup5:insert(maxY)
localGroup5:insert(y1, y2)
–localGroup5:insert()
–localGroup5:insert()
–localGroup5:insert()
-------------------------------------------------------------------
– Create event listeners
-------------------------------------------------------------------
Runtime:addEventListener( “enterFrame”, onFrameUpdate )
Runtime:addEventListener( “collision”, onGlobalCollision )
–Runtime:addEventListener( “accelerometer”, onTilt )
return localGroup5
end[/code] [import]uid: 27760 topic_id: 28291 reply_id: 115135[/import]
Ah OK I see - you are trying to insert line and balls which are both tables and cannot be inserted into the group.
Only insert display objects nothing else
[import]uid: 52491 topic_id: 28291 reply_id: 115342[/import]