Perform action when object is in range

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
NDR
Posts: 2
Joined: Sun Nov 26, 2017 8:59 pm

Perform action when object is in range

Post by NDR » 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.

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
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.

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
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.

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
I am sorry for all the questions and appreciate any help that anyone can provide.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9669
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Perform action when object is in range

Post by dunbarx » Sun Nov 26, 2017 10:52 pm

So much fun.

Oh yes, welcome to the forum.

It will take a little time to work through all you ask, so try this to address one aspect of your question. Make three buttons. Name the first one "b1" and place it near the lower left of the card. Name the second one "b2" and place it near the upper right of the card. Now make a polyLine graphic with lots of angles, and run it from btn "b1" to btn "b2".

In the script of the other button:

Code: Select all

on mouseUp
   set the loc of btn "b1" to line 1 of the points of grc 1
   put the points of grc "p1" into tPoints
   
   repeat with y = 1 to the number of lines of tPoints
      move btn "b1" to line y of tPoints
      wait 15
      if distance(the loc of btn "b1",the loc of btn "b2") < 100 then
         fireAway
         exit to top
      end if
   end repeat
   
end mouseUp

function distance a,b
   put item 1 of a - item 1 of b into x
   put item 2 of a - item 2 of b into y
   return abs(trunc(sqrt(x^2 + y^2)))
end distance

on fireAway
   answer "shoot"
end fireAway
Click that third button. Now this is crude, but do you see what is going on? Of course, nothing exciting happens when btn "b1" gets close to btn "b2", but maybe you can take that on?

Craig Newman

NDR
Posts: 2
Joined: Sun Nov 26, 2017 8:59 pm

Re: Perform action when object is in range

Post by NDR » Tue Nov 28, 2017 3:28 pm

Hello Craig,

With your help i was able to make some progress and get the towers to shoot on their own. I also was able to figure out how to make the game notice the difference between the cloned buttons. The problem that I am faced with now is that with the new code I can only get one enemy on the screen at a time. If i modify the code back to what it originally was I can get all the enemies to populate but the towers will not shoot at them.

Is there a way to spawn multiple enemies at a time and still have them stop at each point along the path that i created. This is my current code to move my enemies and spawn them.

Code: Select all


on makeWave1            --Spawns the enemies, this is in the card script
   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
      moveEnemy
      wait .5 seconds with messages
   end repeat
end makeWave1

on moveEnemy              --Moves the enemies along the path, this is in the card script
   put the points of grc "path" into tPoints
   repeat with y = 1 to the number of lines of tPoints
      move btn ("Enemy" & i) to line y of tPoints
      wait 15
      call checkLocation of button "FireTower1"
      if the loc of btn ("Enemy" & i) is (875),(485) then
         delete the button ("Enemy" & i)
         subtract 1 from field "mineHealth"
      end if
   end repeat
end moveEnemy

on checkLocation                --checks if the tower is in range at stops on the path. this is in the button script
   if distance(the loc of btn ("Enemy" & i),the loc of btn "FireTower1") < 100 then
      gfireTower
   end if
end checkLocation

Also, is there a way to modify my checkLocation handler to do nothing if the button "fireTower1" does not exist yet, as in if the player has not placed it into the game yet? I figured that I could create a true or false statement that becomes true once the tower is cloned but I wasn't able to get it to work.

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2718
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: Perform action when object is in range

Post by jmburnod » Tue Nov 28, 2017 7:56 pm

Hi NDR,
is there a way to modify my checkLocation handler to do nothing if the button "fireTower1" does not exist yet
Yes:

Code: Select all

on checkLocation
   if there is not button "fireTower1" then exit checkLocation
   ...
end checkLocation
Best regards
Jean-Marc
https://alternatic.ch

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9669
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Perform action when object is in range

Post by dunbarx » Tue Nov 28, 2017 9:03 pm

Hi.

It is quite a task to analyze your code and try to visualize your planning and thinking. I can barely do my own. That is why you are getting small snippets of help as we latch onto certain aspects of your project, and not broad advice.

So here is another one. Might it not be better to show and hide the various gadgets in your game, instead of deleting and re-creating? The coding would be simpler, and you only need check the visible instead of the existence of a control. This assumes a fixed maximum number of those controls, though.

Craig

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2718
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: Perform action when object is in range

Post by jmburnod » Wed Nov 29, 2017 10:11 am

Hi,
As Craig said
Might it not be better to show and hide the various gadgets in your game, instead of deleting and re-creating?
Yes:
you will avoid to manage layers, rename controls etc...
If you use idID of controls, you have to consider that each control you create have an unique ID and the ID of a deleted object become unavailable for the current stack.

Best regards
Jean-Marc
https://alternatic.ch

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”