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)