3D Christmas tree (aka the “Merry Christmas” post)

3D, actionscript 3, Math, Particles

Hi there,

lot of time without posting some code. I have been working three months on a Disney project, which will be on-line soon and makes me very proud, but almost drained all of my energies.

Here is my Christmas card/gift for my readers, in less than 80 lines of code. Obviously with source code. The lights are vector graphics in the attached FLA’s library to make it easy to customize or animate.

Nothing too fancy here about the code, just some trigonometric math. You can have a look at the correctOrientations function which can be interesting to avoid lights deformations due to the FP10 native 3D.
Download source code.

I wish you all great winter holidays, and an awesome beginning of 2010!
And make me smile by leaving a greetings comment ;)

Watch the running example:
Click to start (mouseX affects rotation)
xmas2009

10 Comments

10 Comments

  1. jeanphilippe  •  Dec 24, 2009 @2:15 pm

    hi,
    it’s really nice but source don’t run…

  2. Pierluigi Pesenti  •  Dec 24, 2009 @2:50 pm

    For me works well. What kind of error are you given?

  3. Synercoder  •  Dec 24, 2009 @8:50 pm

    Very nice done! RESPECT and a merry christmas

  4. kires  •  Dec 27, 2009 @2:18 pm

    Cool! this is very interesting and really good effect. Cheers

  5. Darrin Massena  •  Dec 28, 2009 @7:51 pm

    Lovely!

  6. rkalexander  •  Apr 8, 2010 @12:32 am

    Wow, man! Beautiful! Thanks for sharing!

  7. Sueki Seto  •  Apr 21, 2010 @3:00 pm

    should I run the code in Flash CS4 or Flex Builder 3.0?
    I tried it in Flash and it won’t run. Please help. Thanks.

  8. Pierluigi Pesenti  •  Apr 21, 2010 @3:09 pm

    Xmas2009.as is the main file of a Flex Builder AS3 project.
    In flash you could use it by cleaning up a little. You could remove stuff like [SWF(width="400", height="600", backgroundColor=0x000033, frameRate="60")] or the embed tags, save it as a class and instance it from a simple fla, or associate the class to the fla file itself.

  9. Sueki Seto  •  Apr 21, 2010 @4:13 pm

    I tried to use “Xmas2009.as” as document class for “lights.fla” but it still won’t work.
    What did I do wrong. Pls help. Thanks.

    package
    {
    import flash.display.Sprite;
    import flash.display.MovieClip;
    import flash.events.Event;

    import flash.filters.GlowFilter;

    public class Xmas2009 extends MovieClip
    {
    private var lights:Sprite;
    private const INIT_RADIUS:Number=150;
    private const MAX_ROTATION_X:Number=20;

    private var light1_class:light1 = new light1();
    private var light2_class:light2 = new light2();
    private var light3_class:light3 = new light3();
    private var light4_class:light4 = new light4();

    public function Xmas2009()
    {
    lights = new Sprite();
    lights.x = 200;
    lights.y = 250;
    lights.filters = [new GlowFilter(0xffcc00, 1, 12, 12, 2, 1)];

    prepare(light1_class, lights, 170, 18, 120);
    prepare(light2_class, lights, 100, 2, 20);
    prepare(light4_class, lights, 100, 2, 20, 180);
    prepare(light3_class, lights, 160, 31, 50);

    addChild(lights);
    addEventListener(Event.ENTER_FRAME, onframe);
    }

    private function onframe(event:Event):void
    {
    var sy:Number = (mouseX-200)/80;
    lights.rotationY += sy;
    correctOrientations(lights);
    }

    private function correctOrientations($target:Sprite):void
    {
    var c:Sprite;
    for(var i:uint=0; i<$target.numChildren; i++) {
    c=$target.getChildAt(i) as Sprite;
    c.rotationY=-$target.rotationY;
    c.rotationX=-$target.rotationX;
    }
    }

    public function prepare(className:Class, target:Sprite, count:uint, revolutions:uint, lightInterval:uint, initialAngle:Number=0):void
    {
    var a:Number = initialAngle;
    var arad:Number, l:Light, yspan:Number, r:Number;

    for (var i:int=0; i<count; i++) {
    r = INIT_RADIUS/count*i;
    a += (360*revolutions)/count;
    arad = a/180*Math.PI;

    l = new Light(className, lightInterval*i);
    l.x = Math.cos(arad)*r;
    yspan = 400/count;
    l.y = i*yspan-200;
    l.z = Math.sin(arad)*r;
    target.addChild(l);
    }
    }

    }
    }

  10. Pierluigi Pesenti  •  Apr 21, 2010 @4:31 pm

    private var light1_class:light1 = new light1();
    private var light2_class:light2 = new light2();
    private var light3_class:light3 = new light3();
    private var light4_class:light4 = new light4();

    you create an instance for every light asse available in the library, but the class Light.as just wants the reference to the class not an instance of that class. It’s the class which provides to the instancing. This works:

    private var light1_class:Class = light1;
    private var light2_class:Class = light2;
    private var light3_class:Class = light3;
    private var light4_class:Class = light4;

    Cheers

Leave a Reply

Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>