The following is a step-by-step tutorial to help you get started with using the Flash WebGL Runtime API to add interactivity to your animation. In this tutorial, you start with a simple FLA file and use the API to add MovieClip instances on stage.
-
Download the file night_webgl.fla and open it in Adobe Flash Professional CC 2014. You can see that the file contains a bitmap image of a night sky on stage.
-
var count = 0; function onAdd() { var sgf = player.getScenegraphFactory(); var star = sgf.createMovieClipInstance("star"); player.getStage().addChildAt(star, 0); var x = Math.random() * 540; var y = Math.random() * 180; var s = Math.random(); var mat = new flwebgl.geom.Matrix([s, 0, 0, s, x, y]); star.setLocalTransform(mat); count++; document.getElementById("count").innerHTML = count + (count > 1 ? " stars" : " star"); }Several things happen here:
- The variable ‘player’ references an instance of the WebGL player and is the façade of the API.
- A reference to the SceneGraphFactory is obtained. It is used to create instances of the MovieClip.
- MovieClip instances are created using the function createMovieClipInstance() and passing the linkage name specified in Flash.
- A reference to the stage (root timeline) is obtained from the player.
- The new MovieClip instance is added as a child of the stage using the addChildAt() function. Passing 0 to the this function adds the child on top of all other children of the stage (above the bitmap).
- The position and size of the star are set using the Matrix class.
Note:
You can download the finished example here.
Download
Note:
- Since you render the WebGL animation on a canvas within the HTML, you are free to modify the surrounding HTML. In this example, we maintain a count of the number of stars created.
- The above example is meant for you to get started on creating interactive animations and games. Visit the WebGL runtime API documentation page to learn more about the other ways to control the elements on your scene.
- When you publish any content, Flash converts the artwork to a GPU-friendly format. Being computationally intensive, the task may take a few minutes to complete. As a best practice, minimize the number of lines and curves in your artwork. Optimizing, smoothening, and straightening the artwork using the options under the Modify > Shape menu helps you simplify your artwork. This helps increase the rendering performance and reduce the publishing time.
