如何根据鼠标位置更改 MovieClip 的透明度? [英] How to change MovieClip transparency based on mouse position?

查看:18
本文介绍了如何根据鼠标位置更改 MovieClip 的透明度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我正在尝试制作一个矩形网格,每个矩形网格越靠近鼠标越透明.

So, I'm trying to make a grid of rectangles each get more transparent the closer the mouse is to it.

使用一些基本的数学运算,我以为我已经掌握了,但实际上我发现了一个奇怪的图形错误(可能?),如下所示:

Using some basic maths, I thought I had got it, but instead it seems I got a weird graphic bug(maybe?) shown here:

圆环的中间是鼠标所在的位置.

The middle of the rings is where the mouse is.

处理透明度的部分代码:

Part of code that deals with transparency:

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 只是获取方块到鼠标的距离.

getDistance is just getting distance from the block to the mouse.

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

Each rectangle is a movie clip, if that matters.

推荐答案

如果您正在尝试这样做:

If you are trying to do this:

然后我认为您的问题基本上是您的 alpha 值范围从 0 到大约 3000 或类似的值.这会导致奇怪的效果.该值需要在 0 到 1 之间平滑地变化(因此它需要是一个浮点数,如 Number).

Then I think your problem is basically that your alpha value is ranging from 0 to about 3000 or something like that. That's going to cause strange effects. The value needs to range smoothly from 0 to 1 (so it needs to be a floating point number as in Number).

这是生成上面我为您编写的图像的代码,它将帮助您朝着正确的方向开始:

Here is the code which generated the image above which I wrote for you that will get you started in the right direction:

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;

            }
        }
    }
}

希望有帮助!

这篇关于如何根据鼠标位置更改 MovieClip 的透明度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆