Perform action when object is in range
Posted: Sun Nov 26, 2017 9:16 pm
Hello,
I am currently trying to create a tower defense game where the tower shoots a bullet at a moving enemy when the enemy comes into range. The game is going to be like any other tower defense game with multiple enemies on the screen at a time. I am really struggling and have a few questions.
1) Is there a way to give an object a health bar that can be effected by collisions. Each Enemy will need its own health bar that is effected when that specific enemy is hit. This is the code I have to spawn enemies which i can adjust the amount of enemies with the repeat statement.
2) I have been able to write some code that shoots a bullet at the enemy when i click the mouse but I cannot figure out how to make the tower do it by itself when the enemy is within a certain range. Is there a way to make the tower shoot when an object gets within a certain range? This is the code that I am using to shoot when I press a button which should be the tower.
3)lastly, I am cloning buttons to create the towers and place them on the screen in different locations but after I place the cloned button I can still interact with it like it is the original button because it has the same code. Is there a way to verify what button the mouse is clicking so that the original button and the clone buttons do different things when clicked? This is the button cloning code I have.
I am sorry for all the questions and appreciate any help that anyone can provide.
I am currently trying to create a tower defense game where the tower shoots a bullet at a moving enemy when the enemy comes into range. The game is going to be like any other tower defense game with multiple enemies on the screen at a time. I am really struggling and have a few questions.
1) Is there a way to give an object a health bar that can be effected by collisions. Each Enemy will need its own health bar that is effected when that specific enemy is hit. This is the code I have to spawn enemies which i can adjust the amount of enemies with the repeat statement.
Code: Select all
global i
on makeWave1
repeat with i = 1 to 2
clone button "EnemyX"
set the name of the last button to ("Enemy" & i)
set the loc of the last button to (105),(205)
set the visible of the last button to true
move button ("Enemy" & i) to the points of the graphic "path" in 10 seconds without waiting
wait .5 seconds with messages
end repeat
end makeWave1
Code: Select all
local y
local i
on mousedown
shootBullet
end mousedown
command shootBullet
repeat with i = 1 to 2
clone button "Ball"
add 1 to y
set the name of the last button to ("Bullet" & y)
put the loc of button "FireTowerA" into button "Ball"
set visible of button ("Bullet" & y) to true
move button "Ball" to loc of button ("Enemy" & i) in 5 ticks
if intersect (button "ball", button ("Enemy" & i)) then
delete the button ("Bullet" & y)
subtract 5 from field "EnemyHealth"
if field "EnemyHealth" < 1 then
repeat with i = 1 to 2
delete button ("Enemy" & i)
end repeat
end if
end if
end repeat
end shootBullet
Code: Select all
global y
on mouseDown
add 1 to y
grab me
clone me
set the name of me to ("FireTower" & y)
set the loc of btn "FireTower" to (59),(650)
end mouseDown