3 Replies Latest reply on Apr 6, 2009 4:43 PM by David Stiller

    "trail period" for a swf

    PAI_T-Bone

      I am trying to play around with time based .swf files. I found this tutorial online for how to setup a "trail period" for a swf. However the code doesn't work but I can't figure out what is wrong with it. Does anyone have any ideas ?

       

      function trialDetector():Void{
      const yyyy:Number = 2009; // year
      const mmmm:Number = 6; // month-1 ( 0 (January), ... 11 (December))
      const dddd:Number = 1; // day of month (1,2,...,30,31)
      var expirationDate:Date = new Date(yyyy,mmmm,dddd);
      var currentDate:Date = new Date();
      if (currentDate > expirationDate)
          {
          // expiration has been detected
          for(;;); // use appropriate action here, for example - infinite loop to stop movie clip
          }
      }
      trialDetector();

       

       

      Thanks

        • 1. Re: "trail period" for a swf
          David Stiller Level 2

          PAI_T-Bone,

           

               That code looks fine, for the most part.  I can tell by the capital V in "Void" that this is ActionScript 2.0, so the "const" keyword is going to be a problem (const is for constants, which isn't available until ActionScript 3.0).  Change those to var, and you should be all right.  Personally, I wouldn't use an empty loop (e.g. for(;;)) to kill the movie.  Why not just prompt the user with a message?

           

               I notice that your test numbers are for July 1, 2009, which is off into the future as of this writing.  When you're testing this, you'll have to either change your computer's clock -- which is an easy way for your users to bypass this mechanism, unfortunately -- or use different values while you're testing.

           

           

          David Stiller
          Adobe Community Expert
          Dev blog, http://www.quip.net/blog/
          "Luck is the  residue of good design."

          • 2. Re: "trail period" for a swf
            PAI_T-Bone Level 1

            yeah, the site wasn't to clear on wether or not it was AS2 or AS3. The for loop is defenatly something im going to kill and replace with a prompt.

             

            However being that i am unfamiliar with some of the parts used, such as Void, Date and new Date, would you be able to point me to a godo reference on what all these are and what there uses are ? I hate to use code without know what the heck its doing.

             

            Thanks again !

            • 3. Re: "trail period" for a swf
              David Stiller Level 2

              PAI_T-Bone,

               

              > yeah, the site wasn't to clear on wether or not it was AS2 or AS3.

               

                   One of the quickest "tells" is that "v":  if it's uppercase, you're dealing with AS2; if it's lowercase (void), you're dealing with AS3.  Another quick give-away -- though it doesn't appear here -- is movie clip properties with underscores (_) in them, such as _width, _height, _x, and _y.  In AS3, none of those have underscores (so just width, height, x, and y).

               

              > The for loop is defenatly something im going to kill and replace with

              > a prompt

               

                   Cool.  Just be aware, users can't see the result of the trace() function (in case that's what you meant by "prompt").  This should be something like a text field, or some image that instructs the user that the SWF is no longer usable.

               

              > However being that i am unfamiliar with some of the parts used,

              >such as Void, Date and new Date, would you be able to point me

              > to a godo reference on what all these are and what there uses are?

               

                   Variables and functions in ActionScript can be typed, as of ActionScript 2.0.  That means they can be "labeled" as pertaining to a particular sort of value.  Strings (text), for example, are typed :String, while numbers can be typed :Number, text formatting :TextFormat, and so on.  When used in this way -- that is, when used for strongly typing variables and functions -- you precede the type of object with a colon (:).

               

                   Here's a non-typed numeric variable:

               

              var myNum = 5;

               

                   Here's the same numeric variable, typed as a Number.

               

              var myNum:Number = 5;

               

                   Here's a custom function that returns a number (the sum of two other numbers), but the function is not strongly typed, and neither are the incoming parameters:

               

              function giveSum(a, b) {

                return a + b;

              };

               

                   Here's that same function again, but this time the parameters and function itself are strongly typed as Numbers (which is what they are):

               

              function giveSum(a:Number, b:Number):Number {

                return a + b;

              };

               

                   When you see Void (or void in AS3), it means that the function in question doesn't return a value at all.

               

                   So what's the use of strongly typed values?  In ActionScript 2.0, this practice helps the Actions panel give you code suggestions (code hinting).  It also helps the compiler track down potential projects, such as, trying to perform arithmetic on two strings, as opposed to two numbers.  For example ... because Flash knows that your expressions are strings -- since you strongly typed them as such! -- the compiler knows that multiplying those values isn't such a good idea.  In ActionScript 3.0, strong typing becomes even more useful, because it lets Flash Player know much much RAM it needs to set aside to hold each variable (again, because it knows exactly what type of variable the value in question is).

               

                   You also asked about Date and the expression new Date().  Fortunately, we just discussed strong typing, and the answer to the next part is based on the same information.  Everything in ActionScript belongs to a certain type, or classification.  Things in ActionScript are called objects, and every object is defined by something called a class.  Think of the class as a blueprint for everything pertaining to the object at hand.  If you're dealing with movie clip symbols, then your owner's manual is the MovieClip class.  If you're dealing with text fields, then your owner's manual is the TextField class, and so on.  In the case of your code, you're dealing with dates (i.e., the current date and time), so the class you're after is the Date class.

               

                   To create a new date object, you simply call its constructor function (you "construct" it), by using the new keyword.

               

              var today:Date = new Date();

               

                   At this point, the variable today points to an instance of the Date class.  This instance -- this variable -- contains all the functionality available to any date object, including certain characteristics (such as the date, the month, the year).  The characteristics and object has are called properties, and they're listed in the Properties heading of the class entry in question.  Many objects can also do things (think of the movie clip's ability to play(), stop(), gotoAndPlay(), and so on).  Things an object can do are called methods, and you'll find them listed in an object's class entry as well.  Things an object can react to -- think of a button's ability to notice mouse clicks, rollovers, etc. -- are called events.  They, too, have their own heading in the class entries of objects that have events.

               

                   That should give you a start, but if you want more info, you might want to check into some books, or maybe a few online video training resources.  It all depends on your learning style, and what works best for you.

               

              > I hate to use code without know what the heck its doing.

               

                   That's music to my ears! 

               

                   If you want an overall guide to Flash CS3 or CS4, you might get something out of Foundation Flash CS3 (or CS4) for Designers (friends of ED).  These are references for designers (that is, non-programmers), that nonetheless takes significant strides into the world of ActionScript.  (Full disclosure, I'm one of the co-authors.)

               

                   If you're interested specifically in ActionScript, you might enjoy Learning ActionScript 3.0: A Beginner's Guide, by Rich Shupe and Zevan Rosser (O'Reilly).  Your best bet is probably to visit a popular vendor, like Amazon, and read the reviews.  If you can, download a sample chapter from the publisher's website, which often gives you a feeling for a particular author's (or group of authors') style.

               

               

              David Stiller
              Co-author, ActionScript 3.0 Quick Reference Guide
              http://tinyurl.com/dpsAS3QuickReferenceGuide
              "Luck is the residue of good design."