我创建了一系列可用于生成和渲染图像的类。我想存储显示的最后一帧的副本,以便我可以将它与当前帧混合以创建视频延音效果。本示例中涉及的类的简要概述:
MasterContainer
:用作主要显示对象的Sprite
的子类。生成类放在MasterContainer
中,并在告知容器呈现时重绘CustomWave
:Shape
的子类,用于包含,绘制和操作GraphicsPath
对象。上述“生成类”之一我当前的尝试涉及使用两个MasterContainer
个对象 - 一个用于当前帧,一个用于最后一帧。如果我没有弄错的话,可以使用MasterContainer
之类的命令将lastMaster.graphics.copyFrom(master.graphics);
(及其子项)的当前外观复制到另一个var time:Number;
var master:MasterContainer = new MasterContainer(); //current frame
var lastMaster:MasterContainer = new MasterContainer(); // last frame
var wave:CustomWave = new CustomWave(new <Number>[0,0,0,0],0xffffff,5); //generator for current frame
master.RegisterComponent(wave); //adds CustomWave and registers with the rendering loop
addChild(lastMaster); //add last frame to stage
addChild(master); //add current frame to stage
addEventListener(Event.ENTER_FRAME, perFrame);
function perFrame(event:Event):void{
time = 0.001 * getTimer();
lastMaster.graphics.copyFrom(master.graphics); //copy previous frame's graphics
UpdatePoints(); //update the path of the CustomWave
UpdateColor(); //update the color of the CustomWave
master.fireRenderCannon(); //redraw objects registered to master
}
。请考虑以下代码:
lastMaster
这似乎在理论上有效,但据我所知,master
最终没有可见的图形内容,即使lastMaster
按预期呈现。我已经尝试了几次来测试是否是这种情况,而且我非常确信它是,但我对AS3很新,我担心我会忽略一些东西 - 代码看起来应该有效。有没有人有关于如何正确测试这个的建议?此代码中是否存在明显的缺陷会导致{{1}}在视觉上空白?有没有更好的方法来实现我的目标?
我认为我对此表示满意......我很乐意接受任何意见。谢谢!
答案 0 :(得分:0)
复制图形后,您尝试用它做什么?
方法copyFrom
作为时钟工作,没有任何问题。你的代码中不存在逻辑错误吗?
function perFrame(event:Event):void{
time = 0.001 * getTimer();
lastMaster.graphics.copyFrom(master.graphics); //Here
//master.graphics.copyFrom(lastMaster.graphics);
UpdatePoints();
UpdateColor();
master.fireRenderCannon();
}
copyFrom
的示例,它适用于任何复杂的图形:
var complex: Shape = new Shape();
adobeExample(complex.graphics);
var test2: Shape = new Shape();
test2.graphics.copyFrom(complex.graphics);
addChild(test2);
private function adobeExample(graphics: Graphics):void{
// define the line style
graphics.lineStyle(2,0x000000);
// define the fill
graphics.beginFill(0x666699);//set the color
// establish a new Vector object for the commands parameter
var star_commands:Vector.<int> = new Vector.<int>();
// use the Vector array push() method to add moveTo() and lineTo() values
// 1 moveTo command followed by 3 lineTo commands
star_commands.push(1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2);
// establish a new Vector object for the data parameter
var star_coord:Vector.<Number> = new Vector.<Number>();
// use the Vector array push() method to add a set of coordinate pairs
star_coord.push(0,0, 75,50, 100,0, 125,50, 200,0, 150,75, 200,100, 150,125, 200,200, 125,150, 100,200, 75,150, 0,200, 50,125, 0,100, 50,75, 0,0);
graphics.drawPath(star_commands, star_coord);
}
答案 1 :(得分:0)
在Bennet和Nicolas的评论之后,很明显,如果没有相当多的重新设计,我的要求几乎是不可能的。所做的更改如下:
DisplayObject
。它们仅用于计算包含使用IGraphicsData
方法绘制生成的图形所需的drawGraphicsData
对象的向量。MasterContainer
现在是一个形状子类,它从每个已注册的生成器中检索Vector.<IGraphicsData>
以便绘制输出。MasterContainer
的内容,并将其与前一帧的颜色抑制版本相结合。位图子类的删节版本:
private var constantSustain:Number;
private var linearSustain:Number;
private var sustain:ColorTransform;
private var lastFrame:BitmapData;
public function BitmapManipulator(constantSustain:Number = 0.998, linearSustain:Number = 0.98) {
this.constantSustain = Math.min(Math.max(constantSustain, 0), 1);
this.linearSustain = Math.min(Math.max(linearSustain, 0), 1);
this.UpdateSustain();
this.addEventListener(Event.ADDED_TO_STAGE, OnAddedToStage)
}
private function UpdateSustain():void {
var constantRelease:Number = 255 * (this.constantSustain - 1);
this.sustain = new ColorTransform(this.linearSustain, this.linearSustain, this.linearSustain, 1,
constantRelease, constantRelease, constantRelease, 0);
}
private function OnAddedToStage(event:Event) {
this.lastFrame = new BitmapData(stage.stageWidth, stage.stageHeight, true, 0);
}
public function DrawFrame(container:MasterContainer):void {
this.lastFrame.draw(container);
this.bitmapData = lastFrame;
this.lastFrame = this.bitmapData
this.lastFrame.colorTransform(getBounds(this), this.sustain);
}
...最后结果@ 60fps使用移位阶段的靛蓝正弦波作为CustomWave
的输入: