如何根据鼠标位置更改MovieClip透明度? [英] How to change MovieClip transparency based on mouse position?
问题描述
使用一些基本的数学算法,我认为我已经掌握了它,但是看起来我看到了一个奇怪的图形错误(也许是?):
戒指的中间是鼠标的位置。
>处理透明度的部分代码:
private function update(e:
{
for(var i:int = 0; i< buttons.length; i ++){
lightFact = getDistance(buttons [i])
lightBrightness = lightPower - (lightFact * 10)
buttons [i] .alpha = lightBrightness
}
}
getDistance只是从块到鼠标的距离。
每个矩形都是一个影片剪辑。 / p>
如果您正在尝试这样做:
然后我认为你的问题基本上就是你的alpha值是范围从0到大约3000或类似的东西。这会导致奇怪的效果。该值需要从0到1平滑地变化(因此它需要是浮点数,如 Number
)。
>包
{
import flash.display。*;
导入flash.events。*;
public class lightFactTest扩展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
{
//为矩形数组创建背景。
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);
//#创建一个精灵(矩形)的网格。 $(变量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);
}
私钥函数lightCalc(e:Event):void
{
size_Glow = 3.5;
size_Radius = 0.64;
//#遍历数组计算每个距离,然后计算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);
//#这是您真正需要关注的代码...
boxesArray [i] .alpha = Math.min(1 / d * 10,1)*( Math.PI / 0.5 - Math.min(size_Radius,0))* size_Glow;
$ b code
$ b希望有所帮助!
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 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:
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;
}
}
}
}
Hope that helps!
这篇关于如何根据鼠标位置更改MovieClip透明度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!