2010-06-24 73 views
0

在Flex 4中,如何將光標更改爲在運行時處確定的位圖圖像?我見過的所有示例都使用CursorManager.setCursor將光標設置爲編譯時指定的類。將鼠標光標更改爲位圖(Flex 4)?

我想要做的是將光標更改爲其位圖數據由上下文確定的位圖。

回答

2
package cursor 
{ 
import flash.display.BitmapData; 
import flash.display.PixelSnapping; 

import mx.core.BitmapAsset; 

public class RuntimeBitmap1 extends BitmapAsset 
{ 

    public static var staticBitmapData:BitmapData; 

    public function RuntimeBitmap1() 
    { 
     super(staticBitmapData); 
    } 
} 
} 

用法:

var bitmapData:BitmapData = new BitmapData(50, 50, false, 0x88888888); 
RuntimeBitmap1.staticBitmapData = bitmapData; 
cursorManager.setCursor(RuntimeBitmap1, 0); 
+0

這工作,謝謝! – justkevin 2010-06-25 14:41:12

0

這裏有幾個簡單的步驟來更改默認光標用位圖圖像:

  1. 使用的圖像創建類型位圖的光標你選擇。您也可以在運行時動態設置bitmapData。
    
    var DEFAULT_CURSOR_IMAGE : Class; 
    var myCursorBitmap : Bitmap; 
    ... 
    myCursorBitmap = new DEFAULT_CURSOR_IMAGE();
  2. 註冊以接收鼠標移動事件並相應地更新光標位置。
    
    function onMouseMove(event : MouseEvent) : void 
    { 
        myCursorBitmap.x = event.localX; 
        myCursorBitmap.y = event.localY; 
    }
  3. 使用Mouse.hide()隱藏真實光標。

  4. 顯示您的自定義光標。您可以稍後通過動態設置bitmapData來更新光標形狀。

    
    addChild(myCursorBitmap); 
    ... 
    myCursorBitmap.bitmapData = myNewCursor;

要恢復默認光標,從舞臺上刪除您的光標位圖,並呼籲Mouse.show()。

+0

這是一個Flex項目,所以我無法將addChild添加到主要的mxml類。 – justkevin 2010-06-25 14:41:57

+0

@justkevin:是的,你是對的 - 在這種情況下,你必須把它放在一個UIComponent中。 – 2010-06-28 09:43:48

1

我想畫一個UIComponent作爲遊標。

我管理它使用Maxims的答案和這個的組合。我不得不做出馬克西姆答案的唯一變化是A S如下:

public function RuntimeBitmap1() 
{ 
    super(RuntimeBitmap1.staticBitmapData); 
} 

否則staticBitmapData通過爲空值排在構造函數中。