2009-05-27 70 views
2

我使用canvas.moveTo(0, 0); canvas.lineTo(100, 100);将一条线添加到画布,但我希望用户移动鼠标来设置线条的旋转。谷歌建议使用rotation属性,但我没有对线对象的引用。我可以获得对该线的参考,还是应该旋转整个画布?这有可能吗?是否可以使用Flex旋转动态添加的行?

回答

2

通常你操纵其上的图形绘制的表面 - 通常是一个Sprite实例,因为它是如此轻便,非常适合任务。如果您创建了一个新的Sprite,使用它的Graphics对象来绘制线条,形状等,则将Sprite添加到UIComponent中 - 您无法直接将Sprite添加到Canvas,而无需首先将其包装到UIComponent实例中 - - 然后将UIComponent添加到您的画布上,您可以通过旋转,移动等直接操作Sprite。

这通常是如何完成的,通过重写createChildren()(如果对象的目的是为了组件实例)或使用其他方法,具体取决于您的需要。例如:

override protected function createChildren():void 
{ 
    super.createChildren(); 

    // Create a new Sprite and draw onto it 
    var s:Sprite = new Sprite(); 
    s.graphics.beginFill(0, 1); 
    s.graphics.drawRect(0, 0, 20, 20); 
    s.graphics.endFill(); 

    // Wrap the Sprite in a UIComponent 
    var c:UIComponent = new UIComponent(); 
    c.addChild(s); 

    // Rotate the Sprite (or UIComponent, whichever your preference) 
    s.rotation = 45; 

    // Add the containing component to the display list 
    this.addChild(c); 
} 

希望它有帮助!

1

嗯......将一个Sprite添加到画布上,然后将该线绘制到Sprite的图形对象上。然后你可以旋转Sprite等等。如果你愿意的话,你可以旋转画布,但是如果你只想将它作为一个Sprite来处理,那么额外的开销就是创建一个画布(请注意,Canvas会将Sprite扩展到链条的某个地方)。

看看这个例子从ASDocs:Rotating things

1

什么是画布(实际Canvas没有lineTo()或moveTo()方法)?

好像你可能正在操纵Canvas的图形对象。在这种情况下,你最好做以下

private var sp : Sprite; 
//canvas is whatever Canvas you wish to add the sprite to 
private function addLine(canvas : Canvas) : void { 
sp = new Sprite(); 
/* Do the drawing of the sprite here, 
    such as sp.graphics.moveTo or sp.graphics.lineTo */ 
sp.rotation = 45; 
canvas.rawChildren.addChild(sp); 
} 

然后,每当你想改变旋转刚刚更新sp.rotation(这是现在在你的画布)

相关问题