我正在尝试将处理草图转换为flash as3文件,而且我已经挂了两个处理命令 - pushMatrix()和popMatrix() - 谁能告诉我如何在flash中转换这些命令?
基本上我只需要存储我到目前为止绘制的线条矩阵并绘制一条新线,然后递归完成。这是我的代码:
var theta;
var xpos:Number = 0;
addEventListener(Event.ENTER_FRAME,draw)
function draw(e:Event) {
graphics.lineStyle(1, 0xf1eee5, 1, false, LineScaleMode.NONE, CapsStyle.SQUARE);
var a = (mouseX / stage.stageWidth) * 90;
trace("a: " + a);
var theta = degreesToRadians(a);
graphics.moveTo(stage.stageWidth/2,stage.stageHeight);
graphics.lineTo(stage.stageWidth/2,stage.stageHeight-150);
branch(150);
if (a <= 30){
xpos+=3;
} else {
}
}
function degreesToRadians(degrees:Number):Number {
return degrees * Math.PI / 180;
}
function branch(h) {
// Each branch will be 2/3rds the size of the previous one
h *= 0.66;
// All recursive functions must have an exit condition!!!!
// Here, ours is when the length of the branch is 2 pixels or less
if (h > 2) {
/* HASN'T BEEN CONVERTED TO FLASH AS3 YET
pushMatrix(); // Save the current state of transformation (i.e. where are we now)
rotate(theta); // Rotate by theta
line(0, 0, 0, -h); // Draw the branch
translate(0, -h); // Move to the end of the branch
branch(h); // Ok, now call myself to draw two new branches!!
popMatrix(); // Whenever we get back here, we "pop" in order to restore the previous matrix state
// Repeat the same thing, only branch off to the "left" this time!
pushMatrix();
rotate(-theta);
line(0, 0, 0, -h);
translate(0, -h);
println(h);
branch(h);
popMatrix();
*/
}
}
答案 0 :(得分:1)
有关绘制api的差异有两个问题。
第一种,除非您在显示层次结构上绘制多个精灵,否则闪存不会跟踪转换矩阵。不建议这样做,因为每次成功的递归都会创建至少两个显示对象并使显示层次结构混乱。
第二个,或多或少,由于第一个原因,闪存也不会自动应用转换来绘制方法,除非使用如上所述的多个显示对象。
以下代码大致可以满足您的需求。我已经玩过你的代码片段,但我不确定在我这边创建的渲染是否与你在处理时使用的算法相匹配。
private var matrices:Vector.<Matrix>;
private var matrix:Matrix;
public function Main()
{
matrices = new Vector.<Matrix>();
matrix = new Matrix();
matrix.identity();
}
private function pushMatrix():void
{
matrices.push(matrix.clone());
}
private function popMatrix():void
{
matrix = matrices.pop();
}
在你的绘制方法中,你必须保持两个点,原点和目标。每个代表基于当前矩阵的变换点。
var origin:Point;
var target:Point;
pushMatrix();
matrix.rotate(theta);
origin = matrix.transformPoint(new Point(0, 0));
target = matrix.transformPoint(new Point(0, -h));
graphics.moveTo(origin.x, origin.y);
graphics.moveTo(target.x, target.y);
...
您可以对算法执行基本优化。重复利用现有的分数会有所帮助。
我没有安装处理,但如果您可以链接代表此特定算法的视频,它可以帮助其他人了解您的目标。
祝你好运!