2009-05-27 40 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(這是現在在你的畫布)

相關問題