LiTWol @ Oleg Terenchuk

  • Contact
  • Tips & Tricks
  • About me

User login

  • Request new password
To prevent automated spam submissions leave this field empty.

litwol's tweets

  • Doing something differently leads to something exceptional — 2 years 18 weeks ago
  •  
  • 1 of 9
  • ››
more
Home

Creating 3d cube movieClip using Sprites and animating rotation through ActionsScript 3.0

Submitted by litwol on Mon, 01/18/2010 - 17:53

The point of this exercise is to learn how to create a 3d object from primitive 2d shapes in Flash ActionScript 3.0.

I added very simple animation effect just to demonstrate that it actually is 3d and rotates.

Note: I left final 6th side out on purpose.

No special instructions to run this code, just drop it into actions editor and preview your movie.

Note that flash has "registration" point. that is the center point of your object when you control it's x and y position on the stage. When creating a complex object such as movieClip that consist of multiple primitive shapes the registration point is always relative to (0,0) coordinate of the container object. if you wish to create different registration point then you must change primitive shapes' coordinate /within/ the container object (ex: sprite1.x = -50, sprite1.y = -50).

import flash.display.*

var mc_mc:MovieClip = new MovieClip();

var sprite1:Sprite = new Sprite();
sprite1.graphics.beginFill(0xFFCC00);
sprite1.graphics.drawRect(0, 0, 100, 100);
sprite1.rotationX = 90;

var sprite2:Sprite = new Sprite();
sprite2.graphics.beginFill(0xCCCCCC);
sprite2.graphics.drawRect(0, 0, 100, 100);
sprite2.x = 100;
sprite2.z = 100;
sprite2.rotationY = 90;

var sprite3:Sprite = new Sprite();
sprite3.graphics.beginFill(0xFF0000);
sprite3.graphics.drawRect(0, 0, 100, 100);
sprite3.z = 100;

var sprite4:Sprite = new Sprite();
sprite4.graphics.beginFill(0x0000FF);
sprite4.graphics.drawRect(0, 0, 100, 100);
sprite4.z = 100;
sprite4.y = 100;
sprite4.rotationX = -90;

var sprite5:Sprite = new Sprite();
sprite5.graphics.beginFill(0x00FF00);
sprite5.graphics.drawRect(0, 0, 100, 100);
sprite5.rotationY = -90;

mc_mc.addChild(sprite1);
mc_mc.addChild(sprite2);
mc_mc.addChild(sprite3);
mc_mc.addChild(sprite4);
mc_mc.addChild(sprite5);

mc_mc.x = 150;
mc_mc.y = 150;
addChild(mc_mc);
setInterval(rotate, 100, mc_mc);

function rotate(mc_mc) {
mc_mc.rotationX += 10;
mc_mc.rotationY += 10;
mc_mc.rotationZ += 10;
}

Tags:
  • ActionScript 3
  • CS4
  • Flash

1 response to "Creating 3d cube movieClip using Sprites and animating rotation through ActionsScript 3.0"

1. Re : 3D cube used in Dice animation

Submitted by Nemesis Fixx (not verified) on Wed, 12/22/2010 - 10:01.

Thanks a lot for this code. I was actually looking for a hint on how to start working on a dice rolling animation and I think this is definitely going to be much help.

Thanks for sharing

  • reply

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <h2> <br>
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Lines and paragraphs break automatically.

More information about formatting options

To prevent automated spam submissions leave this field empty.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.

LiTWoL © Oleg Terenchuk - Hosted on Linode.com 512