I am trying to create a way to have enemies move towards my player and then attack once they get to a certain position.
this is the code that I have so far
function enemyFollow(self, event) local finalSpotXR = self.target.x local finalSpotXL = self.target.x local finalSpotY = self.target.y local x1 = self.x local y1 = self.y local x2 = self.target.x local y2 = self.target.y local deltaY = y2 - y1 local deltaX = x2 - x1 local dist = math.sqrt(( deltaX \* deltaX ) + ( deltaY \* deltaY )) if self.x \>= self.target.x then transition.moveTo (self, {x=finalSpotXR, y=finalSpotY, time= dist/enemy.speed}) elseif self.x \< self.target.x then transition.moveTo (self, {x=finalSpotXL, y=finalSpotY, time= dist/enemy.speed}) end local attackTimer if self.x == finalSpotXR and self.y == finalSpotY then attackTimer = timer.performWithDelay( 1000, enemy:attack(target), 0 ) elseif timer.cancel( attackTimer ) end end enemy.enterFrame = enemyFollow -- moves enemies Runtime:addEventListener("enterFrame", enemy) -- moves enemies
This is the error I get
Error loading module ‘Enemy’ from file ‘/Applications/CoronaSDK/SampleCode/Interface/SlideView/BH Arena Game/Enemy.lua’:
/Applications/CoronaSDK/SampleCode/Interface/SlideView/BH Arena Game/Enemy.lua:54: ‘then’ expected near ‘end’
File: error loading module ‘Enemy’ from file ‘Enemy.lua’
I do want to note that I am using a form of OOP that I found on the forum, so the above code is in my enemy.lua and at the beginning of my main.lua file I call it with
local Enemy = require("Enemy")
I have narrowed down the error to be caused by the timer that I have set for the attacking
local attackTimer if self.x == finalSpotXR and self.y == finalSpotY then attackTimer = timer.performWithDelay( 1000, enemy:attack(target), 0 ) elseif timer.cancel( attackTimer ) end
When I comment out the code for the timer, my enemies will move to my player just fine.
Any help would be awesome