Our lies make baby Jesus cry

Category: Flash drawing API

Flash AS3: dynamically draw a mask with lines

I just finished a project that allowed a user to dynamically draw lines on a chalkboard. Obviously, I used the Flash drawing API to draw the lines, but I wanted to give the lines a chalk texture. I figured it would be simple – just create a background texture that looked like chalk and use the dynamically drawn lines as a mask…

Wrong! Flash can’t use dynamically drawn lines as a mask. It can use dynamically drawn shapes (like circles and rectangles) without a problem as long as they have a fill color. Unfortunately, Flash considers all lines to be infinitely thin vectors, so they don’t work as a mask. Even if you set the lineStyle property to be really wide, Flash simply can’t use the lines to create a mask.

I figured out a relatively easy way to do this. Check out the example below:

How is this done? Essentially, you can get around Flash's issues by creating a transparent bitmap copy of your dynamically drawn lines and then using the bitmap as your mask. Do this on every frame and you have a dynamic mask using the drawing API! Of course, with Flash, there is always a catch. The catches are:

  • You must set the BitmapData properties correctly to create a transparent bitmap (download the example and check it out)
  • You must set cacheAsBitmap to true for both the new mask and the masked object or this doesn't work.

As always, I've included a source file download. I commented this one VERY thoroughly, so you should be able to implement it pretty easily. You can toggle the "enableMasking" property in the code to see the line drawing without the masking. Combine this with my earlier post on drawing smooth lines and you can make something really cool. Good Luck!

Flash AS3: A quick test of Five3D

Five3D is a compact Actionscript library that allows you to create 3D views quickly and easily. Much like Papervision3D, Away3D or Sandy, it allows you to create basic 3D graphics with Actionscript. Why would you use Five3D instead of one of these other libraries? Here’s a quick rundown of the advantages and shortcomings of Five3D  that I have found:

  • Five3D has a stripped-down feature set. If you need to do something simple like move a large number of planes in 3D space, Five3D will be easier to set up than the other 3D packages. The drawback is that it doesn’t do many of the things that the other engines do, like providing built-in primitives or Collada imports.
  • Five3D handles vector objects without first turning them into bitmaps. You can actually use Flash’s native drawing API to create vector shapes and move them in 3D while still retaining the vectors. This creates smoother renders than the other 3D engines, especially when scaling and rotating objects. The other 3D engines create bitmaps of textures and then map them onto the surfaces of objects.
  • Five3D handles 3D text really well. You can convert fonts into vectors and then use them as 3D objects. This makes 3D text effects really simple.
  • The default handling of bitmap textures is cleaner than Papervision. With Five3D, bitmap textures on planes map much more smoothly than with Papervision’s defaults. You can map textures just as well in Papervision, but it’s a little bit of a hassle to set up.
  • The Five3D library is written to extend the native flash classes in a way that is really intuitive. For example, a MovieClip3D is just like a MovieClip, but it has additional features like a z-coordinate. Half the time, you can guess what properties each object will have without even looking them up in the docs.
  • Five3D doesn’t use a camera object. Depending on your needs, this could be an advantage or a disadvantage. You can handle this by simply creating a Sprite3D container to hold all of your objects. Then, move the container to simulate camera movement. I often do this in Papervision projects anyway because of various issues I have had with the camera.

I have built a simple “Hello World” test of Five3D, which you can see below. It’s not very exciting, but it isn’t supposed to be. When I started playing with Five3D, I had a hard time finding a simple, stripped-down example that could get me started, so that is what I’ve built here. You can download the source files here. I have also added links to some more advanced tutorials and samples at the bottom of this post.

I did run into some weird Five3D quirks when I built this. First, when I created a MovieClip3D object and added it to the scene, it wasn't appearing on stage. I traced all of the properties and everything indicated that it was on the stage, but it was not displaying. I Googled around a bit and found this website with the answer: you have to call the activate() function for MovieClip3D objects to get them to render on the stage. I don't understand why you need to do this, but it fixed my problem.

The other quirk that you need to know about is that you have to turn on z-sorting. By default, it z-sorts according to your addChild() calls, just like regular MovieClips. If you don't need z-sorting, just add the 3D items in the correct sequence. I'm guessing that this helps to optimize the code. If you need your objects to spin in 3D space, you can turn on z-sorting with the childrenSorted property (check out the example source code for usage).

Thanks to Mathieu Badimon for this amazing Actionscript package.

Links:

Download the source files for the example above

A 3D text sample with source code

A simple example of Five3D with source code

A fun Five3D experiment

A tutorial on building a 3D photo album with Five3D

Info on integrating Five3D with Jiglib physics

Flash AS3: Smooth lines with the Drawing API

Flash’s drawing API is really useful for a lot of applications and games, but it has one major drawback: user-drawn lines are usually jagged and sloppy looking. The simple fact is that it’s really difficult to draw smooth lines with a mouse. In many cases, it’s not a big deal and it is an accepted drawback of drawing with a mouse.

Recently, however, I built a flash game and those jagged lines were a real problem. I was creating a linerider-style game similar to this one, in which a user draws a track and then clicks a button to watch a character sled down the lines that they have just drawn. It’s fun to draw loops and jumps and watch the rider flip and fly all over the place. The jagged lines created by the Flash drawing API were creating a lot of problems for the physics simulation. My sled never picked up any speed because of all the little bumps on the user-drawn lines.

After spending a lot of time Googling around, I found a forum post that suggested using a basic easing script to draw smooth lines. It was so simple and so clever at the same time! It solved my problem, so I created a demo to show how it works.

Check out the Flash demo below. When it loads, the ease factor is 1, meaning that there is NO EASING. Try drawing a few circles on the gray rectangle and you’ll see that it’s the usual jagged drawing tool. Now, drag the red slider down until the easing reads about 0.25 and try drawing the circles again. You should notice that the lines look much smoother. Try drawing lines at different speeds and with different easing amounts. Remember that lower numbers mean MORE EASING (I know, it seems backwards, but it prevents a deadly “divide by zero” issue that you get by doing it the other way).

There is one drawback to this method. The line lags behind your mouse a little bit as you draw. In most cases, this isn't a huge issue and the benefit of smoother lines outweighs this drawback.

As usual, you can download the complete source code packet here.

If you've never played Linerider, check out this killer version. Please note that I did not create this awesome game. I created a similar game for a client that wishes to remain anonymous.

Powered by WordPress & Theme by Anders Norén