阅读更多
flash player10提供了对3D的支持,虽然功能不够强大,但是有些功能还是很不错的,下面是用10写的一个翻牌游戏。
public class Card extends Sprite
{
[Embed(source='../../../lib/goodbyewow02.jpg')]
private var FrontFace:Class;
[Embed(source='../../../lib/goodbyewow04.jpg')]
private var BackFace:Class;
private var fFace:Sprite;
private var bFace:Sprite;
private var cleanObj:Sprite;
private var dirtyObj:Sprite;
public function Card()
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
var frontBg:DisplayObject = new FrontFace();
var backBg:DisplayObject = new BackFace();
frontBg.x = backBg.x = - frontBg.width / 2; frontBg.y = backBg.y = - frontBg.height / 2; fFace = new Sprite();
bFace = new Sprite();
fFace.addChild(frontBg);
bFace.addChild(backBg);
//背面需要额外处理一下,
//翻转过来,并且暂时隐藏
bFace.scaleX *= -1;
bFace.visible = false;
bFace.buttonMode = fFace.buttonMode = true;
//测试的文字
var testTF:TextField = new TextField();
testTF.width = 400;
testTF.height = 200;
testTF.textColor = 0xFFFFFF;
testTF.htmlText = "我的文字描述"; testTF.appendText(testTF.text);
testTF.selectable = true;
testTF.multiline = true;
testTF.wordWrap = true;
testTF.x = frontBg.x + 10;
testTF.y = frontBg.y + 200;
testTF.type = TextFieldType.INPUT;
fFace.addChild(testTF);
cleanObj = new Sprite();
dirtyObj = new Sprite();
dirtyObj.addChild(fFace);
dirtyObj.addChild(bFace);
addChild(dirtyObj);
addChild(cleanObj);
addEventListener(MouseEvent.CLICK, clickHandler);
}
private function clickHandler(e:MouseEvent = null):void
{
// 修改文字时不动
if (e.target is TextField) return;
if (e.target == fFace || e.target.parent == fFace)
{
// flip to back
dirty();
TweenLite.killTweensOf(dirtyObj); TweenLite.to( dirtyObj, 1, {
rotationY:180,ease: Back.easeOut,
onComplete: cleanBackFace,
onUpdate: update} );
} else if (e.target == bFace || e.target.parent == bFace) {
dirty();
TweenLite.killTweensOf(dirtyObj);
TweenLite.to( dirtyObj, 1, {
rotationY:0,
ease: Back.easeOut,
onComplete: cleanFrontFace,
onUpdate: update
} );
}
}
private function update():void
{
bFace.visible = dirtyObj.rotationY > 90 && dirtyObj.rotationY < 270;
fFace.visible = !bFace.visible;
}
private function cleanFrontFace():void
{
if (dirtyObj.contains(fFace))
{
dirtyObj.removeChild(fFace); cleanObj.addChild(fFace);
}
}
private function cleanBackFace():void
{
if (dirtyObj.contains(bFace))
{ dirtyObj.removeChild(bFace);
cleanObj.addChild(bFace);
bFace.scaleX *= -1;
}
}
private function dirty():void
{
if (cleanObj.contains(fFace) || cleanObj.contains(bFace))
{
var face:DisplayObject = cleanObj.getChildAt(0);
cleanObj.removeChild(face);
dirtyObj.addChild(face);
if (face == fFace)
{
dirtyObj.swapChildren(face, bFace);
} else {
bFace.scaleX *= -1;
}
}
}
}
同时如果在mc中有动态文本,和图片,哪么在选择rotationY后,就会变的模糊,解决的办法是在他停止的时候删除后在重新添加。
http://www.webdesign-cn.com
http://www.levs.cn