Posts Tagged ActionScript 2

Again on BlendMode (this time in AS2)

I was really happy when I found out how to do proper fade outs of MovieClips containing other MovieClips, but I am even more happy now because I found how to do it in ActionScript 2. I don’t know why but I was thinking that blendMode was something new that came in Flash 9, but I actually knew it came with Flash 8 at the same time that filters came in. I woke up when my colleague told me he and to roll back an animation from Flash 8 to Flash 7 and had to loose the blendModes because of that.

The way to do it is really similar than in ActionScript 3, it’s actually way easier but more prone to errors (if you mispell “layer” the compiler won’t tell you):

1
my_mc.blendMode = "layer";

That’s all there is to it.

, , ,

No Comments


generate random Strings in AS2 or AS3

I was searching for a function to generate random Strings either in AS2 or in AS3 but I couldn’t find any so I made my own using code from a typewriter effect, but I can’t seem to find the page anymore. I use this code when I load dynamic content from php and such and I don’t want flash to cache my request. Here is an ActionScript 2 version of the code:

1
2
3
4
5
6
7
8
9
function generateRandomString(newLength:Number):String{
  var a:String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  var alphabet:Array = a.split("");
  var randomLetter:String = "";
  for (var i:Number = 0; i < newLength; i++){
    randomLetter += alphabet[Math.floor(Math.random() * alphabet.length)];
  }
  return randomLetter;
}

For the ActionScript 3 version of it I made some optimizations and I created a class with the static method in it here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
package net.zedia.utils{
  public final class StringUtils{
    public static function generateRandomString(newLength:uint = 1, userAlphabet:String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"):String{
      var alphabet:Array = userAlphabet.split("");
      var alphabetLength:int = alphabet.length;
      var randomLetters:String = "";
      for (var i:uint = 0; i < newLength; i++){
        randomLetters += alphabet[int(Math.floor(Math.random() * alphabetLength))];
      }
      return randomLetters;
    }
  }
}

here is how you use it:

1
2
3
4
import net.zedia.utils.StringUtils;
 
trace (StringUtils.generateRandomString(4));//for a random string of 4 characters
trace (StringUtils.generateRandomString(4, "ROGER"));//for a random string of 4 characters using only the following letters: ROGER

Cet article en Français

, , ,

28 Comments


ExternalInterface Problems in ActionScript 2

Where I work we use a lot of Javascript because the guys doing it are quite gifted and also because I am the only member of the flash department (well designers do flash, but when it gets complicated I handle it), so it is often the case where Flash has to call a Javascript function. With all of my readings I had come to the conclusion that ExternalInterface.call was the best way to access Javascript functions. I guess I was wrong…

Well first of all ExternalInterface works very badly with Opera (at least the ActionScript 2 version, haven’t tested the ActionScript 3 version). Searching a bit I found people saying the it worked sometimes, it worked when it wasn’t inside a mouse related event, etc. Well this isn’t very reliable.

Also I ran in some trouble where when the Javascript was called in the html it worked fine in all browsers, when I used ExternalInterface it worked fine in Internet Explorer, didn’t do a thing in Opera and just crashed Firefox. Well that’s nice cross-browser behavior…

To solve my problem, I just had to do it the good old way; getURL(“javascript:myFunction()”). For the same Javascript function (the one that crashed FireFox), it worked in all browser. I hope this all works better in AS3.

, ,

3 Comments


Using Delegate to have the right scope when using TweenLite, Tweener and Events in ActionScript 2

Not so long ago I was having a bad day using Flash 8 and ActionScript 2 just after completing a project in ActionScript 3. Now this Dan Hunter dude made a comment saying I should try using this Delegate class. Well last week I thought I should give it a try so I head over the blog post and try to figure out what it is used for. I had heard the word Delegate before, but never quite understood it.

So I look at the post and it is exactly what I hate in a post; it is very technical and not quite to the point. I don’t think I am a bad programmer and I should understand stuff like that but there is something missing in the post: why should I use this class in the first place?

Well I am going to tell you why you should used this class; because it will give you the right scope of the function called by the delegate function. Ok, ok maybe I’m not quite clear either, but go read this article on how to use the Delegate class and you should understand everything. I found it while installing the new version of Flash Develop (beta 6). The real useful thing about the Delegate class is that it permits you to use the “this” you want inside the function you want.

Here is my example using events; let’s start by using a simple button on click custom event. So you make this custom button class that handle the rollover and rollout internally and when you have a onrelease event, you send an event to the class containing the button. You do so in order to keep your code clean and well organized. Here is the code you would have for the event listener inside the class that will contain the custom button:

1
2
3
4
5
myCustomButton.addEventListener("click", handleClick);
 
function handleClick(evt:Object):Void{
  trace (this)//this will actually refer to myCustomButton instead of the containing class
}

By using the Delegate class, “this” will refer to the containing class. Here is how you do it:

1
2
3
4
5
myCustomButton.addEventListener("click", Delegate.create(this, handleClick));
 
function handleClick(evt:Object):Void{
  trace (this)//this will refer to the containing class
}

This will save you the trouble of having to find in which scope you are and also having to use this._parent all the time.

Another good use of the Delegate class is when you use the onComplete argument with Tweener or TweenLite. When you use the onComplete argument with both these Tweening engines, it will call the function you specify it when it will be done Tweening. With Tweener this function will be scoped to the MovieClip your doing a Tween on and with TweenLite the function will be scoped to the TweenLite Object which is not very convenient. Here is how you would use the Delegate class for these situation:

1
2
3
4
5
6
7
Tweener.addTween(myCustomButton, {_alpha:100, time:1, onComplete:Delegate.create(this, doNextStep)});
 
TweenLite.to(myCustomButton, 1, {_alpha:100, onComplete:Delegate.create(this, doNextStep)});
 
function doNextStep():Void{
  trace (this)//this will refer to the class containing the Tweening code
}

Well this will save me a lot of trouble. I was thinking that I was a good programmer and that experience didn’t really mean anything but these little things, like using the Delegate class to have the scope you want or know that TweenLite is faster than Tweener which is faster than the Adobe Tween classes,  can only be acquired with experience and they make your days a whole lot better. This kinda is a lesson of humility.

, ,

2 Comments


ActionScript 2 and Object Oriented Programming; an entire day of trouble

I was back to work today after the holidays and my first day hasn’t been that fun. First of all I’m back working in ActionScript 2 after working on the cool Christmas card which was in ActionScript 3. Second, I have been trying all day to make my menu work while programming it in an object oriented kinda way but all my efforts have been in vain.

 The first big problem I encountered is that even if you assign a class to a symbol in the library, if you put that symbol on the stage, it won’t use the class you assigned to it. You have to use the attachMovie method in order to make that work. Ok that’s fine I can find workaround in order to make that work.

Now I finally get that working the trouble is that the onRollOver and other onSomething won’t work in that class because of where it is situated (in an other class or other bug I just couldn’t find). Well I lost my whole day on that…

I feel it’s kinda wasteful to spend time on problems like this (in ActionScript 2) because I am not really learning something that will be useful in the long run. Anyway I don’t usually like to make post like this, because I don’t provide an answer to my problem but I really had to get this out of my system. Sorry about this, better post next time.

, ,

3 Comments


onReleaseOutside in ActionScript 3

It’s always strange to find out that something you thought was so basic in a previous version disappears in the next version. Well, that’s what happened with the onReleaseOutside event function from ActionScript 2. That is actually not a bad thing because I used it mostly with buttons so that they wouldn’t stay in the down state or over state when you released outside. In ActionScript 3, as soon as you leave a button (or MovieClip or Sprite) it fires the MOUSE_OUT event so you don’t really have to watch for the onReleaseOutside event like you used too.

Where you still need the onReleaseOutside event is when you drag an object like the thumb of a scrollbar because you have to know when to stop the drag. The trick is to use a MOUSE_DOWN event from the object you want to drag to start you dragging process and to use the MOUSE_UP event from the stage (this is  the trick). It doesn’t really matter to use the event from the stage because the MOUSE_UP event is nearly never used and you remove the listener after it has fired to free memory. I’ll post source code about it tomorrow.

,

No Comments


ActionScript 3 and Events

I originally thought that dispatching events in ActionScript 3 would be more complicated than in ActionScript 2. But I was wrong, I wasn’t very complicated to dispatch events in ActionScript 2, well, it’s even less complicated to do so in ActionScript 3. The thing is that version 3 of ActionScript is built around the event model. Another interesting thing is that usually your class that you’d want to dispatch an event from would have to extend EventDispatcher in order to do so, but if this class already extends Sprite or MovieClip you don’t have to extend EventDispatcher; Sprite and MovieClip are already descendant of the EventDispatcher class.  What this means is that you can call the dispatchEvent method from inside a Sprite or MovieClip extending class with no other code to write.

What is a bit more complicated is when you want to create a custom event. It’s not really harder but the thing you have to know is that you have to override the clone and toString method. Once you’ve done that you’re ready to dispatch your own events. I’ll post an example about that soon.

, ,

No Comments


Composition vs Inheritance

I have been reading a lot these days about composition and inheritance. Inheritance is a pretty easy concept to grasp; composition on the other side is a bit more complicated. I have read quite a bit and I found much more advocates of composition than of inheritance. At least, with all this reading, i finally understood what interfaces were used for. I started looking on the subject because I had 2 problems, my first one in ActionScript 2 where I had 4 symbols all using the same base class. This worked good until I had to import that swf into another swf. Than it was as if Flash was using an older version of my swf. My second problem in ActionScript 3, I was working on a button class using 2 symbol on the stage. All these button were to use the same code so I wanted to extend another class; no can’t do because it cannot find definition for the symbols on stage in the superclass… Well I think I am going to use composition for the latest problem, but I haven’t found a solution for the first one…

, , ,

No Comments


SEO and Flash part 2

In the previous article I said my tutorial about search engine optimization was going to be in 2 parts, well it’s going to be in 3 parts. In this part, I’ll explain the brute force way of making what we did the last time even better and I’ll explain the motivation behind it.

Remember that in part 1 I said that one of the most important factor in search engine optimization was what is inside the title tag. Well the easy way of making you flash website be present in search engines didn’t really optimize that factor; we used it a bit but not to its full potential. Also there is something else I didn’t speak about; the description meta tag. This tag is not a factor to be well ranked in search engine, but it can definitely be a reason why the user will click on your link on the Google page or any search engine’s page. The description meta tag define the paragraph of text that will be displayed below the link in the search engine search result page. Having a good and precise description can really help get users to your website. Lastly, if somebody searched for an expression that is present in your flash website but it is really far in and not that easy to find, in our previous method, the user will have to search trough your site to find it and might leave before even finding it.

Ok, so the previous method was that we dumped all the text present in the swf file in the html page embedding it. This was fine, but not really optimal as I have explained. Going from there, what the brute force is all about is dividing all the content we dumped in the first method into different html pages. For each section in the flash website, we make a page for it in Html and we embed the same swf in all pages. There are 3 particularities you have to add to make this work.

First, in the embedding method using SWFObject, you have to pass a variable the swf file telling it what page it is on. You can do it in this way:

<script type=“text/javascript” src=“swfobject.js”></script>
<script type=“text/javascript”>
    var flashvars = {
	page : "WRITEYOURPAGEHERE"
    };
    swfobject.embedSWF(“myContent.swf”, “myContent”, “300″, “120″, “9.0.0″, "expressInstall.swf", flashvars);

</script>

So now the swf files knows what page it is on, but it has to do something with it. When the flash loads it has to check in ActionScript for the variable that was passed to it in our case _root.page and display the good content section according to it.

The last thing you have to do is in the alternative content, make a link menu that links all html pages you created. This menu has to be in every html page of your site. You have to do this so that search engines spiders can access all your pages and index them.

What we did with this method is create multiples entry pages to your flash website instead of just one. Also this enables us to give a different title and description to each page which optimize those factors. With all this said, I can now explain why I call this the brute force method; this is not yet the best method to optimize your website for search engines. It will give you the same results as the next method, but will require more time as is harder to maintain since when you make a change in content in the flash file, you also have to do the same modification in the html page. The good point is that this method doesn’t require any server side scripting like php or asp.

, , ,

1 Comment


New FlashDevelop beta version

This is just to tell you that there is a new version of the beta of FlashDevelop 3(ActionScript editor). There wasn’t any change since august 25th. I didn’t try it yet, but I soon will, it can only be better.

, ,

No Comments