Tower Defense - Zone Detection & Tower Placement

Hey Guys,

I am looking to make a Tower Defense game but am having some issues trying to figure out the best way to implement a crucial step to the game “The placement of towers into zones”. e.g. I have my map divided into 5 rows and 5 columns like a grid.

What I’m trying to figure out is the best way to detect that when the player clicks on the tower button and it creates my tower with a 0/5 alpha how I can detect I’m in a grid and lock/snap the tower to that grid zone.

I was thinking of placing invisible squares as the grid and then looking at X, Y values and if between a certain X.Y setting the tower XY to the squares XY but not 100% sure on how to go about doing that I was hoping there was an easier way.

Any help would be great guys. [import]uid: 40417 topic_id: 11490 reply_id: 311490[/import]

Hey,

I’m working on a similar project, expect mine is more of a hybrid tower defense/rpg/turn based strategy game.

Your zone system doesn’t seem bad, I think Field Runners or The Creeps probably does something similar.

The best way I can think of doing it for what your saying is compare the event.x/event.y to a value/script.

for example:
local function buildtower ( event)
for i = 1, 10 do
for h = 1, 10 do
if event.phase == “ended” then
if (event.x <= (something * i )) and ( event.y <= (something * h )) then
local tower = display.newImageRect(“youimage.png”, numbers,numbers)
tower.x = (some formula)
tower.y = (some formula)
print (tower.x)
elseif (event.x <= something * i) and ( event.y <= something * i) then
print (“no”)
end
end
end
end
end
local background = display.newRect(10,10,500,500)
background:addEventListener(“touch”, buildtower)

I’m not sure how well this code will show up as I’ve never posted code on this form before.

You’ll want to modify this to your own means and add a way to check if theres a tower there before building a new one. You’ll also want to modify the script/the statement to create a grid, but I hope that should give you a start or an idea on how (at least I would) approach it.

Also I didn’t really test the code fully and it’s late so if there are any errors/typos I’m sorry. [import]uid: 23649 topic_id: 11490 reply_id: 41636[/import]

well, all you need do is in your touch event handler to do similar to following

[lua]local x = math.floor(event.x*(1/10))*10
local y = math.floor(event.y*(1/10))*10[/lua]

What you are essentially doing above is limit x and y values from touch event to multiples of 10.
Just change 10 to whatever number you want… [import]uid: 48521 topic_id: 11490 reply_id: 41642[/import]

This solved my “snap to grid” issue rather well. Nice solution, Chinmay!! [import]uid: 146178 topic_id: 11490 reply_id: 129776[/import]

Buying this template, might save you some time -

http://developer.coronalabs.com/forum/2012/08/14/tower-defense-template-sale

£19.99

Dave [import]uid: 117617 topic_id: 11490 reply_id: 129784[/import]

This solved my “snap to grid” issue rather well. Nice solution, Chinmay!! [import]uid: 146178 topic_id: 11490 reply_id: 129776[/import]

Buying this template, might save you some time -

http://developer.coronalabs.com/forum/2012/08/14/tower-defense-template-sale

£19.99

Dave [import]uid: 117617 topic_id: 11490 reply_id: 129784[/import]