forked from: 3D cube
// forked from ll_koba_ll's 3D cube
// write as3 code here..
package {
import flash.display.*;
import flash.text.*;
import flash.events.*;
import flash.events.MouseEvent;
import flash.geom.*;
import flash.display.Sprite;
[SWF(frameRate="24", backgroundColor="#676666")]
public class Cube extends Sprite {
private var sp:Sprite;
private var faceArray:Array;
private const CUBE_SCALE:int = 300;
public var imgData:BitmapData;
public var bitmapdata:BitmapData = new BitmapData(800,600, false, 0x000000);
public var bitmap:Bitmap = new Bitmap(bitmapdata);
public var canvas:BitmapData = Bitmap(addChild(bitmap)).bitmapData;
public var count:int;
public var test:int = 0;
public function Cube() {
inittrace(stage);
init();
}
private function init():void {
createCubeFace();
addEventListener(Event.ENTER_FRAME, function(e:Event):void {
sp.rotationY += 1;
sp.rotationX += 1;
});
}
public function draw(event:MouseEvent):void {
trace("in draw");
stage.addEventListener(MouseEvent.MOUSE_MOVE,drawGraphics);
stage.addEventListener(MouseEvent.MOUSE_UP,stopDrawing);
}
public function drawGraphics(event:MouseEvent):void {
//trace(mouseX);
//imgData.fillRect(new Rectangle(5, 5, 10, 10), 0xFFF000);
//imgData.setPixel(mouseX,mouseY,0x555555);
paint(mouseX,mouseY,10);
}
public function paint(x:int,y:int,size:int) {
for(var i:int=0;i<size;i++) {
for(var j:int=0;j<size;j++) {
imgData.setPixel(x+i,y+j,0xff0000);
}
}
}
public function stopDrawing(event:MouseEvent):void {
trace(mouseX);
}
private function createCubeFace():void {
sp = new Sprite();
addChild(sp);
sp.x = 250;
sp.y = 200;
var s:Sprite;
faceArray = new Array();
function createFace(c:uint):Sprite {
trace("creating cube face"+test);
test++;
var s:Sprite = new Sprite();
drawRect(s, c);
sp.addChild(s);
//var imgData:BitmapData = new BitmapData(50, 20, false, 0xFF00FF00);
imgData = new BitmapData(300, 300, false, 0xFF00FF00);
var bmp1:Bitmap = new Bitmap(imgData);
bmp1.x = -150;
bmp1.y = -150;
s.addChild(bmp1);
return s
}
// 背面
s = createFace(0xff0000);
s.z = CUBE_SCALE/2;
var t:TextField = new TextField();
s.addChild(t);
t.text = test.toString();
t.autoSize = TextFieldAutoSize.LEFT;
t.setTextFormat(new TextFormat("_ゴシック", 95, 0xeeeeee));
t.x = -50
t.y = -20
// 左
s = createFace(0x0000ff);
s.rotationY = 90;
s.x = -CUBE_SCALE/2;
t = new TextField();
s.addChild(t);
t.text = test.toString();
t.setTextFormat(new TextFormat("_ゴシック", 95, 0xeeeeee));
// 右
s = createFace(0x00ff00);
s.rotationY = 90;
s.x = CUBE_SCALE/2;
t = new TextField();
s.addChild(t);
t.text = test.toString();
t.setTextFormat(new TextFormat("_ゴシック", 95, 0xeeeeee));
t.x = -50
t.y = -20
// 上
s = createFace(0xffff00);
s.rotationX = 90;
s.y = -CUBE_SCALE/2;
t = new TextField();
s.addChild(t);
t.text = test.toString();
t.setTextFormat(new TextFormat("_ゴシック", 95, 0xeeeeee));
t.x = -50
t.y = -20
// 下
s = createFace(0x00ffff);
s.rotationX = 90;
s.y = CUBE_SCALE/2;
t = new TextField();
s.addChild(t);
t.text = test.toString();
t.setTextFormat(new TextFormat("_ゴシック", 95, 0xeeeeee));
t.x = -50
t.y = -20
// 前面
s = createFace(0xaaaaaa);
s.z = -CUBE_SCALE/2;
t = new TextField();
s.addChild(t);
t.text = test.toString();
t.setTextFormat(new TextFormat("_ゴシック", 95, 0xeeeeee));
t.x = -50
t.y = -20
}
private function drawRect(s:Sprite, c:uint):void {
//drawing onto canvas code
this.addEventListener(MouseEvent.MOUSE_DOWN,draw);
//var imgData:BitmapData = new BitmapData(50, 20, false, 0xFF00FF00);
//imgData = new BitmapData(50, 20, false, 0xFF00FF00);
//var bmp1:Bitmap = new Bitmap(imgData);
//s.addChild(bmp1);
//imgData.fillRect(new Rectangle(5, 5, 10, 10), 0xFFF000);
//end drawing to canvas code
var g:Graphics = s.graphics;
g.lineStyle(2, 0xffff00);
g.beginFill(c, 0);
//canvas.setPixel32(60,30,0xeeeeee);
g.drawRect(-CUBE_SCALE/2, -CUBE_SCALE/2, CUBE_SCALE, CUBE_SCALE);
g.moveTo( 14, 400);
g.beginBitmapFill(canvas, null, true, false);
g.endFill();
g.drawCircle(50, 40,10);
//s.addChild(canvas);
trace("finished drawREct"+count);
count++;
}
}
}
///// WONDERFL TRACE /////
import flash.display.Sprite;
import flash.display.Stage;
import flash.text.TextField;
import flash.text.TextFormat;
function inittrace(s:Stage):void
{
WTrace.initTrace(s);
}
//global trace function
var trace:Function;
//wtreace class
class WTrace
{
private static var FONT:String = "Fixedsys";
private static var SIZE:Number = 12;
private static var TextFields:Array = [];
private static var trace_stage:Stage;
public static function initTrace(stg:Stage):void
{
trace_stage = stg;
trace = wtrace;
}
private static function scrollup():void
{
// maximum number of lines: 100
if (TextFields.length > 100)
{
var removeme:TextField = TextFields.shift();
trace_stage.removeChild(removeme);
removeme = null;
}
for(var x:Number=0;x<TextFields.length;x++)
{
(TextFields[x] as TextField).y -= SIZE*1.2;
}
}
public static function wtrace(... args):void
{
var s:String="";
var tracefield:TextField;
for (var i:int;i < args.length;i++)
{
// imitating flash:
// putting a space between the parameters
if (i != 0) s+=" ";
s+=args[i].toString();
}
tracefield= new TextField();
tracefield.autoSize = "left";
tracefield.text = s;
tracefield.y = trace_stage.stageHeight - 20;
var tf:TextFormat = new TextFormat(FONT, SIZE, 0xeeeeee);
tracefield.setTextFormat(tf);
trace_stage.addChild(tracefield);
scrollup();
TextFields.push(tracefield);
}
}