wtf with my regeneration

I think what’s important to remember, as well, is that this isn’t a problem that is impossible to solve. It’s not like you’ve stumbled upon some brand new concept in the “mobile game physics body regeneration” space. What you are attempting to accomplish is possible, but not in the way you attempting to accomplish it currently.

Your best bet is to stop development on your current project, create a new project that JUST focuses on the problem you are encountering here. Work on that until you feel you can solve the concept and get it working reliably for that small mini-project. Then you can put the logic you created in the mini-project, into your larger project.

I realize this might seem pointless, but I guarantee that it will help you figure out the problem, and help you to understand the best way to debug other problems that will inevitably arise as well. 

Good luck!

Alex@Panc

You have not read my previous posts …

Everything works … but when i reloading all variables ,then they are  switched off in many areas of physics … all … but i localized this problems, but in the cycle this problems is not solved …

roaminggamer

I will try to edit the code as you advised…

However, Lua … … me a little alien with a = function () … it’s like in js … but I avoid such constructions, because i don’t like their…

Yeah, you don’t have to do it the way I showed.  If you have a function A that is called by B simply declare A before B and you’re fine too:

local function a() print("a") end local function b() a() end

I showed you how to use ‘forward declaration’ because that helps solve sticky issues where resolving scope by declaration order is not feasible.

If Lua is foreign to you, I’d strongly suggest learning more about it before jumping into developing a game.

Simply take some online tutorials or buy a book that teaches Lua and use Corona to learn in.

You can put anything in main.lua, so simply writing Lua programs in main.lua and learning Lua first is completely doable.

Once you understand Lua, you’ll have a much better foundation for playing with Corona features.

Lua and JavaScript are not that drastically different.  In JS you use { and } to mark the beginning and ending of blocks.  You would write:

function myFunction( someparamters ) {

      // do stuff

}

in Lua thats

function myFunction( some paramters )

     – do stuff

end

They are very similar.  In Javascript you indent each block of code so you can match your closing braces } with the block that started it (function, if, for, etc.).  In Lua you do the same with “end” instead of }

I looked at your code.  It’s really, really hard to read.  Between the indenting problem where we can’t tell where one function begins or ends and the functions within functions I can’t really help.

What part of your code is resetting the game?  What function is doing this work?

Rob

@gleb269,

I’m dying to see this resolved, but you’ll have to help us help you.  The primary issue is that your code is not legible.  If I could simply read the code without struggling I’d probably be able to help.

Let me show you what I mean.  

This is well formatted code:

local function quad( a, b, c ) local sol if( math.abs( a ) \< tiny ) then if( math.abs( b ) \< tiny ) then if( math.abs( c ) \< tiny ) then sol = { x = 0, y = 0 } else sol = nil end else sol = { x = -c / b, y = -c / b } end else local disc = b \* b - 4 \* a \* c if (disc \>= 0) then disc = math.sqrt( disc ) a = 2 \* a sol = { x = ( -b - disc ) / a, y = ( -b + disc ) / a } end end return sol end 

This example of what your code is like:

local function quad( a, b, c ) local sol if (math.abs(a) \< tiny) then if (math.abs(b) \< tiny) then if (math.abs(c) \< tiny) then sol = {x=0, y=0} else sol = nil end else sol = {x=-c/b, y=-c/b} end else local disc = b\*b - 4\*a\*c if (disc \>= 0) then disc = math.sqrt(disc) a = 2\*a sol = { x=(-b-disc)/a, y=(-b+disc)/a } end end return sol end 

To be clear:  I’m not showing you this to make fun of you or shame you.  I just want you to see the difference and to help you understand our plight.  

If you look at both samples above, you’ll notice something.  Although you probably don’t know what the function does right away, you at least have the possibility of figuring it out by looking at the formatted one.  The un-formatted one is impenetrable, even though it is the same code.

So, please take some time to indent your code and to get rid of unnecessary blank lines.

The repost the link.  Then we’ll have a chance of resolving this.

Thanks,

Ed

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