Back to Content

3D Movement with Actionscript 3


This example shows how to move a 3d object in a Coppercube scene using Actionscript 3. This feature only works with the Flash (.swf) target of CopperCube. For generic scripting which works on all targets, take a look at the scripting overview for JavaScript scripting.

To start, do the following:

The scene for this example.

Use the following code (saved as 'main.as' here, if you are using it in another way you will have to rename the constructor function):
package  
{
    import flash.display.*
    import flash.net.URLRequest;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    
    public class main extends Sprite 
    {
        private var fileToLoad:String = "test.swf"; 
        private var loader:Loader; 
        private var coppercubeSprite:DisplayObject;
        
        public function main()
        {  
            // load the coppercube scene from the other swf
            loader = new Loader();    
            addChild(loader);

            loader.load(new URLRequest(fileToLoad)); 
            loader.contentLoaderInfo.addEventListener(Event.INIT, onCoppercubeLoaded); 
        }  

        // called when the coppercube .swf file has been loaded and initialized
        private function onCoppercubeLoaded(e:Event):void 
        {     
            // get access to the simple API object:
            coppercubeSprite = loader.content as DisplayObject;
            
            stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
        }
        
        public function onKeyDown(event:KeyboardEvent):void
        {
            if (coppercubeSprite == null)
                return;
                
            var simpleAPI:Object = coppercubeSprite['simpleAPI'];
                
            if (simpleAPI && simpleAPI.isLoaded())
            {           
                // when the 'X' key is pressed, move the scene node with name 'cubeMesh' up a bit
            
                if (event.keyCode == 88)
                {
                    var cubeMesh:Object = simpleAPI.getSceneNodeByName("cubeMesh");
                    var position:Object = simpleAPI.getSceneNodePosition(cubeMesh);
                
                    if (position)
                    {
                        position.y += 1;
                        simpleAPI.setSceneNodePosition(cubeMesh, position.x, position.y, position.z);
                    }               
                }
            }
        }
    }
}  
This will load the just created file test.swf and move the cube in the scene a bit up everytime the key 'X' is pressed. If you don't see anything in your flash movie, check if the test.swf file could be loaded correctly. Note that you need to target Flash player 11 or, when using the 'Use Old Flash player rendering' setting at least Flash player 10 (when using Flex, use at least Flex SDK 3.3 with the compiler flag -target-player 10).
You might also need to compile your flash file with the compiler setting '-use-network=false'. An alternative is to change your local flash player security settings or to run the flash movie from a (local) webserver.

This example works by accessing the simpleAPI object exposed by the CopperCube SWF file. You can manipulate all aspects of the 3D scene with this API: textures, materials, positions, objects etc. For a full reference to all functions, see the AS3 Scripting reference.