enemy moving and attacking

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

in the last block of code that you show in your post, the  ‘elseif’ needs some code (a condition to test) and  a  ‘then’  at the end of that line of code.

something like this:  

elseif self.x   >  finalSpotXR then 

if you read the end of the error message carefully it spells out what the error is and what line it is on in your enemy.lua

good luck

Bob

Thanks man, that seems to have solved that problem.

now that I have put the condition to test I am getting another error reading

Attempt to index a nil value

File: ?

stack traceback:

?: in function ‘cancel’

Enemy.lua:53: in function <Enemy.lua:24>

?: in function <?:221>

 

 

it sounds like something is wrong with the timer.cancel function. any idea on what to do with this?

 

Sorry for the questions, I’m pretty new to this and still learning how to piece it all together

so it is something with the cancel.timer function. I commented it out and placed a print function in there and it ran just fine printing what I had put.

I guess I read more into the api and see what I can find

the error is telling you that  ‘attackTimer’ which is a handle to the function call  ‘timer.performWithDelay…’ is nil.  Guessing you cancelled it previously or the elseif condition in that block of code is called before the handle attackTimer ever gets assigned.   Some coders will always check to see if the handle is  ‘not nil’  before calling the timer.cancel( timer handle)

if attackTimer ~= nil then

     timer.cancel(attackTimer)

     attackTimer = nil

end

ideally you may need to post more complete segments of code to get more solid answers to various issues as they may pop-up.  As you are fairly new to this, you will certainly run into more bugs… so more compete sections of code will be helpful.

in the last block of code that you show in your post, the  ‘elseif’ needs some code (a condition to test) and  a  ‘then’  at the end of that line of code.

something like this:  

elseif self.x   >  finalSpotXR then 

if you read the end of the error message carefully it spells out what the error is and what line it is on in your enemy.lua

good luck

Bob

Thanks man, that seems to have solved that problem.

now that I have put the condition to test I am getting another error reading

Attempt to index a nil value

File: ?

stack traceback:

?: in function ‘cancel’

Enemy.lua:53: in function <Enemy.lua:24>

?: in function <?:221>

 

 

it sounds like something is wrong with the timer.cancel function. any idea on what to do with this?

 

Sorry for the questions, I’m pretty new to this and still learning how to piece it all together

so it is something with the cancel.timer function. I commented it out and placed a print function in there and it ran just fine printing what I had put.

I guess I read more into the api and see what I can find

the error is telling you that  ‘attackTimer’ which is a handle to the function call  ‘timer.performWithDelay…’ is nil.  Guessing you cancelled it previously or the elseif condition in that block of code is called before the handle attackTimer ever gets assigned.   Some coders will always check to see if the handle is  ‘not nil’  before calling the timer.cancel( timer handle)

if attackTimer ~= nil then

     timer.cancel(attackTimer)

     attackTimer = nil

end

ideally you may need to post more complete segments of code to get more solid answers to various issues as they may pop-up.  As you are fairly new to this, you will certainly run into more bugs… so more compete sections of code will be helpful.