Ideas on achieving attack system?

I am thinking of ways on how I could achieve an attack and health system. What I want is to have a character walk towards the nearest enemy or the enemy base, once it is within range, attack, than wait until it can attack again.

If it dies, it changes sprite, if it attacks, it changes sprite, if it idle, it changes sprite, if it is walking it changes sprite. Kind of a similar way to this game: https://www.youtube.com/watch?v=uKEFRRrfx3s

IMPORTANT NOTE: I am not asking for someone to do everything for me, I am just open to suggestions, SMALL bits of code, tutorials, etc. I do not want someone to use their time working on code for a stranger.

Thank you in advance!

(to make it clear I am not being lazy, I will be working too, tomorrow is my last day of school, and I will be working on Corona twice as hard)

 

Well the animations should probably done with sprites and display.newSprite().  See the guide: https://docs.coronalabs.com/guide/media/spriteAnimation/index.html

Then you might want to have an enterFrame listener on each tower or each actor that checks to see if anything is in range. This can be done a couple of ways. You can have a list of targets in an array and in each actor’s enterFrame listener loop over that table and see if any of the .x’s of the objects is in range:

if math.abs( targets[1].x - my.x ) \< attackDistance then &nbsp; &nbsp; &nbsp;... end

where targets is a table of  your display.newSprite() objects, my is the object checking to see if it should attack and attackDistance is some value you choose.

If you’re using physics, there is a cool physics routine called physics.queryRegion() that basically creates a box and reports to you objects that are in the box. If your box size is where your actor can see, then you check this each frame to see if something has entered your box. You might choose to use a timer instead of enterFrame if your actor can only attack every so often. queryRegion() might be too expensive to check every frame. In the game I’m making if there is an explosion I get all the objects within the size of the explosion and apply damage. https://docs.coronalabs.com/api/library/physics/queryRegion.html

You can also create an invisible object that represents your actors sight and have a physics collision test with it and when an enemy enters into the invisible collision box you will get an event to process.  

Rob

Thanks, this is some really valuable info you gave. I would like to have this ongoing, until I get some code to show the community and this part of coding my game ends.

Do some research on “state machines” as this is what you are asking about.

Move a box with physics is easy… actually creating a decent game takes 4x research per line of code.

State machines. never heard of them, I’ll look them up!

I found an article on the Corona blog about Coroutines, and it had a section about using them for state machines. Is this recommended? It looks perfect in this scenario.

Coroutines are really advanced and you do not need that complexity yet.  You need to work your AI per unit (i.e. for each enemy type) and each unit should have it’s own enterframe event to work out what it should be doing.

You will need to define states like “hunting”, “attacking”, “wandering”, “retreating” and then code each state and animate your unit per frame.

For example, if unit 1 is “hunting” then it’s frame would move it a bit closer to the player, etc.

Well the animations should probably done with sprites and display.newSprite().  See the guide: https://docs.coronalabs.com/guide/media/spriteAnimation/index.html

Then you might want to have an enterFrame listener on each tower or each actor that checks to see if anything is in range. This can be done a couple of ways. You can have a list of targets in an array and in each actor’s enterFrame listener loop over that table and see if any of the .x’s of the objects is in range:

if math.abs( targets[1].x - my.x ) \< attackDistance then &nbsp; &nbsp; &nbsp;... end

where targets is a table of  your display.newSprite() objects, my is the object checking to see if it should attack and attackDistance is some value you choose.

If you’re using physics, there is a cool physics routine called physics.queryRegion() that basically creates a box and reports to you objects that are in the box. If your box size is where your actor can see, then you check this each frame to see if something has entered your box. You might choose to use a timer instead of enterFrame if your actor can only attack every so often. queryRegion() might be too expensive to check every frame. In the game I’m making if there is an explosion I get all the objects within the size of the explosion and apply damage. https://docs.coronalabs.com/api/library/physics/queryRegion.html

You can also create an invisible object that represents your actors sight and have a physics collision test with it and when an enemy enters into the invisible collision box you will get an event to process.  

Rob

Thanks, this is some really valuable info you gave. I would like to have this ongoing, until I get some code to show the community and this part of coding my game ends.

Do some research on “state machines” as this is what you are asking about.

Move a box with physics is easy… actually creating a decent game takes 4x research per line of code.

State machines. never heard of them, I’ll look them up!

I found an article on the Corona blog about Coroutines, and it had a section about using them for state machines. Is this recommended? It looks perfect in this scenario.

Coroutines are really advanced and you do not need that complexity yet.  You need to work your AI per unit (i.e. for each enemy type) and each unit should have it’s own enterframe event to work out what it should be doing.

You will need to define states like “hunting”, “attacking”, “wandering”, “retreating” and then code each state and animate your unit per frame.

For example, if unit 1 is “hunting” then it’s frame would move it a bit closer to the player, etc.