如果我没错的话,这段代码将展示你想要看到的内容。
首先,我忽略了你最初设置的对象和信息,并专注于示例情况本身;为“单块”(使用下面的示例,任何对象都可以,甚至是纹理)伪投影阴影。
我的理由是,使用 ActionScript 的 Matrix
类非常容易,这是一个值得学习的方便工具。
解决方案:
您可以使用内置的 Matrix
类对 DisplayObject
进行扭曲变换。
尝试这个示例:
(“有用”的部分在 _EF EnterFrame 处理程序中 ;) )
import flash.display.MovieClip;
import flash.geom.Matrix;
import flash.events.Event;
import flash.display.BitmapData;
const PIP180:Number = Math.PI / 180;
const MAX_SHADOW_HEIGHT_MULTIPLIER:Number = 0.25;
const ANIM_DEG_PER_FRAME:Number = 1.0 * PIP180;
var tx:BitmapData = new MonolithTexture();
var skew:Number = -10 * PIP180;
var mono:MovieClip = new MovieClip();
mono.graphics.beginBitmapFill(tx);
mono.graphics.drawRect(0, -tx.height, tx.width, tx.height);
mono.graphics.endFill();
mono.x = stage.stageWidth / 2;
mono.y = stage.stageHeight - 100;
mono.width = 100;
mono.height = 300;
var shad:MovieClip = new MovieClip();
shad.graphics.beginFill(0x000000);
shad.graphics.drawRect(0, -tx.height, tx.width, tx.height);
shad.graphics.endFill();
addChild(shad);
addChild(mono);
addEventListener(Event.ENTER_FRAME, _EF);
function _EF(e:Event):void {
skew = (skew + ANIM_DEG_PER_FRAME) % Math.PI;
var mtx:Matrix = new Matrix(mono.scaleX, 0, Math.cos(skew), mono.scaleY * Math.sin(skew) * MAX_SHADOW_HEIGHT_MULTIPLIER, mono.x, mono.y);
shad.transform.matrix = mtx;
}
现在你需要知道的是如何将这个应用到你的情况中,以下是N个因素:
Q1:你想从什么角度投影阴影?
A1:水平因素是skew
变量本身,而垂直角度在此处被存储为常量,称为MAX_SHADOW_HEIGHT_MULTIPLIER
Q2:你只想向上投影阴影还是自由投影?
A2:如果只向上投影阴影,则保持skew
在正范围内,否则让它取负值以获得“向下”阴影。
P.S.:如果您将对象的内部呈现为不与0 y对齐的基点,则可以使它们看起来漂浮/下沉,或者使用相反符号的预定义值垂直偏移两个对象。