Home‎ > ‎

Moving Enemies

Now that we have a game loop. we can do some neat things. Every screen refresh, we can automatically have enemies moving across the screen that we need to avoid. Now your player can not just sit there, it has to look out for oncoming objects

Adding the enemies and setting up the code
We first add the enemies which will be buttons which we can skin with our images. Then we add the message to the game loop.
  • Add buttons (which will be skinned with the images) to your card. Generally, you can call them what ever you want.
            For this example, I am using: "e1", "e2",  ...

  • Add the message "moveEnemy" to the game loop so that it gets sent every screen refresh to the card script. In the game loop - uncomment the message to moveEnemy (take off the dashes "--" at the beginning of the line - " -- moveEnemy"
  • uncomment "detectCollisions" too because with enemies, you will have collisions
            on updateScreen 
                   if sGameRunning is true then     
                          moveBox
                          --      moveTerrain
                          moveEnemy
                          --      detectCollisions
                          --      updateScore     
                   end if  
            end updateScreen
  • Add a message handler for the message - "moveEnemy" on the card script
            on moveEnemy

            end moveEnemy

  • Add a message handler for the message - "detectCollisions" on the card script
            on detectCollisions

            end detectCollisions

Add code to move your enemy in the direction that you want

1. Moving Objects Right to Left
Here we have objects coming towards us and we have to avoid them (or collect them) They can be asteroids, bees, missiles, arrows, or what ever fits the theme of your game. They can also be butterflies, coins, diamonds or objects that you want to collect and get points or awards for. At any rate, we will do objects moving towards us, from the right.

We want our enemies to move to the left so we need to subtract a number from its location on the screen. then we have to check if it went off the left side of the card (screen). If it did, then we want to have it come in from the right again
  • Add the following code to your moveEnemy message handler. (I used 12 to move, change it for your game) After you move each enemy, you have to make sure that it did not go off the screen (Card)
            on moveEnemy
                set the left of the button "e1" to the left of the button "e1" - 12
                if
the left of the button "e1" < 0 then
                        set the left of the button "e1" to the right of the card "level1"
                end if

               set the left of the button "e2" to the left of the button "e2" - 12
               if the left of the button "e2" < 0 then
                        set the left of the button "e1" to the right of the card "level1"
                end if
            end moveEnemy

Add the following to the "detectCollisions"

            on detectCollisions
               if intersect (button "box" , button "e1" , 255) then
                        answer "Game Over"
                        stopGame
                end if
                if intersect (button "box" , button "e2" , 255) then
                        answer "Game Over"
                        stopGame
    
        end detectCollisions


2. Moving Objects From Top to Bottom
Here we have objects falling down on us we have to avoid them (or collect them) They can be asteroids, rocks, raindrops or what ever fits the theme of your game. They can also be flowers, coins, diamonds or objects that you want to collect and get points/awards for. At any rate, we will do objects falling down from the sky (top of screen)

We want our objects to move down so we need to add a number from its location on the screen. then we have to check if it went off the bottom of the card (screen). If it did, then we want to have it come in from the top again
  • Add the following code to your moveEnemy message handler. (I used 12 to move, change it for your game) After you move each enemy, you have to make sure that it did not go off the screen (Card)
            on moveEnemy
                set the top of the button "e1" to the top of the button "e1" + 12
                if
the top of the button "e1" > the bottom of the card "level1" then
                        set the bottom of the button "e1" to the top of the card "level1"
                end if

                 set the top of the button "e2" to the top of the button "e2" + 12
                if
the top of the button "e2" > the bottom of the card "level1" then
                        set the bottom of the button "e2" to the top of the card "level1"
                end if

    
        end moveEnemy

Add the following to the "detectCollisions"

            on detectCollisions
               if intersect (button "box" , button "e1" , 255) then
                        answer "Game Over"
                        stopGame
                end if
                if intersect (button "box" , button "e2" , 255) then
                        answer "Game Over"
                        stopGame
    
        end detectCollisions

3. Moving Objects Diagonally From Top to Bottom and Right to Left
Here we have objects coming at us from the sky at different angles (more realistic?)

This is a combination of the above two examples. At this point, you should be able to figure it out yourself. just combine the above (#1, #2) and merge them with no duplicate lines of code. 

Have Fun

Comments