Hi everyone i’m new to corona. Thomas i was wondering how does your method work because i have tried applying it in my game but the actual movement of the enemy doesn’t work. Here’s my code to help me out please.
local Ennemi = {}
function Ennemi:new(spawnerX,spawnerX,vie,cible)
– ennemi = l’intance d’un ennemi
local ennemi = display.newCircle(0, 0, 15)
randomSpawn = math.random(1, #spawnLocation)
spawner = spawnLocation[randomSpawn]
spawnerX = spawner.x
spawnerY = spawner.y
local coteX = personnage.x - ennemi.x
local coteY = personnage.y - ennemi.x
local distance = math.sqrt(coteX^2 + coteY^2)
speed = 0.2
local transitionTime = (distance)/speed
angleRad = math.atan2(coteY,coteX)
angleDeg = math.deg(angleRad)
ennemiSpeed = 0.5
function ennemi:init()
– dans une methode (objet:function()) on peut utiliser ‘self’ pour parler de l’objet (dans notre cas: self = ennemi) : avantage: code pas mal plus réutilisable
self.x = spawnerX
self.y = spawnerY
self.vie=vie
camera:add(ennemi,1,false)
physics.addBody( self, { density=1.0, friction=0.3, bounce=0.2, radius=15 } )
self:setFillColor(1, 0, 0, 1)
self:addEventListener( “collision”, self )
–self:bouge()
function getAngle()
ennemi:rotate(angleDeg)
self:move()
end
timer.performWithDelay(300, getAngle, 1)
end
–local function rebouge()
–transition.moveTo( self, { x=personnage.x, y=personnage.y, time=transitionTime , onComplete=bouge} )
–ennemi:bouge()
–end
–function ennemi:bouge()
–transition.moveTo( self, { x=personnage.x, y=personnage.y, time=transitionTime, onComplete=rebouge } )
–end
function ennemi:move()
ennemi.x = ennemi.x + math.cos(ennemi.rotation)*ennemiSpeed
ennemi.y = ennemi.y + math.sin(ennemi.rotation)*ennemiSpeed
end
function ennemi:collision(event)
if(event.other==bullet) then
self:removeSelf()
display.remove(bullet)
scoreBase = scoreBase+1
score.text = scoreBase
elseif (event.other==personnage)then
resetScore()
end
end
sceneGroup:insert(ennemi)
ennemi:init()
– une fois qu’on a fabriqué l’ennemi, on le ‘return’
return ennemi
end