Archive for the ‘Flash’ Category

Making a simple button using Tweener and Sprites in AS3

Saturday, November 24th, 2007

In a previous tutorial, I showed how to make a simple button using the Tween classes and Sprites. I am going to redo the same tutorial but using Tweener instead. I am not going to re-explain the first step which are all the same than in the previous tutorial; I am just going to speak about the mouse handler functions.

So here is the new code using Tweener:

Actionscript:
  1. package{
  2. import flash.display.Sprite;
  3. import flash.text.TextField;
  4. import flash.events.MouseEvent;
  5. import caurina.transitions.*;
  6. public class simpleButton extends Sprite{
  7. public function simpleButton(newLabel:String){
  8. myLabel.text = newLabel;
  9. myLabelOver.text = newLabel;
  10. myLabelOver.alpha = 0;
  11. buttonMode = true;
  12. mouseChildren = false;
  13. addEventListener(MouseEvent.MOUSE_OVER, handleMouseOver);
  14. addEventListener(MouseEvent.MOUSE_OUT, handleMouseOut);
  15. addEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
  16. }
  17. private function handleMouseOver(event:MouseEvent):void{
  18. Tweener.addTween(this.Over,{alpha:1, time:0.5, transition:"linear"});
  19. Tweener.addTween(this.myLabelOver,{alpha:1, time:0.5, transition:"linear"});
  20. Tweener.addTween(this.myLabel,{alpha:0, time:0.5, transition:"linear"});
  21. }
  22. private function handleMouseOut(event:MouseEvent):void{
  23. Tweener.addTween(this.Over,{alpha:0, time:0.5, transition:"linear"});
  24. Tweener.addTween(this.myLabelOver,{alpha:0, time:0.5, transition:"linear"});
  25. Tweener.addTween(this.myLabel,{alpha:1, time:0.5, transition:"linear"});
  26. }
  27. private function handleMouseUp(event:MouseEvent):void{
  28. }
  29. }
  30. }

The major advantage of Tweener in the case of a rollover and a rollout (like most buttons) is that you don't have to keep track of previous Tweens in order to stop them. With the Tween classes, if you don't do that, you're going to end up with strange rollover behaviors.  Also, we had to check if the Tween  existed before trying to stop them which added extra code. Tweener handles all that for us which is pretty practical. I spoke some times of the Fuse Kit; it seems packed with a lot of options, but you can't do simple rollovers with it; with Tweener you can.

Well I'm about done with my tutorial on Tweener but I think I'll do one about using Tweener to Tween between two colors, just like Color.interpolateColor static method.

ActionScript 3 Tweener Basic Tutorial

Friday, November 23rd, 2007

On my previous tutorial about how to do Tweens  using ActionScript 3 I used the Tween classes from Adobe. At the time I didn't know about Tweener and the Fuse Kit didn't have a version in ActionScript 3. I have used Tweener a bit now and I can say that I am starting to like it a lot.

For this tutorial I am going to do exactly what I did in the previous tutorial but this time I'll use Tweener. The first thing you have to do, as with the Tween classes, is import Tweener, you do it in this way.

Actionscript:
  1. import caurina.transitions.*

Tweener as some easing classes in it but you can also use the easing classes from Flash. One good side of Tweener is that it compile perfectly fine in Flex. Tween and easing classes don't compile with Flex... A down side of Tweener is that it adds an additional 8k to your file (as opposed to 2k for Tween classes for a total of 10k).  Ok so now we are ready to go.

Actionscript:
  1. Tweener.addTween(rectangle, {x:300, time:3, transition:"linear"});

The "Tweener.addTween" part is how you add a Tween, that's kinda obvious. "rectangle" is the name of the object you want to do a Tween on one of its property (or more). "x:300" is the final x position that rectangle will be at. "time:3" is the time it will take to get there and "transition:'linear'" is the easing to use. You see this is already easier to use and to read than the Tween classes. One major change is that you input the starting position of the property. I think this is a good thing since like 90% of the time I have been doing Tween, I want the Tween to start from the current position of the property and you can just set the property before doing the Tween anyway.

The next example was about doing multiple Tweens on the same object but on different properties here is how we did it with the Tween classes:

Actionscript:
  1. <pre>var myTweenX:Tween = new Tween(rectangle, "x", Strong.easeOut, 0, 300, 3, true);
  2. var myTweenAlpha:Tween = new Tween(rectangle, "alpha", Strong.easeOut, 0, 1, 3, true);
  3. var myTweenAlpha:Tween = new Tween(rectangle, "width", Strong.easeOut, rectangle.width, rectangle.width + 300, 3, true);
  4. var myTweenAlpha:Tween = new Tween(rectangle, "rotation", Strong.easeOut, 0, 90, 3, true);</pre>

Here is how you would do that with Tweener:

Actionscript:
  1. Tweener.addTween(rectangle, {x:300,alpha:1, width:rectangle.width + 300, rotation:90, time:3, transition:"linear"});

So in one line I did what it used to take me 4 and it easy pretty easy to understand.

Another thing that is really nice with Tweener is that it as a delay property. If you want your Tween to start in 1 second you do it in this way:

Actionscript:
  1. Tweener.addTween(rectangle, {alpha:1, time:3, delay:1, transition:"linear"});

This is practical when you want to do Tweens in sequence (one after the other). With the Tween classes you would do it in this way:

Actionscript:
  1. <pre>var myTweenX:Tween = new Tween(bloc, "alpha", Strong.easeOut, 0, 1, 3, true);
  2. myTweenX.addEventListener(TweenEvent.MOTION_FINISH, doNextTween);</pre>
  3. <pre>
  4. function doNextTween(e:TweenEvent):void{
  5. var myTweenAlpha:Tween = new Tween(bloc, "x", None.easeOut, 0, 300, 3, true);
  6. myTweenX.removeEventListener(TweenEvent.MOTION_FINISH, doNextTween);
  7. }</pre>

With Tweener:

Actionscript:
  1. Tweener.addTween(bloc, {alpha:1, time:3, transition:"linear"});
  2.  
  3. Tweener.addTween(bloc, {x:300, time:3, delay:3, transition:"linear"});

As you can see you use way less code using Tweener and it tends to keep all your code for an animation together which will be easier to understand when you come back. Also I have seen some benchmark and Tweener seems to be performing better than the Tween classes. So all in all I guess I am just going to ditch the Tween classes and start using Tweener from now on.

Papervision3D experiments with models

Friday, November 23rd, 2007

I did experiments with papervision3D all day long and let me tell you this: starting by trying to import a collada model might not be the right idea at first. I finally succeeded, but not with the model I wanted ...  Actually I never got my model to work but at least I know I am doing every thing right in the Flash IDE since it works for other models. I think my problem either comes from the fact that my model had too much polygons in it or we had trouble when exporting the collada file from Maya. I didn't even code anything, I just used the papervision3D component and panel inside Flash. Its great, but I didn't get it to work properly. It is supposed to give you a preview as you are designing but that part didn't work. What did work was the change I was making inside the panel did show up when I compiled the movie.

It's kinda hard to learn how to use papervision3D, at least the part about importing a model. I know gotoandlearn.com has some nice tutorials not about using model but what helped my the most is this post from John Grden. The video at the end shows you how to use the component and the panel.

Composition vs Inheritance

Wednesday, November 21st, 2007

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...

How to pass variables from HTML to the document Class in AS3

Tuesday, November 20th, 2007

I thought I'd make a post about this  just to make it easier for people to find answer for this question: How to pass variables from HTML to the document Class in AS3? I knew this would be more complicated then in ActionScript 2 because it was just too darn simple.  My first guess was that you would retrieve the variables from the constructor of the document class, but I was wrong. You have to use the LoaderInfo class for the root object. Here is a link showing you how to do it.

What’s new in SWFAddress 2.0

Thursday, November 15th, 2007

SWFAddress 2.0 is being released tomorrow and Asual wrote a 3 parts article (1, 2, 3)  about what's new with version 2.0 and why should people use it. It's a very nice article where they also speak about a new SEO method. I was suppose to make another tutorial about SEO, but I'll wait until I have checked their example before making it. Also they revamped their website and I find it's much better than their older version.

Using fl.* packages in Flex and more

Tuesday, November 13th, 2007

Slow days at work means I get to experiment and search the web for new ways to develop. I did a lot of research today on Flex until I stumbled on this presentation on the beedigital blog. It's a very nice presentation on how to integrate Flash and Flex in your workflow. That's exactly what I wanted. After see it I tried to do some test projects in Flex. Most of the time I do every thing in ActionScript so I thought this wouldn't be too difficult. The trouble is that most of the time I also use the Tween and Easing classes from Flash. These classes come in the fl.* package and this package does not compile in Flex. I searched a bit how I could make these classes compile but I only found complicated methods of doing so. I tired also to use the Tween classes from Flex, but in order to use them you have also to include the Flex framework which adds 100k to your project. My simple solution to this problem; using Tweener for my tweens. Tweener only adds 10k to a swf file compared to the fuse engine (which is not in AS3 yet). It's not so bad, it's only a 8k difference to the Flash Tween classes (2k). Also using it a bit, I might have misjudged Tweener a bit because it works very well. I might in the future redo some example using Tweener instead of the Tween classes.

I went on the blog of Samuel Asher Rivell, the author of the presentation I spoke of earlier, and found some other interesting things. First there is the Montreal Game Summit that's going to be held on November 27th and 28th. I live in Montreal so I'd be pretty happy to go there, at least on the first day. It has some interesting conferences about Flash that I'd like to see especially the one given by Samuel Asher Rivell on Advertgames.  The second nice thing I found on his blog is the powerpoint presentation of one of the conference he gave on blitting. I had seen the technique some time on the web but never knowing what it was or how to do it. His presentation is a good introduction to it and I'd be really interested in hearing his speach about it. There might be more on blitting on this blog since it looks really promising.

SEO and Flash part 2

Saturday, November 10th, 2007

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.

Really nice papervision3D website

Friday, November 9th, 2007

While browsing the papervision3D blog, I found a link to the new site for the Canon EOS 400D. I can say that it is absolutely amazing. The design is really nice and papervision3D is used in a way that it adds to the experience, not just to show off some cool 3D. It is in fact very subtle and it is that subtility that makes it awesome. Papervision is really nice, but it has some flaws, like the edges of a plane are usually not that smooth, but in this site, they used Papervision3D in a way that it didn't show. Well nice work!

Finally something about SEO and Flash

Wednesday, November 7th, 2007

I have been postponing this for a while but I think this is a good time for it now. This article will be the first part of a two part tutorial about how to optimize your website in flash for search engines. This tutorial is about the straight forward way of doing it involving no server side scripting. It is not the ultimate way to do it, but it sure is way better than doing nothing.

The first thing anyone interested in optimizing their site for search engines should do is read this article on seomoz.org on the topic. It is about how to optimize in html but a lot of thing that applies to html still applies in flash, read on to know why.

So if you read the article you will know that one of the most important factor is title of the Html page. The title in html is the text you see appearing in the blue bar at the top of your browser. That's nice because with a flash based website, the title is still the html title, so you should give a lot of attention to it. And also it is really easy thing to modify. You should really think about the words you put in your title because you only have a limited number of words. Some say 60, some say more, I have a tendency to put more and let the search engines crop the end of them. Keeping that in mind put the most important key words first. Don't just dump keywords, try to make sentences and slogans, search engines have been around for a while now, they know when you are trying to fool them.

Next thing you have to optimize is your text content. Google, and other search engines, cannot see in your Flash files (well I think now it can, but it sure cannot see dynamic text) so we have to find a way to make your text content accessible to them. It all happens in the html integration of your site. First, don't use the default html page that flash, even flash CS3, creates for you. It's just not good enough and there is a lot of bugs related to it. The best way to integrate a Flash is to use SWFObject. It's so good that Adobe is even speaking with the makers of it to put it in the next version of Flash. Go read about it and check the examples, it is very well explained. The basic of SWFObject is that it will replace a div (or a td) using javascript by the proper Html to embed a swf. All the magic for us happens in the replacing of the div. Here is a sample code of how to embed using SWFObject:

<script type="text/javascript" src="swfobject.js"></script> <script type="text/javascript">
    swfobject.embedSWF("myContent.swf", "myContent", "300", "120", "9.0.0");
    </script>
</head>
  <body>
    <div id="myContent">
      <p>Alternative content</p>
    </div>
  </body>

So you see, everything that is in the div with id myContent will be replaced by your swf file. You can also see the text Alternative content in there, this is where you are going to put all the text that is in your swf file. Think of it as a tune down version of an html page. So don't just cut and paste your text in there; use p tags around paragraphs and most importantly h1 tags around your titles. In the article from seomoz, you can read that text inside h1 tags is also really good for search engines. So use it to put your titles and important text. Another good point of using this technique is that screen readers for blind peoples just as search engines will be able to read your alternative content. Well that is it for part 1 of this article, part 2 will be about creating more than one entry point to your site using either brute force or server side scripting.


Close
E-mail It