wtf with my regeneration

Hello everyone

I have 21 hours I sit and think about how to solve the problem …

physics addbody can not be called when the world is locked

this message, the debugger writes to me

The problem is that before the regeneration, it works, and then … some **** happens

http://www.youtube.com/watch?v=KkHAiiRZbNI&feature=youtu.be

Here I have shown what is the problem… help

Please post the code that is causing your issue, so that we can evaluate and provide assistance.

If you’re trying to create a physics body during a collision event, you’ll get that message:

local function onCollision( event ) if(event.phase == "began" ) then local tmp = display.newCircle( 100, 100, 10) phyics.addBody(tmp) end end

So, delay the creation like this:

local function onCollision( event ) if(event.phase == "began" ) then timer.performWithDelay( 1, function() local tmp = display.newCircle( 100, 100, 10) phyics.addBody(tmp) end ) end end
function create\_mob() physics.getDebugErrorsEnabled() vragsship = {} kolvomobs = 1 for u = 0, 11 do if lvls[num\_lvl][wave][u + 1] == 1 then vragsship[kolvomobs] = display.newImageRect("boyship.png", 25, 25) vragsship[kolvomobs].x = \_W + 100 vragsship[kolvomobs].y = vragsship[kolvomobs].width / 2 + u \* 26 vragsship[kolvomobs].pointsarmer = 4 physics.addBody(vragsship[kolvomobs], "static") -- timer.performWithDelay( 60, physics.addBody(vragsship[kolvomobs], "static") ) vragsship[kolvomobs]:addEventListener("collision", onCollision) vragshipsmoves[kolvomobs]= transition.to(vragsship[kolvomobs], {time = 1500, x = \_W}) kolvomobs = kolvomobs + 1 end kolvovragov = kolvomobs - 1 end end create\_mob()

If I call this code the first time, everything is working, then the second - stops working …

display.setStatusBar(display.HiddenStatusBar) \_W = display.contentWidth \_H = display.contentHeight system.activate("multitouch") local lvls = { { {0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0}, {0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0}, {1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0}, {1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0}, {1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1}}, { {1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1}, {0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0}, {0, 1, 0, 1, 1, 1, 0, 1, 1, 1,1, 0}, {1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0}, {1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}} } local physics = require("physics") function gengame(num\_lvl) local vsegovragov = 6 local popadaniyavkorabli = {} local vragsship = {} physics.start() physics.setGravity(0, 0) local bg = display.newRect(0, 0, \_W + 300, \_H) bg.x = bg.x + bg.width / 2 - 50 bg.y = bg.y + bg.height / 2 bg:setFillColor( 1,1,1 ) bg:toBack() local maxHealth local currentHealth local score local stenkablockd local stenkablocku local stenkablockl local stenkablockr local healthBar local damageBar local projectiler local patr local projectile local projectiler local contents local file local prefix local json local pex local image local medic local temp local randomStar local star1 local star2 local star3 local kolvomobs local wave local vragsship = {} local wavemax local kolvovrago local maxHealth=100 local currentHealth =2 local score =0 local aTimers = {} local starMove local vragshipsmoves={} local medic\_transmove local patr\_transmove local starsnach=true wave = 1 wavemax = #lvls[num\_lvl] kolvovragov = 0 --print(#lvls[num\_lvl]) function create\_mob() physics.getDebugErrorsEnabled() vragsship = {} kolvomobs = 1 for u = 0, 11 do if lvls[num\_lvl][wave][u + 1] == 1 then vragsship[kolvomobs] = display.newImageRect("boyship.png", 25, 25) vragsship[kolvomobs].x = \_W + 100 vragsship[kolvomobs].y = vragsship[kolvomobs].width / 2 + u \* 26 vragsship[kolvomobs].pointsarmer = 4 physics.addBody(vragsship[kolvomobs], "static") -- timer.performWithDelay( 60, physics.addBody(vragsship[kolvomobs], "static") ) vragsship[kolvomobs]:addEventListener("collision", onCollision) vragshipsmoves[kolvomobs]= transition.to(vragsship[kolvomobs], {time = 1500, x = \_W}) kolvomobs = kolvomobs + 1 end kolvovragov = kolvomobs - 1 end end local starTable = {} initStar = function() -- upvalues: starTable star1 = {} star1.imgpath = "star1.png" star1.movementSpeed = 10000 table.insert(starTable, star1) star2 = {} star2.imgpath = "star2.png" star2.movementSpeed = 12000 table.insert(starTable, star2) star3 = {} star3.imgpath = "star3.png" star3.movementSpeed = 14000 table.insert(starTable, star3) end local gamersstars = display.newGroup() function onclo(e) if(e.target.name=="stenkablockl") then -- print("l") display.remove(e.other) e.other = nil end if(e.target.name=="stenkablockr") then -- print("r") display.remove(e.other) e.other = nil end if(e.target.name=="stenkablocku") then -- print("u") display.remove(e.other) e.other = nil end if(e.target.name=="stenkablockd") then --print("d") display.remove(e.other) e.other = nil end end local stckblck = display.newGroup() stenkablockl=display.newRect(-100,-50, 10,\_H+250) stenkablockl.anchorY=0 --physics.addBody(stenkablockl, "static") stenkablockl.name="stenkablockl" stenkablockl:addEventListener("collision", onclo) stenkablocku=display.newRect(-100,-50, \_W+250,10) stenkablocku.anchorX=0 stenkablocku.anchorY=0 --physics.addBody(stenkablocku, "static") stenkablocku.name="stenkablocku" stenkablocku:addEventListener("collision", onclo) stenkablockd=display.newRect(0,\_H+50, \_W+150,10) stenkablockd.anchorX=0 stenkablockd.anchorY=0 -- physics.addBody(stenkablockd, "static") stenkablockd.name="stenkablockd" stenkablockd:addEventListener("collision", onclo) stckblck:insert(stenkablockd) stenkablockr=display.newRect( \_W+150,-50, 10,\_H+250) stenkablockr.anchorX=0 stenkablockr.anchorY=0 --physics.addBody(stenkablockr, "static") stenkablockr.name="stenkablockr" stenkablockr:addEventListener("collision", onclo) -- update the damage bar healthBar = display.newRect(50,45,maxHealth,10) healthBar:setFillColor( 000/255, 255/255, 0/255 ) healthBar.strokeWidth = 1 healthBar:setStrokeColor( 255, 255, 255, .5 ) -- create red damage bar (-create it second so it lays on top) damageBar = display.newRect(50+maxHealth,45, 0,10) damageBar:setFillColor( 255/255, 0/255, 0/255 ) healthBar:toFront() damageBar:toFront() function updateDamageBar() damageBar.x = 50+currentHealth / 2 damageBar.width = maxHealth - currentHealth if(currentHealth\<=0) then --physics.pause() killgame() gengame(2) end --print(currentHealth) end scores = display.newText( "Вы набрали: 0 очков", 150,45, native.systemFontBold, 16 ) scores.anchorX=0 scores:setFillColor( 1, 1, 0.5 ) local function podrgr2() -- physics.addBody( medic, 'static' ) -- medic:addEventListener("collision", onclomed) end -- patr.predn = "patr" local medicrest=false function onclomed(e) print("sheger"..e.other.x) if e.other.x\<300 then e.target:removeSelf() e.target = nil currentHealth=currentHealth+25 medicrest=false end end function onCollision(e) targetX = e.target.x targetY = e.target.y if e.target.pointsarmer == 0 then kolvovragov = kolvovragov - 1 score=score+15 scores.text = "Вы набрали: "..score.." очков" display.remove(e.target) e.target = nil else e.target.pointsarmer = e.target.pointsarmer - 1 end if(e.target~=nil) then if e.target.pointsarmer \< -1 then currentHealth=currentHealth-1 updateDamageBar() --аптечка local function medicdel() display.remove(medic) medic = nil end if(currentHealth~=nil) then if(currentHealth\<=20) then if(kolvovragov\>=6) then -- аптечка if(medicrest==false) then medic = display.newRect( \_W+150, math.random(15,\_W-15), 15, 15 ) timer.performWithDelay(80, podrgr2) medic\_transmove = transition.to(medic, {time = 15000, x = -50}, medicdel) medicrest=true end end end end e.target.pointsarmer=e.target.pointsarmer+1 elseif(e.target.pointsarmer == -1) then display.remove(e.target) e.target = nil --конец игры --physics.pause() print(" the game is over") killgame() end end if kolvovragov == 0 then if wave \<= wavemax then wave = wave + 1 aTimers[0]=timer.performWithDelay(300, create\_mob, 1) end end if(wavemax~=nil) then if wavemax \< wave then print(" the game is over") storyboard.gotoScene("tragi") end display.remove(e.other) e.other = nil end end image = display.newImageRect("glavnycorable.png", 30, 30) image.x = display.contentCenterX image.namers="gg" image.y = display.contentCenterY image:toFront() image:addEventListener("collision", onCollision) image.pointsarmer = -80 function podgruzka() physics.addBody(stenkablockl, "static") physics.addBody(stenkablockr, "static") physics.addBody(stenkablocku, "static") physics.addBody(stenkablockd, "static") physics.addBody(healthBar, "static") physics.addBody(image, "static") end timer.performWithDelay(50, podgruzka) local jslib = require("simpleJoystick") local js = jslib.new(20, 60) js.x = 40 js.y = display.contentHeight - 70 function catchTimer(e) angle=js:getAngle() rangle=angle/57.29577951 speed=5\*js:getDistance() scalex=math.cos(rangle) scaley=math.sin(rangle) velocityx=speed\*scalex velocityy=speed\*scaley if(image.x \>= \_W) then if(velocityx \< 0) then image.x=image.x+velocityx end elseif(image.x \<= 0) then if(velocityx\>0) then image.x=image.x+velocityx end else image.x=image.x+velocityx end if(image.y\>=\_H-image.width/2) then if(velocityy\>0) then image.y=image.y-velocityy end elseif(image.y\<=0+image.width/2) then if(velocityy\<0) then image.y=image.y-velocityy end else image.y=image.y-velocityy end end js:activate() aTimers[1]=timer.performWithDelay(10, catchTimer, -1) create\_mob() local gamershoot = display.newGroup() function vragshoot( event ) if(#vragsship\>0) then for u=0,11 do if(vragsship[u+1]~=nil) then if(vragsship[u+1].width~=nil) then projectile = display.newRect( vragsship[u+1].x-vragsship[u+1].width/2-50, vragsship[u+1].y, 10, 10 ) projectile:setFillColor(1, 0, 0) physics.addBody(projectile, "dynamic", {density = 25}) projectile.gravityScale = 0 projectile.isBullet = true projectile:setLinearVelocity( -200, 0 ) gamershoot:insert( projectile) end end end end end local gamershoots = display.newGroup() aTimers[2]=timer.performWithDelay(1500, vragshoot, -1) function shoot( event ) if ( event.phase == 'began' ) then projectiler = display.newRect( image.x+50, image.y, 5, 5 ) physics.addBody( projectiler, 'dynamic' ) projectiler.gravityScale = 0 projectiler.isBullet = true projectiler:setLinearVelocity( 600, 0 ) gamershoots:insert( projectiler) end end Runtime:addEventListener("touch", shoot) function patrdel() removeSelf(patr) patr = nil end function create\_patr() if(kolvovragov\>=6) then if(currentHealth\<50) then -- аптечка patr = display.newRect( \_W+150, math.random(15,\_W-15), 15, 15 ) physics.addBody( patr, 'static' ) patr.pointsarmer=3 patr.predn = "patr" patr\_transmove = transition.to(patr, {time = 7000, x = -50}, medicdel) end end end aTimers[3] = timer.performWithDelay(180000, create\_patr, -1) function killgame() print("stisl") bg:removeSelf() bg=nil maxHealth=100 currentHealth =100 if(patr~=nil) then patr:removeSelf() patr = nil end transition.cancel( starMove ) transition.cancel(patr\_transmove ) transition.cancel(medic\_transmove ) for u = kolvomobs, 1 do transition.cancel(vragshipsmoves[u] ) end if(#vragsship\>0) then for u=0,11 do if(vragsship[u+1]~=nil) then if(vragsship[u+1].width~=nil) then vragsship[u+1]:removeSelf() vragsship[u+1]=nil end end end end vragsship=nil image:removeEventListener("collision", onCollision) --stenkablockl:removeEventListener("collision", onclo) --stenkablockr:removeEventListener("collision", onclo) -- stenkablocku:removeEventListener("collision", onclo) -- stenkablockd:removeEventListener("collision", onclo) for u = kolvomobs, 0 do vragsship[u]:removeEventListener("collision", onCollision) end gamershoots:removeSelf() gamershoot:removeSelf() gamershoots = nil gamershoot = nil stckblck:removeSelf() stckblck = nil --timer.performWithDelay(10, physics.pause, 1) timer.cancel(aTimers[1]) timer.cancel(aTimers[2]) timer.cancel(aTimers[3]) stenkablockd:removeSelf() stenkablocku:removeSelf() stenkablockl:removeSelf() stenkablockr:removeSelf() healthBar:removeSelf() damageBar:removeSelf() Runtime:removeEventListener( "touch", shoot ) js:removeSelf() js=nil image:removeSelf() maxHealth = nil currentHealth = nil score = nil stenkablockd = nil stenkablocku = nil stenkablockl = nil stenkablockr = nil healthBar = nil damageBar = nil projectiler = nil if(projectile ~= nil)then --projectile:removeSelf() projectile = nil end file = nil prefix = nil json = nil pex = nil --image = nil medic = nil temp = nil kolvomobs = nil wave = nil wavemax = nil kolvovrago = nil scores:removeSelf() scores = nil score=0 end end gengame(1)

it’s full code…

help)

roaminggamer,

it is not quite that look at the code,

By the way I have shown in the video work with delay and without

in fact, nothing is clear …

hmm …

Arrays that I declare in the cycle and in the function remain empty, even though they were filled in the cycle

aMiXwecPY5M.jpg

HELP ME ANYBODY!

Based on the screen shot, I would guess you’re not calling create_mob().  You define a function but don’t run it.   Those two print statements right after the function will be 0 until you actually run create_mob().

Rob

Rob Miracle,

I’m not so stupid, look at the code, it’s all there!

Hi.  I haven’t answered back for the following reasons:

  1. I didn’t ask for a whole code dump.  Honestly, I don’t have time to read that much code and puzzle through it.  I almost did, but…

  2. No effort went into making that code legible.  What is up with the indentation, and massive gaps?  You’re asking for me to spend my precious time (for free) to work out the issue.  Don’t make me work so hard at it.  ( See formatting tip below )

  3. I can’t run the code since I don’t have the resources (PNG).

  4. Actually, I tried to run the code, but its impossible to figure out where one function starts and another ends.

  5. You’re creating global functions inside of global functions.  I’m not clear why, but I see people do this all the time.  It will create a scoping nightmare very quickly.

I suggest doing the following:

  1. Start over.   I do this all the time.    My general approach to coding is: 

a. Quick and dirty pass - The mind dump.

b. Organize and re-write - Re-write #1.  I break things down into modules, throw away dead code, and clean up a lot.

c. Refine - Re-write #2 Focus on individual components and modules.  Make them cleaner, better, faster, safer. In this step I re-write the bad parts of my code.

  1. Write individual functions (not functions in functions).

  2. Clean up the number of variables in use (seems like there are a lot).

  3. Clean up the scoping.

Here is how I’d structure the code.  If you know there will be functions that need to call other functions do this:

-- Requires -- local physics = require "physics" -- System init -- system.activate("multitouch") -- Locals (declare locals first) -- local maxHealth local currentHealth local score ... etc. -- Forward declarations (declare the functions first) -- local myFunction local myOtherFunction -- Define Functions -- myFunction = function( arg1, arg2, ... ) myOtherFunction() -- Calls other function here end myOtherFunction= function( ) ... end myFunction( 1, 2, 3 )

(Code) Formatting Tip

You should never just grab the code from your editor and paste it into a code block (<>) in the forums.  Instead do this:

  1. Open a plain text editor (like notepad.exe in Windows)
  2. Paste your code in there.
  3. Do a ‘global search and replace’ and replace all TAB characters with three spaces.  (Tabs do not align nicely in forums code blocks).
  4. Go through and clean up the indentation and extra gaps between lines.
  5. Make it legible.
  6. Paste it to a code block in the forums.
  7. Finish your post.
  8. Post it.
  9. Go back and read it.  Can it be read?  If not, fix it.

Hi again.  

I just want to say.  I know I sometimes come across as harsh, but I really want to help folks.  

I just need to balance how much time I spend helping others (for free) versus working for $.

Digging through a lot of code to fix a problem is sometimes over my threshold.  So, I (strongly) encourage folks to follow this mantra when asking for help.  Be:

  • Clear - Make your question as accessible and understandable as possible.  Make sure we’re able to understand what you need.

  • Concise - Post the bare minimum needed to ask the question and no more.  None of us has time to read lots of code or dig through long paragraphs.

  • Precise - Only post material directly related to the question.  Don’t talk about unrelated content.  It’s confusing and distracting.

And as I said, based on the “screen shot” you’re printing the values after defining the function but before running it.  Move your prints to after you run it.

Ed’s right.  Your code is very hard to read and follow.  The community wants to help, but its really hard when we have to fight formatting issues too.

Rob

Remarks taken

i send in the evening formatted and working code of project :slight_smile:

thanks

I clear all tabs for you …

The project is attached.

I know I can’t read .rar files.  If you want to compress up a whole folder, please use .zip.  But still this is probably more code not less, more concise code.

Rob

Hi I took a quick look at the contents of the RAR, but I can’t make heads or tails of it.  There is NO indentation at all, everything is aligned left.  Is this on purpose?  Whatever the case, it makes the code way too hard to read.

Also, I noticed that you’ve got a function,

function gengame(num\_lvl) ... end

and in this function you create a bunch of other (global) functions.  This just isn’t going to work.  I mean it could work but it would be so prone to error that I’d suggest again doing this:

  1. Start over.

  2. Write individual functions, no functions in functions.

  3. Use forward declaration (as shown above) to provide access to functions that need to be called in other functions.

  4. Reduce as much as possible.  

I think if you do this, the problem will either go away or be much easier to find.

That’s the best I can do for you.

Rob, I have attached a zip file to the old post

roaminggamer

Objects that are used in functions are local and Lua complains when the functions are global, all the more so in the function gen () I initialize function, and then delete it

and code with tabs

Guys… i need in yours help

I can’t afford the time to help.  I’ve looked at your latest download and it still has the same issues I’ve asked you to address:

  • Creates global functions inside other functions for no discernible reason
  • multiple blank lines for no reason
  • erratic indentation

I have to make a living and digging through this will take at least an hour to work out what is intended and what is happening. 

I want to help you, but I want to pay my mortgage more.  Sorry boss.

Note: Above you said something about locals and globals I didn’t understand, but I think you’re misunderstanding the concept.

Please see below:

-- The wrong way to write code and these are all global functions -- function a() function b() print("b") end b() end function c() a() end c() -- creates b() and prints "b" c() -- creates b() AGAIN (wipes out old one) b() -- prints "b"; function was created in call to c() --\> call to a() 

-- The right way to write code/functions when you need to call them from each other -- -- Forward declarations local a local b local c -- Definitions a = function() b() end b = function () print("b") end c = function() a() end c() c() b()