[]急急急,如何设置按钮点击加载ani

我新手,还有许多不懂,请赐教。有一个ani动画,点击一个按钮在屏幕上显示这个动画
已邀请:

Aar0n

赞同来自: Alex Huang

package {
    import laya.net.Loader;
    import laya.net.ResourceVersion;
    import laya.utils.Handler;
    import laya.webgl.WebGL;
    import laya.utils.Mouse;
    import laya.ui.Button;
    import laya.display.Sprite;
    import laya.display.Animation;
    import laya.maths.Rectangle;
    import laya.display.Stage;
    
    public class LayaUISample {
        private var skin:String = "https://layaair.ldc.layabox.co ... 3B%3B
        private var aniPath:String = "https://layaair.ldc.layabox.co ... 3B%3B
        public function LayaUISample() {
            //初始化引擎
            
            Laya.init(600, 400,WebGL);
            Laya.stage.alignV = Stage.ALIGN_MIDDLE;
            Laya.stage.alignH = Stage.ALIGN_CENTER; 
            Laya.stage.scaleMode = "showall";
            Laya.stage.bgColor = "#232628";
            
            //激活资源版本控制
ResourceVersion.enable("version.json", Handler.create(this, beginLoad), ResourceVersion.FILENAME_VERSION);
        }
        
        private function beginLoad():void {
            //加载引擎需要的资源
            Laya.loader.load(aniPath, Handler.create(this,onLoaded),null,Loader.ATLAS);
        }
        
        private function onLoaded():void {
            //加载按钮资源
            Laya.loader.load(skin,Handler.create(this,onBegin)) 
    }
     private function onBegin():void{
            //按钮成功后会调函数中添加按钮
            var btn:Button = new Button(skin);
         Laya.stage.addChild(btn);
            btn.pos(100,100);
            //按钮被点击的回调
            btn.clickHandler=new Handler(this, onClickButton)
     }
     private function onClickButton():void{
         //创建动画并播放
            var ani:Animation = new Animation();
            ani.loadAtlas(aniPath);
            ani.interval = 30;
            ani.index =1;
            ani.play();
            trace(ani);
            //获取图片边界得到尺寸,然后把中心点放到中间,默认在左上角
            var bounds:Rectangle = ani.getGraphicBounds();
            ani.pivot(bounds.width / 2, bounds.height / 2);
            
            ani.pos(Laya.stage.width / 2, Laya.stage.height / 2);
            
            Laya.stage.addChild(ani); 
     }
}
}
 
 

要回复问题请先

商务合作
商务合作