Simple 2D Molehill Example

Here is a minimalistic FlashDevelop AS3 project that demonstrates how to use the Molehill API to draw a single 2D sprite on the screen. You can get the project here:
Assuming you have followed the instructions below the result should look like this:

Flash: Simple 2D Molehill Example

To compile the example you need the Incubator version of the Flash Player, a new FlexSDK (“Hero”) and an updated playerglobal.swc which includes the Molehill API.
All files can be downloaded from Adobe Labs.
After downloading and unzipping the SDK to the folder {flexSDK} create a directory named “10.1” in {flexSDK}frameworkslibsplayer and copy the new playerglobal.swc into it. Next fire up FlashDevelop and in Settings->AS3Context update the Flex SDK Location so it points to the new SDK. Finally just unzip & extract the FD project and hit F5 to compile.

Here are the basic steps to draw a textured quad:

  • Create a vertex buffer with 4 points to define a quad.
  • Create an index buffer that keys into this vertex buffer to define two triangles.
  • Create and upload a texture.
  • Compile the vertex and fragment programs (I’m using the AGAL mini-compiler from Adobe).

Then on every frame:

  • Assign the vertex and fragment program.
  • Transform the sprite and create a model-view-projection matrix for the vertex program. Since this is a 2D example we have to set up an orthographic projection.
  • Draw the triangles.

The mvp transformation could be done by the vertex shader but to keep it stupid simple I’m creating the matrix in AS3. Also note that we need to flip the y-axis and shift the origin to the upper-left corner.

41 thoughts on “Simple 2D Molehill Example”

  1. Thanks for the information, but I still can’t get it to run correctly using your demo. I’ve set up with the Flex Hero SDK as Adobe said to download. (19786 Build), hooked the most recent FlashDevelop version up to its location like you said, added the playerglobal.swc into the correct folder, but its still throwing errors. For example, I’m getting “Error #1014: Class flash.display3D::Context3D could not be found.” which should have been solved with adding the playerglobal and sdk. Any tips?

  2. Is this a compiler error or runtime error? In the latter case, make sure -swf-version=13 is present in Project Properties->Compiler Options->Additional Compiler Options and the Target is Flash Player 10.1

  3. If you don’t want to “override” the 10.1 playerglobal you can also copy your config xml from the obj folder in your project root to a different folder, change the contents of the tag to 11.0.0 and then set Project Properties->Compiler Options->Load Configuration File to your newly created config file.

    When you do that, the SDK will look for the playerglobal.swc in the “11.0” folder, instead of the “10.1” folder, so you can just copy it to that folder instead :)

    I want to try Molehill, but I can’t find the AGAL mini-compiler you mentioned anywhere, is it publicly available?

    Thanks :)

  4. Yes, the -swf-verion=13 tag is there, and the target is 10.1. I’m also going to assume that its a runtime error since the FlashPlayerDegbugger is launched (which could be the problem since I didn’t change which FP to launch the .swf in.)

  5. Thanks for the AGAL mini compiler source.

    UnknownGuardian – Project Properties->Test Movie->Play in popup should work nicely for testing (if you install the IE incubator plugin).

  6. I need help.. can you point me to the link of the hero SDK you are using?

    I tried downloading but I don’t see Stage3D or anything else fancy in the playerglobal sdk

  7. flex_sdk_4.5.0.17689
    playerglobal.swc – updated

    command line: Error: unknown configuration variable ‘swf-version’
    Use ‘mxmlc -help’ for information about using the command line.
    Build halted with errors (fcsh).

    :[ ??

  8. Same here:

    command line: Error: unknown configuration variable ‘swf-version’
    Use ‘mxmlc -help’ for information about using the command line.
    Build halted with errors (fcsh).

  9. Update: downloaded the latest playerglobal.swc, renamed it to replace the current one, compiled fine! But when running in the browser via the index.html I get: VerifyError: Error #1014: Class flash.display3D::Context3D could not be found.

    IT has the “Adobe Flash Player 11 (incubator build)” in the bottom corner.

  10. I did find some more useful resources to kinda match up what’s going on in this example. Thibault Imbert @ has another great tutorial. I think his and this one pair up nicely to kinda explain to us non-3d guys what the hell is going on

  11. Strange thing – works smoothly only when I set framrate > 60 fps, with some breaks if above.

  12. Direct rendering – if opened through the wrapper, if pure swf – software rendering

  13. question: how would you go about placing two instances of the MoleSprite on the GPU canvas? Having an example of 2 instance, might help me in learning how to paint many 2D elements to the canvas.


  14. @Michael

    I kinda figured it out after playing with you example more. Folks can check it out here –

    Keep in mind I have barely scratched the surface and this example is probably a good candidate for optimization.

    I did check out the M2D code and found it overly complicated and not very intuitive for somebody trying to do a simple 2D to GPU. Might give it another go though.

  15. Maybe off topic but I just solved my problem with ‘Class flash.display3D::Context3D could not be found.’ error in Flash Develop compiling Molehill Example. I did updatr flex dir for Flex SDK and also set additional compiler option (-swf-version=13) but I had to change also output platform target to Flash Player 11…dhaaa. I had to do it manualy in as3proj xml file since my FD version is showing only FP9 and FP10.

  16. Hello, after set and run your project,

    trace(“driver info: ” + _context3D.driverInfo);


    driver info: Software

    Do you know how to enable directX(I’m using win7)

  17. Hi Michael. Can you please update the molehill examples on the site.
    It freezes Firefox for a minute and crashes the flash player.
    Adobe changed the viewport API in molehill beta.


Comments are closed.