Page 1 of 1

CPU controlled movement for enemy

Posted: Mon Dec 01, 2014 9:53 am
by SammL1
My code for creating an enemy at a random location on the top of the screen now works but I can't get the enemy to move down the screen, this is what I've got so far;

Code: Select all

on moveAlien
   if moveEnemy = true then
      send this to me in 10 millisecs
      set the top of image "alienEnemy" to (the bottom of image"alienEnemy" + 2)
      if the top of image "alienEnemy" >= 400 then
         delete image"alienEnemy"
         
         put false into moveEnemy
         //put (lives - 1) into lives
         put false into alienEnemy
      end if
   end if
end moveAlien
Many thanks. Samm.

Re: CPU controlled movement for enemy

Posted: Mon Dec 01, 2014 2:07 pm
by Klaus
Hi Samm,

almost! :D

Code: Select all

on moveAlien
   if moveEnemy = true then
      send "movealien" to me in 10 millisecs
...
But I would put this at the end of the handler and use better readable and UNnested IF THEN clauses 8)

Code: Select all

on moveAlien
     ## No need to continue in this case:
     if moveEnemy = FALSE then
        exit movealien
     end if

      set the top of image "alienEnemy" to (the bottom of image"alienEnemy" + 2)
      if the top of image "alienEnemy" >= 400 then
         delete image"alienEnemy"       
         put false into moveEnemy
         //put (lives - 1) into lives
         put false into alienEnemy
      end if
      send "movealien" to me in 10 millisecs
end moveAlien
Best

Klaus