11 Replies Latest reply on Mar 23, 2010 7:24 AM by NOCK2k8

    If...and statements for Countdown Timers

    NOCK2k8 Level 1

      Hey guys,

       

      I currently have this code:

       

      on exitFrame me
       
        if sprite("man").intersects(sprite("finish")) then
          go to "EndScreenLevel1Win"
         
        else
          go to frame "Level1"
        end if

       

      end

       

      I want to change it so that it's more like:

       

      on exitFrame me
       
        if sprite("man").intersects(sprite("finish")) AND CountdownTimer =>0 then
          go to "EndScreenLevel1Win"

        if sprite("man").intersects(sprite("finish")) AND CountdownTimer == 0 then
           go to "EndScreenLevel1Lose"


        else
          go to frame "Level1"


        end if

      end

       

      How would I go about doing this?

       

      I know what I have written isn't quite right, somebody will probably know straight away what I need to do.

       

      Any help greatly appreciated.

       

      Thanks in advance,

      James

        • 1. Re: If...and statements for Countdown Timers
          Chunick Level 3

          I would write it like this:

           

            if (sprite("man").intersects(sprite("finish"))) AND (CountdownTimer > 0) then
              put "EndScreenLevel1Win"
            else if (sprite("man").intersects(sprite("finish"))) AND (CountdownTimer = 0) then
              put "EndScreenLevel1Lose"
            else
              put "Level1"
            end if

          • 2. Re: If...and statements for Countdown Timers
            NOCK2k8 Level 1

            Thanks for your speedy reply.  Unfortunately I know get an error stating:

             

            Script error: variable used before assigned a value

             

              if (sprite("man").intersects(sprite("finish"))) AND (CountdownTimer >? 0) then

             

             

            How should I define it?

             

            Thanks in advance!

            • 3. Re: If...and statements for Countdown Timers
              NOCK2k8 Level 1

              By changing the code slightly to this:

               

              on exitFrame me
               
                if sprite("man").intersects(sprite("finish")) AND ("CountdownTimer" > "0:00") then
                  go to "EndScreenLevel1Win"
                else if (sprite("man").intersects(sprite("finish"))) AND ("CountdownTimer" < "0:00") then
                   go to "EndScreenLevel1Lose"
                else
                  go to frame "Level1"
                end if

               

              end

               

              I get the result of it moving to the frame labelled 'EndScreenLevel1Win' but, it never directs to 'EndScreenLevel1Lose'.  Any ideas why this might be?  So close, yet...so far!

              • 4. Re: If...and statements for Countdown Timers
                Mike Blaustein Level 4

                Do you have a countdown timer?  If so, what is it called?  Is it a variable?  or a text member?

                or a sprite?  etc.  We really can't tell you how to do what you are asking without knowing how your stuff is set up.

                • 5. Re: If...and statements for Countdown Timers
                  NOCK2k8 Level 1

                  Right, I'm still trying to get my head around all of this terminology, but I think that:

                   

                  It is text with code added to it, when I click it on stage it says in the property inspector:

                   

                  Sprite 5: Text

                  CountdownTimerText

                  Internal

                   

                   

                  I have then put CountdownTimer into the 'Name' field.

                   

                  Should my code be saying WHEN CountdownTimerText > 0:00 instead of CountdownTimer do you think?

                   

                  Sorry to be a pain, I hope that I explained that okay?

                   

                  Thanks for your help.

                  • 6. Re: If...and statements for Countdown Timers
                    Chunick Level 3

                    I don't know why you are now getting an error... I simply took your code and rewrote it so that the logic would work. Nothing more.

                     

                    btw, 0:00 is not a valid value for a variable. If it was a string, eg. "0:00" then it would be... however, you then couldn't reliably compare a string as you would a numerical value using the greater than or less than operators.

                     

                    As well, CountdownTimerText in the Property Inspector would be the name of the member, not a variable and not the name of the sprite.You need to pick up a book on Director or work through some online tutorials such as the ones here: http://www.deansdirectortutorials.com

                    • 7. Re: If...and statements for Countdown Timers
                      NOCK2k8 Level 1

                      Right...

                       

                      I've looked through the tutorials, and still don't get it.

                       

                      I understand why you cannnot compare 0 to a string, and I understand that my timer is a sprite not a variable in the if...then code that I'm trying to use to make the decision.

                       

                       

                      But I still have no idea how to make that code work. Unless, I have to build a countdown timer in that code too, so I effectively have two timers: 1 that the user sees and the other that lingo uses to make the comparison?

                       

                      Any help would be greatly appreciated.  Thanks in advance.

                      • 8. Re: If...and statements for Countdown Timers
                        Chunick Level 3

                        Ok, so you've really never written code before. Maybe you could explain what you're trying to do and I can write some code examples with comments to illustrate.

                        • 9. Re: If...and statements for Countdown Timers
                          NOCK2k8 Level 1

                          I've written code, just not Lingo code.

                           

                          I have my game screen which involves a movie sprite man getting to the finish.

                           

                          I also have a sprite that counts down from 5:00 to 0:00 for the user to see.

                           

                          I want to set it up so that if the user successfully navigates from the start to finish that they see a 'Congratulations you win screen', and if the timer runs down to 0:00 before they get there a 'Unlucky, you failed' screen.

                           

                          I thought that there would be a million tutorials on the 'net for this but can't find any.

                           

                          I'm thinking that in the code for my Level1 screen I need to set up some global variables such as:

                           

                          global CountdownTimer, TimerStart, TimerEnd

                           

                          But that is about all I have come up with :S

                           

                          Any help would be great.  Thanks in advance.

                          • 10. Re: If...and statements for Countdown Timers
                            Sean_Wilson Adobe Community Professional
                            I also have a sprite that counts down from 5:00 to 0:00 for the user to see.

                            So you have code on this sprite that drives the timer. Did you write it, or did it come from the Library Palette or elsewhere?

                            • 11. Re: If...and statements for Countdown Timers
                              NOCK2k8 Level 1

                              I've answered my own question.

                               

                              All I need to do was modify the code that I had added to the timer sprite.  I've pasted it below:

                               

                              -- behaviour script attched to a text sprite
                              on beginSprite me
                                sprite(me.spriteNum).member.text = "5:00"
                                startTimer()
                              end beginSprite

                               

                              on enterFrame me
                                tSecElapsed = (the timer)/60
                                if tSecElapsed < 300 then
                                  tTime = 300 - tSecElapsed
                                  tSec = tTime mod 60
                                  if tSec < 10 then
                                    tSec = "0" & tSec
                                  end if   
                                  tMin = tTime/60
                                  sprite(me.spriteNum).member.text = tMin & ":" & tSec
                                else
                                  sprite(me.spriteNum).member.text = "0:00"
                                   go to "EndScreenLevel1Lose"
                                end if
                              end enterFrame

                               

                               

                              3 lines up from the bottom where is says   go to "EndScreenLevel1Lose" is all I need to add in so that when the timer hits 0:00 it moves to the "EndScreenLevel1Lose" screen.

                               

                              Chuffed to say the least.

                               

                              Big thanks to all those that tried to help.