Help bij making a table for physic

Hello everybody,

I’m busy with a game that i want to make. But it doesn’t work and i don’t know what to do now. I want to put “angry” in  a table but i don’t know how. I have looked to tutorials and video’s but i don’t understand it. Can anybody help me out? or can tell me witch tutorial explain it good?

display.setStatusBar(display.HiddenStatusBar) local physics = require( "physics" ) physics.start() physics.setGravity(0, 0) local centerX = display.contentCenterX local centerY = display.contentCenterY local actualW = display.actualContentWidth local actualH = display.actualContentHeight local secondsLeft = 01 \* 60 -- 20 minutes \* 60 seconds local background = display.newRect( centerX, centerY, actualW, actualH ) background:setFillColor( 0, 0, 1 ) local score = 0 local scoreTxt = display.newText( "0", 0, 0, native.systemFontBold, 40 ) scoreTxt.x = centerX + actualW/2 - 20 scoreTxt.y = centerY - actualH/2 + 20 local function updateScore(event) score = score + 1 scoreTxt.text = string.format("%d", score ) end local clockText = display.newText("01:00", display.contentCenterX, 80, native.systemFontBold, 80) clockText:setFillColor( 0.7, 0.7, 1 ) local happy = display.newCircle( 0, 0, 10 ) happy.x = 45 happy.y = 45 physics.addBody( happy, "dynamic" ) happy:setFillColor( 0, 1, 0 ) happy.myName = "happy" local function angry() angry = display.newCircle( 0, 0, 10 ) angry.x = 420 angry.y = 280 physics.addBody( angry, "static" ) angry:setFillColor( 1, 0, 0 ) angry.myName = "angry" physics.addBody( angry, "static" ) end timer.performWithDelay( 5000, angry, 50 ) angry() local function updateTime() -- decrement the number of seconds secondsLeft = secondsLeft - 1 -- time is tracked in seconds. We need to convert it to minutes and seconds local minutes = math.floor( secondsLeft / 60 ) local seconds = secondsLeft % 60 -- make it a string using string format. local timeDisplay = string.format( "%02d:%02d", minutes, seconds ) clockText.text = timeDisplay end local countDownTimer = timer.performWithDelay( 1000, updateTime, secondsLeft ) function touchScreen(event) if event.phase == "began" then transition.to(happy, {time=1000, x=event.x, y=event.y}) end end Runtime:addEventListener( "touch", touchScreen ) function moveAngry() transition.to(angry, {time=1000, x=math.random( centerX - actualW/2, centerX + actualW/2 ), y=math.random( centerY - actualH/2, centerY + actualH/2 ), onComplete=moveAngry }) end moveAngry() local function functions() display.remove(angry) updateScore() end function onCollision(event) if event.phase == "began" then if event.target.myName == "angry" and event.other.myName == "happy" then functions() end end end angry:addEventListener( "collision", onCollision ) local function endGame() if secondsLeft == 0 then print("bye") Runtime:removeEventListener( "enterFrame", endGame ) end end Runtime:addEventListener( "enterFrame", endGame )

Thanks!

You can not use angry as both a function and variable:

local function angry() -\<----- used as a function here angry = display.newCircle( 0, 0, 10 )&nbsp; --\<------ used as a variable/object here.

I believe you have already asked this question before. Can you provide a better explanation on what you want to do?

Hello Rob,

Thanks for your post. I will try to give a better explanation:

I want that every few seconds there will be added a new “Angry”. The script i have build now added “angry” automatically. The first angry works fine, when “happy” touch “angry”, “angry” will be destroyd and my score goes +1. But all the other “angry” don’t work, if “happy” touch them there happens nothing. I have read that if you make a table this issue will be solve, i have seen many tutorials but i have no idea how to create one.

Maybe something like:

local angryList = {} local function makeAngry() local angry = display.newCircle( 0, 0, 10 ) angry.x = 420 angry.y = 280 physics.addBody( angry, "static" ) angry:setFillColor( 1, 0, 0 ) angry.myName = "angry" physics.addBody( angry, "static" ) angryList[#angryList + 1] = angry end timer.performWithDelay( 5000, makeAngry, 50 ) makeAngry()

Nope, that didn’t work. 

Explain? Are you getting errors? What are you expecting? What are you seeing?
 

We can’t really read your mind or see through your eyes. You have to use words and screen shots, and pasting in your code for us to help.

Rob

Hi Rob,

Yeah i didn’t explait it to well. So i will try it again, i hope this is will be clearer.

When i use this script: 

display.setStatusBar(display.HiddenStatusBar) local physics = require( "physics" ) physics.start() physics.setGravity(0, 0) local centerX = display.contentCenterX local centerY = display.contentCenterY local actualW = display.actualContentWidth local actualH = display.actualContentHeight local secondsLeft = 01 \* 60 -- 20 minutes \* 60 seconds local background = display.newRect( centerX, centerY, actualW, actualH ) background:setFillColor( 0, 0, 1 ) local score = 0 local scoreTxt = display.newText( "0", 0, 0, native.systemFontBold, 40 ) scoreTxt.x = centerX + actualW/2 - 20 scoreTxt.y = centerY - actualH/2 + 20 local function updateScore(event) score = score + 1 scoreTxt.text = string.format("%d", score ) end local clockText = display.newText("01:00", display.contentCenterX, 80, native.systemFontBold, 80) clockText:setFillColor( 0.7, 0.7, 1 ) local happy = display.newCircle( 0, 0, 10 ) happy.x = 45 happy.y = 45 physics.addBody( happy, "dynamic" ) happy:setFillColor( 0, 1, 0 ) happy.myName = "happy" local function angry() angry = display.newCircle( 0, 0, 10 ) angry.x = 420 angry.y = 280 physics.addBody( angry, "static" ) angry:setFillColor( 1, 0, 0 ) angry.myName = "angry" physics.addBody( angry, "static" ) end timer.performWithDelay( 5000, angry, 50 ) angry() local function updateTime() -- decrement the number of seconds secondsLeft = secondsLeft - 1 -- time is tracked in seconds. We need to convert it to minutes and seconds local minutes = math.floor( secondsLeft / 60 ) local seconds = secondsLeft % 60 -- make it a string using string format. local timeDisplay = string.format( "%02d:%02d", minutes, seconds ) clockText.text = timeDisplay end local countDownTimer = timer.performWithDelay( 1000, updateTime, secondsLeft ) function touchScreen(event) if event.phase == "began" then transition.to(happy, {time=1000, x=event.x, y=event.y}) end end Runtime:addEventListener( "touch", touchScreen ) function moveAngry() transition.to(angry, {time=1000, x=math.random( centerX - actualW/2, centerX + actualW/2 ), y=math.random( centerY - actualH/2, centerY + actualH/2 ), onComplete=moveAngry }) end moveAngry() local function functions() display.remove(angry) updateScore() end function onCollision(event) if event.phase == "began" then if event.target.myName == "angry" and event.other.myName == "happy" then functions() end end end angry:addEventListener( "collision", onCollision ) local function endGame() if secondsLeft == 0 then print("bye") Runtime:removeEventListener( "enterFrame", endGame ) end end Runtime:addEventListener( "enterFrame", endGame )

I get this screen: 

http://i66.tinypic.com/2mwrp5w.jpg (the image is very big so i don’t think it will be handy if i post it here)

So as you can see “angry” moves the hole time. When i go over “angry”, the score will be +1 and “angry” will be disappear. But than after a few seconds the other “angry’s” will be displayed. They also move the hole time. But when an other “angry” is created the previous “angry” will stop moving the hole time. Also when i go over the “angry’s” they didn’t disappear en the score don’t go +1.

what i want:

After a few seconds there will be a created a “angry” this one moves the hole time (also when other “angry’s” will be added in the game). Everytime when “happy” goes over “angry”, “angry” will be disappear and the score will be +1.

Can somebody please help me, i have tried months but the only thing that i receive is headache. 

@rob 

i have added your code in my code like this way:

display.setStatusBar(display.HiddenStatusBar) local physics = require( "physics" ) physics.start() physics.setGravity(0, 0) local centerX = display.contentCenterX local centerY = display.contentCenterY local actualW = display.actualContentWidth local actualH = display.actualContentHeight local secondsLeft = 01 \* 60 -- 20 minutes \* 60 seconds local background = display.newRect( centerX, centerY, actualW, actualH ) background:setFillColor( 0, 0, 1 ) local score = 0 local scoreTxt = display.newText( "0", 0, 0, native.systemFontBold, 40 ) scoreTxt.x = centerX + actualW/2 - 20 scoreTxt.y = centerY - actualH/2 + 20 local function updateScore(event) score = score + 1 scoreTxt.text = string.format("%d", score ) end local clockText = display.newText("01:00", display.contentCenterX, 80, native.systemFontBold, 80) clockText:setFillColor( 0.7, 0.7, 1 ) local happy = display.newCircle( 0, 0, 10 ) happy.x = 45 happy.y = 45 physics.addBody( happy, "dynamic" ) happy:setFillColor( 0, 1, 0 ) happy.myName = "happy" local angryList = {} local function makeAngry() local angry = display.newCircle( 0, 0, 10 ) angry.x = 420 angry.y = 280 physics.addBody( angry, "static" ) angry:setFillColor( 1, 0, 0 ) angry.myName = "angry" physics.addBody( angry, "static" ) angryList[#angryList + 1] = angry end timer.performWithDelay( 5000, makeAngry, 50 ) makeAngry() timer.performWithDelay( 5000, angry, 50 ) angry() local function updateTime() -- decrement the number of seconds secondsLeft = secondsLeft - 1 -- time is tracked in seconds. We need to convert it to minutes and seconds local minutes = math.floor( secondsLeft / 60 ) local seconds = secondsLeft % 60 -- make it a string using string format. local timeDisplay = string.format( "%02d:%02d", minutes, seconds ) clockText.text = timeDisplay end local countDownTimer = timer.performWithDelay( 1000, updateTime, secondsLeft ) function touchScreen(event) if event.phase == "began" then transition.to(happy, {time=1000, x=event.x, y=event.y}) end end Runtime:addEventListener( "touch", touchScreen ) function moveAngry() transition.to(angry, {time=1000, x=math.random( centerX - actualW/2, centerX + actualW/2 ), y=math.random( centerY - actualH/2, centerY + actualH/2 ), onComplete=moveAngry }) end moveAngry() local function functions() display.remove(angry) updateScore() end function onCollision(event) if event.phase == "began" then if event.target.myName == "angry" and event.other.myName == "happy" then functions() end end end angry:addEventListener( "collision", onCollision ) local function endGame() if secondsLeft == 0 then print("bye") Runtime:removeEventListener( "enterFrame", endGame ) end end Runtime:addEventListener( "enterFrame", endGame )

i get this error; http://i67.tinypic.com/2v7t99l.jpg

What is line 56? Why would angry be non-existant, non-initialized or recently deleted at that point in your code?

Hi,

I suspect that latest error is to do with this part of the code;

timer.performWithDelay( 5000, makeAngry, 50 ) makeAngry() timer.performWithDelay( 5000, angry, 50 ) angry()

where you still have ‘angry’ as a function (as discussed earlier, it cannot be a function and a variable).

Also, in regards to why only the newest angry moves, that is probably due to the fact that each new angry is simply called “angry”. So the moveAngry function you have will only ever refer to the most recently created angry - it won’t move all of the previous ‘angry’ instances.

I would suggest that you create the angry balls as a table; i.e.

angryCounter = 0 local function makeAngry() angryCounter = angryCounter + 1 local angry[angryCounter] = display.newCircle( 0, 0, 10 ) angry[angryCounter].x = 420 angry[angryCounter].y = 280 physics.addBody( angry[angryCounter], "static" ) angry[angryCounter]:setFillColor( 1, 0, 0 ) angry[angryCounter].myName = "angry" angryList[#angryList + 1] = angry[angryCounter] end

and then, when you move the angry balls around, you just loop through the angry table;

function moveAngry() for moveCounter = 1, angryCounter do transition.to(angry[moveCounter], {time=1000, x=math.random( centerX - actualW/2, centerX + actualW/2 ), y=math.random( centerY - actualH/2, centerY + actualH/2 )}) end timer.performWithDelay(1000,moveAngry) end

Note that since the transition.to will be called for each angry in the table, you cannot use onComplete anymore, hence adding a timer.performWithDelay instead.

I can’t guarantee that’ll all work - I just typed it up directly into this reply, but it should give you an idea of how to proceed.

I would also suggest you reconsider how you’re moving the angry balls around; since you have gravity set to zero, you might find it simpler to just set the velocity for each angry and then you don’t have to constantly repeat the transition.to. If you are using transition.to in order to ensure they don’t collide with each other, then you can use collision filters to set them to only interact with happy.

Cheers,

Simon

DixonCourtEntertainment

You can not use angry as both a function and variable:

local function angry() -\<----- used as a function here angry = display.newCircle( 0, 0, 10 )&nbsp; --\<------ used as a variable/object here.

I believe you have already asked this question before. Can you provide a better explanation on what you want to do?

Hello Rob,

Thanks for your post. I will try to give a better explanation:

I want that every few seconds there will be added a new “Angry”. The script i have build now added “angry” automatically. The first angry works fine, when “happy” touch “angry”, “angry” will be destroyd and my score goes +1. But all the other “angry” don’t work, if “happy” touch them there happens nothing. I have read that if you make a table this issue will be solve, i have seen many tutorials but i have no idea how to create one.

Maybe something like:

local angryList = {} local function makeAngry() local angry = display.newCircle( 0, 0, 10 ) angry.x = 420 angry.y = 280 physics.addBody( angry, "static" ) angry:setFillColor( 1, 0, 0 ) angry.myName = "angry" physics.addBody( angry, "static" ) angryList[#angryList + 1] = angry end timer.performWithDelay( 5000, makeAngry, 50 ) makeAngry()

Nope, that didn’t work. 

Explain? Are you getting errors? What are you expecting? What are you seeing?
 

We can’t really read your mind or see through your eyes. You have to use words and screen shots, and pasting in your code for us to help.

Rob

Hi Rob,

Yeah i didn’t explait it to well. So i will try it again, i hope this is will be clearer.

When i use this script: 

display.setStatusBar(display.HiddenStatusBar) local physics = require( "physics" ) physics.start() physics.setGravity(0, 0) local centerX = display.contentCenterX local centerY = display.contentCenterY local actualW = display.actualContentWidth local actualH = display.actualContentHeight local secondsLeft = 01 \* 60 -- 20 minutes \* 60 seconds local background = display.newRect( centerX, centerY, actualW, actualH ) background:setFillColor( 0, 0, 1 ) local score = 0 local scoreTxt = display.newText( "0", 0, 0, native.systemFontBold, 40 ) scoreTxt.x = centerX + actualW/2 - 20 scoreTxt.y = centerY - actualH/2 + 20 local function updateScore(event) score = score + 1 scoreTxt.text = string.format("%d", score ) end local clockText = display.newText("01:00", display.contentCenterX, 80, native.systemFontBold, 80) clockText:setFillColor( 0.7, 0.7, 1 ) local happy = display.newCircle( 0, 0, 10 ) happy.x = 45 happy.y = 45 physics.addBody( happy, "dynamic" ) happy:setFillColor( 0, 1, 0 ) happy.myName = "happy" local function angry() angry = display.newCircle( 0, 0, 10 ) angry.x = 420 angry.y = 280 physics.addBody( angry, "static" ) angry:setFillColor( 1, 0, 0 ) angry.myName = "angry" physics.addBody( angry, "static" ) end timer.performWithDelay( 5000, angry, 50 ) angry() local function updateTime() -- decrement the number of seconds secondsLeft = secondsLeft - 1 -- time is tracked in seconds. We need to convert it to minutes and seconds local minutes = math.floor( secondsLeft / 60 ) local seconds = secondsLeft % 60 -- make it a string using string format. local timeDisplay = string.format( "%02d:%02d", minutes, seconds ) clockText.text = timeDisplay end local countDownTimer = timer.performWithDelay( 1000, updateTime, secondsLeft ) function touchScreen(event) if event.phase == "began" then transition.to(happy, {time=1000, x=event.x, y=event.y}) end end Runtime:addEventListener( "touch", touchScreen ) function moveAngry() transition.to(angry, {time=1000, x=math.random( centerX - actualW/2, centerX + actualW/2 ), y=math.random( centerY - actualH/2, centerY + actualH/2 ), onComplete=moveAngry }) end moveAngry() local function functions() display.remove(angry) updateScore() end function onCollision(event) if event.phase == "began" then if event.target.myName == "angry" and event.other.myName == "happy" then functions() end end end angry:addEventListener( "collision", onCollision ) local function endGame() if secondsLeft == 0 then print("bye") Runtime:removeEventListener( "enterFrame", endGame ) end end Runtime:addEventListener( "enterFrame", endGame )

I get this screen: 

http://i66.tinypic.com/2mwrp5w.jpg (the image is very big so i don’t think it will be handy if i post it here)

So as you can see “angry” moves the hole time. When i go over “angry”, the score will be +1 and “angry” will be disappear. But than after a few seconds the other “angry’s” will be displayed. They also move the hole time. But when an other “angry” is created the previous “angry” will stop moving the hole time. Also when i go over the “angry’s” they didn’t disappear en the score don’t go +1.

what i want:

After a few seconds there will be a created a “angry” this one moves the hole time (also when other “angry’s” will be added in the game). Everytime when “happy” goes over “angry”, “angry” will be disappear and the score will be +1.

Can somebody please help me, i have tried months but the only thing that i receive is headache. 

@rob 

i have added your code in my code like this way:

display.setStatusBar(display.HiddenStatusBar) local physics = require( "physics" ) physics.start() physics.setGravity(0, 0) local centerX = display.contentCenterX local centerY = display.contentCenterY local actualW = display.actualContentWidth local actualH = display.actualContentHeight local secondsLeft = 01 \* 60 -- 20 minutes \* 60 seconds local background = display.newRect( centerX, centerY, actualW, actualH ) background:setFillColor( 0, 0, 1 ) local score = 0 local scoreTxt = display.newText( "0", 0, 0, native.systemFontBold, 40 ) scoreTxt.x = centerX + actualW/2 - 20 scoreTxt.y = centerY - actualH/2 + 20 local function updateScore(event) score = score + 1 scoreTxt.text = string.format("%d", score ) end local clockText = display.newText("01:00", display.contentCenterX, 80, native.systemFontBold, 80) clockText:setFillColor( 0.7, 0.7, 1 ) local happy = display.newCircle( 0, 0, 10 ) happy.x = 45 happy.y = 45 physics.addBody( happy, "dynamic" ) happy:setFillColor( 0, 1, 0 ) happy.myName = "happy" local angryList = {} local function makeAngry() local angry = display.newCircle( 0, 0, 10 ) angry.x = 420 angry.y = 280 physics.addBody( angry, "static" ) angry:setFillColor( 1, 0, 0 ) angry.myName = "angry" physics.addBody( angry, "static" ) angryList[#angryList + 1] = angry end timer.performWithDelay( 5000, makeAngry, 50 ) makeAngry() timer.performWithDelay( 5000, angry, 50 ) angry() local function updateTime() -- decrement the number of seconds secondsLeft = secondsLeft - 1 -- time is tracked in seconds. We need to convert it to minutes and seconds local minutes = math.floor( secondsLeft / 60 ) local seconds = secondsLeft % 60 -- make it a string using string format. local timeDisplay = string.format( "%02d:%02d", minutes, seconds ) clockText.text = timeDisplay end local countDownTimer = timer.performWithDelay( 1000, updateTime, secondsLeft ) function touchScreen(event) if event.phase == "began" then transition.to(happy, {time=1000, x=event.x, y=event.y}) end end Runtime:addEventListener( "touch", touchScreen ) function moveAngry() transition.to(angry, {time=1000, x=math.random( centerX - actualW/2, centerX + actualW/2 ), y=math.random( centerY - actualH/2, centerY + actualH/2 ), onComplete=moveAngry }) end moveAngry() local function functions() display.remove(angry) updateScore() end function onCollision(event) if event.phase == "began" then if event.target.myName == "angry" and event.other.myName == "happy" then functions() end end end angry:addEventListener( "collision", onCollision ) local function endGame() if secondsLeft == 0 then print("bye") Runtime:removeEventListener( "enterFrame", endGame ) end end Runtime:addEventListener( "enterFrame", endGame )

i get this error; http://i67.tinypic.com/2v7t99l.jpg

What is line 56? Why would angry be non-existant, non-initialized or recently deleted at that point in your code?

Hi,

I suspect that latest error is to do with this part of the code;

timer.performWithDelay( 5000, makeAngry, 50 ) makeAngry() timer.performWithDelay( 5000, angry, 50 ) angry()

where you still have ‘angry’ as a function (as discussed earlier, it cannot be a function and a variable).

Also, in regards to why only the newest angry moves, that is probably due to the fact that each new angry is simply called “angry”. So the moveAngry function you have will only ever refer to the most recently created angry - it won’t move all of the previous ‘angry’ instances.

I would suggest that you create the angry balls as a table; i.e.

angryCounter = 0 local function makeAngry() angryCounter = angryCounter + 1 local angry[angryCounter] = display.newCircle( 0, 0, 10 ) angry[angryCounter].x = 420 angry[angryCounter].y = 280 physics.addBody( angry[angryCounter], "static" ) angry[angryCounter]:setFillColor( 1, 0, 0 ) angry[angryCounter].myName = "angry" angryList[#angryList + 1] = angry[angryCounter] end

and then, when you move the angry balls around, you just loop through the angry table;

function moveAngry() for moveCounter = 1, angryCounter do transition.to(angry[moveCounter], {time=1000, x=math.random( centerX - actualW/2, centerX + actualW/2 ), y=math.random( centerY - actualH/2, centerY + actualH/2 )}) end timer.performWithDelay(1000,moveAngry) end

Note that since the transition.to will be called for each angry in the table, you cannot use onComplete anymore, hence adding a timer.performWithDelay instead.

I can’t guarantee that’ll all work - I just typed it up directly into this reply, but it should give you an idea of how to proceed.

I would also suggest you reconsider how you’re moving the angry balls around; since you have gravity set to zero, you might find it simpler to just set the velocity for each angry and then you don’t have to constantly repeat the transition.to. If you are using transition.to in order to ensure they don’t collide with each other, then you can use collision filters to set them to only interact with happy.

Cheers,

Simon

DixonCourtEntertainment