hi,
i have a problem with my enemy. When i use one enemy, (not a table) i have no problem with this code below. But when i use a table with my enemy the function enemy[i].attack move my character hyper faster and after a delay my enemy dissappears.
Could you tell me why ? Thanks
--parameters.lua local M={} M.W=display.contentWidth M.W2=display.contentWidth\*.5 M.H=display.contentHeight M.H2=display.contentHeight\*.5 M.number={} M.number.enemy=1 M.cnt={} M.position={} M.current={} M.multiplier=10000000000000000000 return M
-------------------------------------------------------------------------------- -- main.lua -------------------------------------------------------------------------------- local physics = require("physics") local P =require("parameters") local grid=require("grid") physics.start() physics.setScale(5) physics.setGravity(0,0) -------------------------------------------------------------------------------- --group -------------------------------------------------------------------------------- local backgroundGroup=display.newGroup() local enemyGroup=display.newGroup() local playerGroup=display.newGroup() local hudGroup=display.newGroup() -------------------------------------------------------------------------------- --variables -------------------------------------------------------------------------------- local borders={} local background local grid={} local bonus local weapon local obstacle={}--obstacles durs et picots local player local enemy={} local hud={}-- local viseur -------------------------------------------------------------------------------- -- draw Elements -------------------------------------------------------------------------------- local function drawPLayer() player=display.newCircle(playerGroup,P.W2,P.H2,10) player:setFillColor(1,0,0) player.name="player" player.alpha=1 player.isVisible=true player.isHit=false player.isMoving=false player.float=function() if player.isMoving==false then choix={-19,19} player:applyLinearImpulse(choix[math.round(math.random(1,2))],choix[math.round(math.random(1,2))]) end end timer.performWithDelay(1000,player.float,-1) physics.addBody(player,{density=10,friction=0,bounce=0}) end local function drawEnemy() enemyGroup.body=display.newCircle(enemyGroup,P.W2+30,P.H2,5) enemyGroup.body:setFillColor(0,0,0) for i=1,P.number.enemy do enemy[i]=enemyGroup physics.addBody(enemy[i],{density=1,friction=0,bounce=.50}) enemy[i].name="enemy" enemy[i].alpha=1 enemy[i].isHit=false enemy[i].isDead=false enemy[i].isVisible=true enemy[i].isMoving=false enemy[i].attack=function() if enemy[i].isDead == false then local attack={} attack.direct=function() enemy[i]:setLinearVelocity((player.x-enemy[i].x)\*P.multiplier,(player.y-enemy[i].y)\*P.multiplier) end attack.zigzag=function() enemy[i]:applyLinearImpulse((player.x-enemy[i].x),(player.y-enemy[i].y)) timer.performWithDelay(100,function() enemy[i]:applyLinearImpulse((player.x-enemy[i].x),(player.y-enemy[i].y)) end) timer.performWithDelay(200,function() enemy[i]:applyLinearImpulse((player.x-enemy[i].x),(player.y-enemy[i].y)) end) end attack.feinte=function() enemy[i]:applyLinearImpulse((player.x-enemy[i].x+200)\*P.multiplier\*.5,(player.y-enemy[i].y-200)\*P.multiplier\*.5) timer.performWithDelay(500,function() enemy[i]:applyLinearImpulse((player.x-enemy[i].x)\*P.multiplier,(player.y-enemy[i].y)\*P.multiplier) end) end attack.repos=function() end attack.crazy=function() enemy[i]:applyLinearImpulse(math.random(100,P.W-100),math.random(100,P.H-100)) end choix=math.round(math.random(1,5)) if choix==1 then print("direct") attack.direct() elseif choix==2 then print("zigzag") attack.zigzag() elseif choix==3 then print("feinte") attack.feinte() elseif choix==4 then print("repos") attack.repos() elseif choix==5 then print("crazy") attack.crazy() end end end timer.performWithDelay(3000,enemy[i].attack,-1) timer.performWithDelay(500,function() enemy[i].isMoving=true end,-1) timer.performWithDelay(1500,function() enemy[i].isMoving=false end, -1) u=0 enemy[i].damping=function() u=u+.19 print(u) if enemy[i].isMoving then enemy[i].linearDamping=u else enemy[i].linearDamping=0 u=0 end end timer.performWithDelay(100,enemy[i].damping,-1) end end
drawPlayer() drawEnemy()