Home‎ > ‎Moving Targets (code)‎ > ‎

Moving Targets Code

Let us start with our code from the previous lessons where you have a button "box" that is you r player and using the arrowKey message to move it on the card.

Here is the code to add moving targets (Enemies) to your game:

1. Add a "Start" button with this code:

   on mouseUp
        startGame
    end mouseUp

2. add a "Stop" button with this code:

   on mouseUp
        stopGame
    end mouseUp

3. Name your card "myCard" and add this code to it: (before the code already in the card)

global gameRunning

on startGame   
   put true into gameRunning
   moveEnemy
end startGame

on stopGame   
   put false into gameRunning   
end stopGame

on moveEnemy  
   if gameRunning is true then   
      set the left of button "enemy" to the left of button "enemy" - 5
       if the left of the button "enemy" < 0 then
          set the left of the button "enemy" to the right of the card "myCard"
       end if
       send "moveEnemy" to me in 2 ticks   
   end if   
end moveEnemy



Mow you will have an Enemy constantly going across the screen from right to left.
Comments