Quick Links

Education Edu

Simulations Sims

Math Tools Math

Games Games

Generative ArtArt

Actinoscript Prog

Farmville Farm




Planetary Orbits

Click the above-left picture of the Planetary Orbits applet to start the application full screen.


Solar System Orbits


I saw FlashAndMath's solar system tutorial on the display list & children and decided to make my own with slightly better graphics and orbital accuracy rivaling Bohr's model of the atom. I decided to allow the user to reposition the satellites and soon discovered that child movieclips thought that they were the parent and couldn't be dragged independently. After some experimentation, I found a solution of putting the events on the zero-th child of each heavenly body rather than the body itself. This also required that a "graphic" symbol of the planet/sun/etc. be inside the actual symbol that was addChild-ed. So, for example, moon is an instance of library symbol mcMoon which in turn has gMoon (actually another movieClip) inside it. But, at least it works. This is also a means to add filters to an object but not it's children.


Content on this page requires a newer version of Adobe Flash Player.

Get Adobe Flash player


Next, I added the ability to zoom into and out from the bodies with the mouse wheel and discovered another problem that I have yet to solve -- the wheel zoom doesn't work unless the user first clicks on the movie. Once that is done though, wheeling works as intended, when just mousing over the object. At least, that's what happens in the flash IDE, but on the web page, even clicking doesn't work. Instead, the html page scroll vertically. But, if opened in its own window that mouse wheeling is applied to the movie rather than the page, so try the swf file directly. Any ideas to fix this are welcome.

Gary of FlashGameU also showed me how to create instances from text strings using the getDefinitionByName method, very useful when you have a list of object names in a table.


Here's what the frame 1 actonscript 3 looks like.

// ************** instantiate bodies var galaxy:mcGalaxy = new mcGalaxy(); var galaxyR:Number = galaxy.width/2; addChild(galaxy);galaxy.name = "galaxy"; galaxy.x = (stage.stageWidth - galaxyR)/2; galaxy.y = (stage.stageHeight - galaxyR)/2; var sun:mcSun = new mcSun(); var sunR:Number = sun.width/2; var sunD:Number = 2*sunR; galaxy.addChild(sun); sun.name = "sun"; sun.x = sunD; var earth:mcEarth = new mcEarth(); earth.scaleX = earth.scaleY = 0.5; var earthR:Number = earth.width/2; var earthD:Number = earthR + sunR; sun.addChild(earth); earth.name = "earth"; earth.x = earthD; var moon:mcMoon = new mcMoon(); var moonR:Number = moon.width/2; var moonD:Number = earthR + moonR + 5; earth.addChild(moon); moon.name = "moon"; moon.x = moonD; // ************** set button mode galaxy.buttonMode = true; sun.buttonMode = true; earth.buttonMode = true; moon.buttonMode = true; // ************** add even listeners galaxy.getChildAt(0).addEventListener (MouseEvent.MOUSE_OVER, galaxyOver); galaxy.getChildAt(0).addEventListener (MouseEvent.MOUSE_OUT, notOver); sun.getChildAt(0).addEventListener (MouseEvent.MOUSE_OVER, sunOver); sun.getChildAt(0).addEventListener (MouseEvent.MOUSE_OUT, notOver); earth.getChildAt(0).addEventListener (MouseEvent.MOUSE_OVER, earthOver); earth.getChildAt(0).addEventListener (MouseEvent.MOUSE_OUT, notOver); moon.getChildAt(0).addEventListener (MouseEvent.MOUSE_OVER, moonOver); moon.getChildAt(0).addEventListener (MouseEvent.MOUSE_OUT, notOver); galaxy.getChildAt(0).addEventListener (MouseEvent.MOUSE_DOWN, dragItem); galaxy.getChildAt(0).addEventListener (MouseEvent.MOUSE_UP, dropItem); sun.getChildAt(0).addEventListener (MouseEvent.MOUSE_DOWN, dragItem); sun.getChildAt(0).addEventListener (MouseEvent.MOUSE_UP, dropItem); earth.getChildAt(0).addEventListener (MouseEvent.MOUSE_DOWN, dragItem); earth.getChildAt(0).addEventListener (MouseEvent.MOUSE_UP, dropItem); moon.getChildAt(0).addEventListener (MouseEvent.MOUSE_DOWN, dragItem); moon.getChildAt(0).addEventListener (MouseEvent.MOUSE_UP, dropItem); // ************** mousewheel zoom galaxy.getChildAt(0).addEventListener(MouseEvent.MOUSE_WHEEL, wheelZoom); sun.getChildAt(0).addEventListener(MouseEvent.MOUSE_WHEEL, wheelZoom); earth.getChildAt(0).addEventListener(MouseEvent.MOUSE_WHEEL, wheelZoom); moon.getChildAt(0).addEventListener(MouseEvent.MOUSE_WHEEL, wheelZoom); function wheelZoom(e:MouseEvent):void { e.currentTarget.parent.scaleX += e.delta/10; if (e.currentTarget.parent.scaleX<0.5) e.currentTarget.parent.scaleX = 0.5; e.currentTarget.parent.scaleY = e.currentTarget.parent.scaleX; } // ************** mouseover/out routines function galaxyOver (e:MouseEvent):void { mouse_tf.text = "galaxy"; } function sunOver (e:MouseEvent):void { mouse_tf.text = "sun"; } function earthOver (e:MouseEvent):void { mouse_tf.text = "earth"; } function moonOver (e:MouseEvent):void { mouse_tf.text = "moon"; } function notOver (e:MouseEvent):void { mouse_tf.text = ""; } // ************** dropshadow filters galaxy.getChildAt(0).filters = [new DropShadowFilter()]; earth.getChildAt(0).filters = [new DropShadowFilter()]; moon.getChildAt(0).filters = [new DropShadowFilter()]; // ************** drag routines var dragging:Boolean = false; function dragItem(e:MouseEvent):void { dragging = true; e.currentTarget.parent.startDrag(); } function dropItem(e:MouseEvent):void { dragging = false; e.currentTarget.parent.stopDrag(); } // ************** perform orbits var dThGalaxy:Number = -0.2; var dThSun:Number = 0.5; var dThEarth:Number = 1; var dThMoon:Number = 1.5; stage.addEventListener(Event.ENTER_FRAME, doOrbits); function doOrbits(e:Event):void { if (!dragging) { galaxy.rotation += dThGalaxy; sun.rotation += dThSun; earth.rotation += dThEarth; moon.rotation += dThMoon; } } // ************** mouse pointer var arrow_mc:mcBigArrow = new mcBigArrow(); addChild(arrow_mc); arrow_mc.x=stage.mouseX; arrow_mc.y=stage.mouseY; arrow_mc.startDrag(); stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler); stage.addEventListener(Event.MOUSE_LEAVE, mouseLeaveHandler); Mouse.hide(); //var mouseLabel:MovieClip = new MovieClip(); var mouse_tf:TextField = new TextField(); var mouse_tff:TextFormat = new TextFormat(); mouse_tff.size=24; mouse_tff.color = 0xff0000; mouse_tf.setTextFormat(mouse_tff); mouse_tf.text = "asdf"; mouse_tf.y = 50; arrow_mc.addChild(mouse_tf); function mouseMoveHandler(evt:MouseEvent):void { arrow_mc.x = evt.stageX; arrow_mc.y = evt.stageY; arrow_mc.visible = true; arrow_mc.mouseEnabled = true; } function mouseLeaveHandler(evt:Event):void { arrow_mc.visible = false; arrow_mc.mouseEnabled = false; }