Abot enemy AI

Hi, I’m currently working on a top down game, where the enemy follow the player and attacks him.

 

I’m having trouble coding the enemy AI to keep following the player.

 

I think enterFrame and keep updating the player’s position might be the solution of this, but I’m not good at doing “enterFrame” things. I’m not sure if I’m in the right direction,

 

Can anyone give me some basic ideas how to code the enemy AI or give me an example?

 

Thanks a lot 

Hi.  I can’t give you an exact answer, but I can point you in the right direction:

I have coded seeking enemies in at least two games made for the Corona Geek hangouts.  You can get the full code to these games here:

Also here are links to some of the ‘enemy code including seeking logic’:

There are a few heavy duty topics involved, so if this is way too much, you can start with a simple timer that runs once every 100 ms or so.  Simply have the transition to the player, then cancel that transition on the next run of the timer.  This won’t be super pretty, but it’s a start on understanding the concepts.

local player = display.newCircle( 100, 100, 10 ) player:setFillColor(0,1,0) function player.randomMove( self ) if( self.removeSelf == nil ) then transition.cancel(self) return end tranistion.to( self, { x = math.random(50, 200), y = math.random(50, 200), onComplete = self.randomMove } ) end local enemy = display.newCircle (0,0,10) enemy:setFillColor(1,0,0) function enemy.seek( self ) transition.cancel(self) if( self.removeSelf == nil ) then return end transition.to( self, { x = player.x, y = player.y } ) timer.performWithDelay( 100, self.seek ) end player:randomMove() enemy:seek()

Hi.  I can’t give you an exact answer, but I can point you in the right direction:

I have coded seeking enemies in at least two games made for the Corona Geek hangouts.  You can get the full code to these games here:

Also here are links to some of the ‘enemy code including seeking logic’:

There are a few heavy duty topics involved, so if this is way too much, you can start with a simple timer that runs once every 100 ms or so.  Simply have the transition to the player, then cancel that transition on the next run of the timer.  This won’t be super pretty, but it’s a start on understanding the concepts.

local player = display.newCircle( 100, 100, 10 ) player:setFillColor(0,1,0) function player.randomMove( self ) if( self.removeSelf == nil ) then transition.cancel(self) return end tranistion.to( self, { x = math.random(50, 200), y = math.random(50, 200), onComplete = self.randomMove } ) end local enemy = display.newCircle (0,0,10) enemy:setFillColor(1,0,0) function enemy.seek( self ) transition.cancel(self) if( self.removeSelf == nil ) then return end transition.to( self, { x = player.x, y = player.y } ) timer.performWithDelay( 100, self.seek ) end player:randomMove() enemy:seek()