2

私はスプライト(円)を持っています、私はactionscriptでそれを作りました。擬似コードは次のとおりです。

    var board:Sprite = new Sprite();
    var spDot:Sprite = new Sprite()
    spDot.graphics.lineStyle(1,0x0000CC);   
    spDot.graphics.beginFill(0xFFFFFF); //white;
    spDot.graphics.drawCircle(0,0,dZ);  
    spDot.graphics.endFill();
    spDot.name="v";
    board.addChild(spDot);

現在のスプライトの色(白)を黒に変更するためのボタン「btnA」があります。

btnA.addEventListener(MouseEvent.CLICK, changeColor);
function changeColor(evt:MouseEvent){
     (board.getChildByName("v") as Sprite).graphics.beginFill(0x000000);
}

しかし、私の問題は、この部分でエラーを返しました:(board.getChildByName("v") as Sprite).graphics.beginFill(0x000000);

(board.getChildByName("v") as Sprite).graphics.beginFill(0x000000);実は色を変えるのに使うと思いました。何か考えはありますか?ありがとうございました!

4

2 に答える 2

2

色付けする必要のある表示オブジェクトの実装を開示しないことをお勧めします。このステートメントに同意する場合は、ColorTransformを使用できます;)

ColorTransformクラスを使用すると、表示オブジェクトの色の値を調整できます。色調整または色変換は、赤、緑、青、およびアルファ透明度の4つのチャネルすべてに適用できます。

例:

btnA.addEventListener(MouseEvent.CLICK, buttonDidClick);

function buttonDidClick(e:MouseEvent) {
    transformColor(board.getChildByName("v"), 0x000000);
}

function transformColor(target:DisplayObject, color:uint):void {
    var colorTransform:ColorTransform = new ColorTransform();
    colorTransform.color = color;
    target.transform.colorTransform = colorTransform;
}
于 2015-03-25T11:38:39.310 に答える
1

この最も簡単な方法は、グラフィックデータをクリアして、グラフィックオブジェクトに再描画することです。

function drawCircle(sprite:Sprite, radius:Number = 40, fillColor:int = 0):Sprite
{
  if (!sprite) return null;

  const g:Graphics = sprite.graphics;

  g.clear();
  g.lineStyle(1, 0x0000CC);   
  g.beginFill(fillColor);
  g.drawCircle(0, 0, radius);
  g.endFill();

  return sprite;
}

また、特定のタイプを期待する必要がある場合は、暗黙の呼び出しを使用しないことを強くお勧めします。

function changeColor(evt:MouseEvent)
{
  // hides the fact, that you're having an instance of am unexpected type
  (board.getChildByName("v") as Sprite).graphics.beginFill(0x000000);
}

有効な参照がありますが、1009/nullポインタになります。

function changeColor(evt:MouseEvent)
{
  // fails fast - for example when you change from sprite to bitmap.
  Sprite(board.getChildByName("v")).graphics.beginFill(0x000000);
}

この場合、速く失敗することがキャストに適した方法です。

于 2013-01-02T10:12:00.527 に答える