如果我是对的,此代码将显示您想看到的内容。
首先,我忽略了您对对象和信息的初始设置,而是专注于示例情况本身;Matrix
“整体”的假投影阴影(任何对象都可以使用下面的示例,甚至是纹理) 我的原因是使用ActionScript 类非常容易,这是一个值得学习的方便工具。
解决方案:可以使用内置Matrix
类对DisplayObject
s进行倾斜变换。试试这个例子:
(“有用”的部分在于 _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; // you can also calculate this from an angle, like ... = Math.sin(angle * PIP180);
const ANIM_DEG_PER_FRAME:Number = 1.0 * PIP180; // the shadow creeps at a +1 degree per frame rate
var tx:BitmapData = new MonolithTexture(); // define this BitmapData in the library
var skew:Number = -10 * PIP180; // initial
var mono:MovieClip = new MovieClip();
mono.graphics.beginBitmapFill(tx);
// drawn that way the registration point is 0,0, so it's standing on the ground
mono.graphics.drawRect(0, -tx.height, tx.width, tx.height);
mono.graphics.endFill();
// align monolith to the "ground"
mono.x = stage.stageWidth / 2;
mono.y = stage.stageHeight - 100;
// make it be 100x300 pixel
mono.width = 100;
mono.height = 300;
var shad:MovieClip = new MovieClip();
// colored:
shad.graphics.beginFill(0x000000);
// or textured:
//shad.graphics.beginBitmapFill(tx);
shad.graphics.drawRect(0, -tx.height, tx.width, tx.height);
shad.graphics.endFill();
addChild(shad); // shadow first
addChild(mono); // then the caster object
addEventListener(Event.ENTER_FRAME, _EF);
function _EF(e:Event):void {
// animate skew on the positive half circle
skew = (skew + ANIM_DEG_PER_FRAME) % Math.PI;
// Matrix takes 6 parameters: a, b, c, d, x, y
// for this shadow trick, use them as follows:
// a = width scaling (as mono and shad are drawn in the same way, copy mono.scaleX for a perfect fit
// b = 0, because we don't want to project the vertical axis of transformation to the horizontal
// c = horizontal skew
// d = height scaling * skew * making it a bit flat using the constant
// x = mono.x, ...
// y = mono.y since originally mono and shad look alike, only the Matrix makes shad render differently
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
在正范围内,否则让它为“向下”阴影也取负值
PS:如果您渲染它们不捕捉到 0 y 作为基点的对象的内部,您可以使它们看起来浮动/下沉,或者使用预定义的值垂直偏移两个对象,使用相反的符号。