如何根据鼠标位置更改MovieClip透明度?

时间:2016-06-11 22:32:01

标签: actionscript-3 graphics 2d rectangles

所以,我试图制作一个矩形网格,每个矩形网格越接近它就越透明。

使用一些基本的数学,我以为我得到了它,但似乎我得到了一个奇怪的图形错误(可能?)显示在这里:

enter image description here

戒指的中间是鼠标所在的位置。

处理透明度的部分代码:

private function update(e:Event = null):void
{
    for (var i:int = 0; i < buttons.length; i++) {
        lightFact = getDistance(buttons[i])
        lightBrightness = lightPower - (lightFact * 10)
        buttons[i].alpha = lightBrightness
    }
}

getDistance只是从块到鼠标的距离。

如果重要的话,每个矩形都是一个影片剪辑。

1 个答案:

答案 0 :(得分:5)

如果您尝试这样做:

lightFact

然后我认为你的问题基本上是你的alpha值从0到大约3000或类似的东西。这将导致奇怪的效果。该值需要从0到1平滑范围(因此它需要是Number中的浮点数)。

以下代码生成了我为您编写的图像,它将帮助您开始正确的方向:

package
{

    import flash.display.*;
    import flash.events.*;


    public class lightFactTest extends MovieClip
    {

        private var boxesArray: Array = new Array();
        private var xDist: Number = 0;
        private var yDist: Number = 0;
        private var d: Number = 0;
        private var size_Glow : Number = 0;
        private var size_Radius : Number = 0;

        public function lightFactTest(): void
        {
            // creates a background for rectangles array.
            var BG_box: Sprite = new Sprite();
            BG_box.graphics.lineStyle();
            BG_box.graphics.beginFill(0x080839);
            BG_box.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
            BG_box.graphics.endFill();
            addChild(BG_box);

            //# creates a grid of sprites (rectangles).
            for (var i:int = 0; i < (stage.stageWidth / 10); i++)
            {
                for (var j:int = 0; j < (stage.stageHeight / 10); j++)
                {
                    var box: Sprite = new Sprite();
                    box.graphics.lineStyle();
                    box.graphics.beginFill(0xFFFFFF);
                    box.graphics.drawRect(0, 0, 10, 10);
                    box.graphics.endFill();
                    addChild(box);
                    box.x += i*10; //+ 50;
                    box.y += j*10; //+ 50;
                    boxesArray.push(box);
                }
            }

            addEventListener(Event.ENTER_FRAME, lightCalc);
        }


        private function lightCalc(e: Event): void
        {
            size_Glow = 3.5;
            size_Radius = 0.64;

            //# iterates through the array calculating each distance and then alpha.
            for (var i:int = 0; i < boxesArray.length; i++)
            {
                xDist = Math.abs(stage.mouseX - boxesArray[i].x);
                yDist = Math.abs(stage.mouseY - boxesArray[i].y);
                //var d: Number = Math.pow(xDist * xDist + yDist * yDist, 0.5);
                d = Math.sqrt(xDist * xDist + yDist * yDist) / (size_Radius / 5);

                //# This is the code that you really need to focus on...
                boxesArray[i].alpha = Math.min(1 / d * 10, 1 ) * (Math.PI / 0.5 - Math.min(size_Radius, 0) ) * size_Glow;

            }
        }
    }
}

希望有所帮助!