AS 3制御ボタンの緩動スケーリング
次のクラスは、直接起動クラスに設定し、実行して効果を見ることができます.
package
{
import flash.display.Graphics;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
public class TestEasing extends Sprite
{
private var _easing:Number=0.3;
private var _bigScaleX:Number=4;
private var _bigScaleY:Number=4;
public function TestEasing()
{
if(stage)
{
addToStage();
}
else this.addEventListener(Event.ADDED_TO_STAGE,addToStage);
}
private function addToStage(e:Event = null):void
{
var mc:MovieClip;
var graphics:Graphics;
var size:Number=50;
for(var i:int = 0; i < 4; i++)
{
mc = new MovieClip();
graphics = mc.graphics;
graphics.beginFill(0xFF0000);
graphics.drawRect(-size/2,-size/2,size,size);
graphics.endFill();
mc.x = int(i%30) * (mc.width + 50)+100;
mc.y = 100;
mc.isOver = false;
mc.buttonMode = true;
this.addChild(mc);
mc.addEventListener(MouseEvent.MOUSE_OVER,mouseOver);
mc.addEventListener(MouseEvent.MOUSE_OUT,mouseOut);
}
}
private function onEnterFrameHandler(e:Event):void
{
var tMc:MovieClip=e.target as MovieClip;
if(!tMc)return;
var tx:Number;
var targetScale:Number;
if(tMc.isOver)
{
targetScale=_bigScaleX;
}else
{
targetScale=1;
}
tx=targetScale-tMc.scaleX;
tMc.scaleX+=tx*_easing;
tMc.scaleY+=tx*_easing;
if(Math.abs(tx)<0.01)
{
tMc.scaleX=targetScale;
tMc.scaleY=targetScale;
tMc.removeEventListener(Event.ENTER_FRAME,onEnterFrameHandler);
}
}
private function mouseOver(e:MouseEvent):void
{
var tMc:MovieClip=e.target as MovieClip;
if(!tMc)return;
tMc.isOver=true;
if(!tMc.hasEventListener(Event.ENTER_FRAME))
{
tMc.addEventListener(Event.ENTER_FRAME,onEnterFrameHandler);
}
}
private function mouseOut(e:MouseEvent):void
{
var tMc:MovieClip=e.target as MovieClip;
if(!tMc)return;
tMc.isOver=false;
if(!tMc.hasEventListener(Event.ENTER_FRAME))
{
tMc.addEventListener(Event.ENTER_FRAME,onEnterFrameHandler);
}
}
}
}