13 Replies Latest reply on Aug 19, 2008 2:38 AM by Nanomedia

    Execute a script every two seconds

    wgb14 Level 1
      Hi all

      I am trying to find a way to exectute the script below, without the use of a timeout object. Any ideas?

      on getDataForNoface1 me

      if repeatinfo = TRUE AND (noface="user_face") AND (ycoord = "far away") then
      --use this far away option once at the beginning of the presenation and 1-2 times (depending of the length of the presenation)
      --during the presenation
      sprite(5).Hypertext = "\q [I am about to start my presentation, would you like to come closer?]"
      repeatinfo = FALSE
      else
      if repeatinfo = FALSE AND (noface="user_face") AND (ycoord = "normal" OR ycoord = "close") then
      --interrupt the above script when the character sees the user
      sprite(5).Hypertext = "\stop[]"
      repeatinfo = TRUE
      else
      if repeatinfo = TRUE AND (noface = "no_user_face") AND (ycoord = "normal" OR ycoord = "close" OR ycoord = "far away") AND (xcoord="right" or xcoord="left") then
      -- If the character can not see the user at all, same scenario as far away!
      sprite(5).Hypertext = "\q [Master I can't see you anymore can you come closer?]"
      repeatinfo=FALSE

      -- write the audio length code
      end if
      end if
      end if
      end
        • 1. Re: Execute a script every two seconds
          Level 7
          > I am trying to find a way to exectute the script below, without the use of a
          > timeout object. Any ideas?

          Why would you try to do this without using a timeout object? The
          situation you describe is *exactly* why they were introduced.
          • 2. Re: Execute a script every two seconds
            wgb14 Level 1
            The problem is that I already have a timeout object that updates the xcoord and ycoord every 0.8 seconds. If I will try to call the above handler after 0.8 seconds it will simply not work. Thats why I don't want a timeout object. Any ideas?
            • 3. Re: Execute a script every two seconds
              enrique_rodriguez
              If you are forced to do something without Timeout objs, maybe you should use the _movie.frameTempo (the actual framerate of the movie).
              For example if _movie.frameTempo = 30, you know that every frame duration is 1000/30 = 33.33 milliseconds, so if you need to execute the script ever 100 millisecs you must execute the script every 3 frames (3*33.33 =99.99).

              Disavantages:

              -If you run your code in a slow machine you could have frame lag that would affect the time of execution.
              -you will need a enterframe (or exitframe) to count the frames.
              -it's ugly

              • 4. Re: Execute a script every two seconds
                Level 7
                > The problem is that I already have a timeout object that updates the xcoord and
                > ycoord every 0.8 seconds. If I will try to call the above handler after 0.8
                > seconds it will simply not work. Thats why I don't want a timeout object. Any
                > ideas?

                You seem to be implying that you can't have 2 separate and distinct
                timeout objects firing at different intervals. If so, that is incorrect
                - you could and should use a different timeout to service the
                #getDataForNoface1 handler
                • 5. Re: Execute a script every two seconds
                  wgb14 Level 1
                  Here is the original code:

                  -- attach this behavior script to the text member that will display
                  -- the data value from the ActiveX control

                  property spriteNum, sp, mem
                  property dataSprite
                  global xcoord,ycoord,noface

                  on beginsprite me
                  -- create references to the sprite and the
                  -- member that the script is attached to
                  sp = sprite(spriteNum)
                  mem = sp.member
                  -- create a reference to the ActiveX data sprite
                  dataSprite = sprite(1)
                  -- create the timeout object (MX2004 syntax)
                  -- 800 referrs to 0.8 seconds which is roughly
                  -- how often the timeout handler is called (and how often the x,y coordinates will be updated)
                  dataTMObj = timeOut().new("activeXData", 800, #getDataFromActiveX, me)
                  end

                  on getDataFromActiveX me
                  xcoord = dataSprite.XCoord_desc
                  ycoord = dataSprite.YCoord_desc
                  noface=datasprite.no_face
                  --use .setContentsAfter() method of the text member
                  -- to display the data from the ActiveX control
                  mem.setContentsAfter(dataSprite.XCoord_desc & RETURN & dataSprite.YCoord_desc & RETURN & datasprite.no_face& RETURN)
                  translate3
                  end

                  on endSprite
                  if objectP(timeOut("activeXData")) then timeOut("activeXData").forget()
                  dataTMObj = 0
                  end


                  Here is the translate3 code

                  global xcoord,ycoord,noface,audiolength,repeatinfo

                  on translate3 me

                  --x coordinates
                  if xcoord = "right" then
                  sprite(5).Hypertext = "\SetSwitch [switch= lookleft state= d]"
                  --sprite(5).Hypertext = "\setstateintensity [switch=lookleft state=d f0=2 ]"
                  else
                  if xcoord = "left" then
                  sprite(5).Hypertext = "\SetSwitch [switch= lookright state= d]"
                  end if
                  end if
                  --y coordinates
                  if ycoord = "far away" then
                  sprite(5).Hypertext = "\translate [figure= camera x= 0 y= 0 z= 100 t= 2]"
                  else
                  if ycoord = "normal" then
                  sprite(5).Hypertext = "\translate [figure= camera x=0 y= 0 z= 50 t= 2]"
                  else
                  if ycoord = "close" then
                  sprite(5).Hypertext = "\translate [figure= camera x=0 y= 0 z= 20 t= 2]"
                  end if
                  end if
                  end if

                  --The globals are updated every 0.8 seconds
                  --If the user is far away from the character for 0.7 seconds at the beginning of a presenation then ask him/her to come closer
                  --Likewise if the user will not be seen by the character at all for 0.7 seconds then ask him/her to come closer

                  --Problem how can I increase the time?

                  dataTMObj = timeOut().new("getDataForNoface3",700, #getDataForNoface1,me)

                  end

                  on getDataForNoface1 me

                  if repeatinfo = TRUE AND (noface="user_face") AND (ycoord = "far away") then
                  --use this far away option once at the beginning of the presenation and 1-2 times (depending of the length of the presenation)
                  --during the presenation
                  sprite(5).Hypertext = "\q [I am about to start my presentation, would you like to come closer?]"
                  repeatinfo = FALSE
                  else
                  if repeatinfo = FALSE AND (noface="user_face") AND (ycoord = "normal" OR ycoord = "close") then
                  --interrupt the above script when the character sees the user
                  sprite(5).Hypertext = "\stop[]"
                  repeatinfo = TRUE
                  else
                  if repeatinfo = TRUE AND (noface = "no_user_face") AND (ycoord = "normal" OR ycoord = "close" OR ycoord = "far away") AND (xcoord="right" or xcoord="left") then
                  -- If the character can not see the user at all, same scenario as far away!
                  sprite(5).Hypertext = "\q [Master I can't see you anymore can you come closer?]"
                  repeatinfo=FALSE
                  else

                  -- query the length of the ogg file playing and if no userface or user too far away say



                  end if
                  end if
                  end if

                  end

                  on getDataForNoface3 me
                  if objectP(timeOut("getDataForNoface3")) then timeOut("getDataForNofac3").forget()
                  dataTMObj = 0
                  end
                  end

                  If I will try to modify the dataTMObj (say to 1000 milliseconds) the getDataForNoface1 will fail to work. Any ideas?
                  • 6. Re: Execute a script every two seconds
                    Nanomedia
                    quote:

                    Originally posted by: wgb14
                    The problem is that I already have a timeout object that updates the xcoord and ycoord every 0.8 seconds. If I will try to call the above handler after 0.8 seconds it will simply not work. Thats why I don't want a timeout object. Any ideas?


                    Just create a single timeout object (0.4 seconds) and two counters (variables):
                    delay1 = 2 and delay2 = 5
                    When delay1 = 0 update the xcoord and ycoord and set delay1 = 2 again.
                    When delay2 = 0 call the "getDataForNoface1" function and set delay2 = 5 again.
                    I'm sure you got the picture.
                    • 7. Re: Execute a script every two seconds
                      wgb14 Level 1
                      Can you provide a sample code? Its impossible to understand what you are saying otherwise
                      • 8. Re: Execute a script every two seconds
                        Nanomedia Level 1
                        quote:

                        Originally posted by: wgb14
                        Can you provide a sample code? Its impossible to understand what you are saying otherwise


                        Impossible???


                        • 9. Re: Execute a script every two seconds
                          wgb14 Level 1
                          Ok, I am still confused! Where do I set the time for myFunction1 and myFunction2 in your example? Assuming that myFunction1 is my getDataForNoface1 where should I set its time? And why delay=2 and delay=5? Are these seconds?
                          • 10. Re: Execute a script every two seconds
                            Nanomedia Level 1
                            quote:

                            Originally posted by: wgb14
                            Ok, I am still confused! Where do I set the time for myFunction1 and myFunction2 in your example? Assuming that myFunction1 is my getDataForNoface1 where should I set its time? And why delay=2 and delay=5? Are these seconds?


                            Everything is already set up.
                            Because the timeout object is set to 400ms:
                            "myFunction1" is called every 0.8 seconds (delay1 * 400ms)
                            "myFunction2" is called every 2 seconds (delay2 * 400ms)
                            Clear enough now?
                            • 11. Re: Execute a script every two seconds
                              wgb14 Level 1
                              Here it is

                              dataTMObj = timeOut().new("activeXData", 400, #getDataFromActiveX, me)

                              end

                              on getDataFromActiveX me
                              xcoord = dataSprite.XCoord_desc
                              ycoord = dataSprite.YCoord_desc
                              noface=datasprite.no_face

                              delay1 = delay1 - 1
                              if (delay1 = 0) then
                              translate3
                              delay1 = 2
                              end if
                              delay2 = delay2 - 1
                              if (delay2 = 0) then
                              scenario1
                              delay2 = 5

                              end if

                              The code does not call neither translate3 nor scenario1. Any ideas what can be wrong?
                              • 12. Re: Execute a script every two seconds
                                wgb14 Level 1
                                Got it many many thanks!!!
                                • 13. Re: Execute a script every two seconds
                                  Nanomedia Level 1
                                  Just use the algorithm i already gave it to you, insert your extra variables (global & beginSprite),
                                  insert the content of "myFunction1()" and "myFunction2()" and put everything in a frame script.