在传统的Flash中,MovieClip就是影片剪辑,具有时间轴,也有它该有的属性,比如x坐标,ScaleX缩放等。还有他该有的方法比如gotoAndStop。在Starling中,也同样有MovieClip这个对象,它的父类是Image。在Starling中,MovieClip是否也像传统的MovieClip一样,也是个动画,也有相同的属性和方法呢。在这里的答案是。Starling中的MovieClip是模仿传统的MovieClip,构造出时间轴的样子。不过有些属性和方法,还是有些出入的。
比如,传统有gotoAndStop方法以及有gotoAndPlay方法,但在Starling中,只有一个属性currentFrame代替了这两个方法的效果。

先体验下Starling中创建的MovieClip的效果:


说下Starling中MovieClip的创建过程。
之前,说过纹理集。那么创建MovieClip和纹理集一起,会很方便的。因为MovieClip的第一个参数是Texture的集合,表示MovieClip的每一帧的纹理。正好符合纹理集的特点,并且,纹理集还有个方法返回的就是这样一个集合。
准备MovieClip的纹理集的贴图。如下图所示:

准备纹理贴图XML

<?xml version="1.0" encoding="UTF-8"?>
<TextureAtlas imagePath='../../../assets/images/bird.png'>
     <SubTexture name='bird_1' x='0'  y='0' width='166' height='135'/>
     <SubTexture name='bird_2' x='0'  y='135' width='166' height='135'/>
     <SubTexture name='bird_3' x='0'  y='285' width='166' height='162'/>
     <SubTexture name='bird_4' x='0'  y='447' width='166' height='135'/>
</TextureAtlas>

 

有了这些准备。就开始代码走起。创建贴图纹理-创建贴图xml-创建纹理集-创建MovieClip。然后设置MovieClip的。比如设置其loop属性,默认是ture,也就是循环播放的。将它添加到Starling的显示列表中。还有重要的一点,要将它添加到Starling的juggler中。自己还可以额外增加一些功能。比如暂停,播放,停止等控制。好了。把代码贴起,更容易明白了。

package com.vini123.game
{
    import starling.core.Starling;
    import starling.display.MovieClip;
    import starling.display.Sprite;
    import starling.events.Event;
    import starling.textures.Texture;
    import starling.textures.TextureAtlas;
    
    
    /**
     * @author vini123
     * @email lichking_lin86@qq.com
     * @weixin vinifly
     * @date 2014-4-28 下午3:10:28
     * 
     */
    
    public class Game extends Sprite
    {
        [Embed(source = "../../../assets/images/bird.png")]
        private const Bird:Class;
        
        [Embed(source = "../../../assets/images/bird.xml" , mimeType="application/octet-stream")]
        private const BirdXml:Class;
        
        private var _textureAtlas:TextureAtlas;
        private var _bird:MovieClip;
        
        public function Game()
        {
            addEventListener(Event.ADDED_TO_STAGE , addStageHandler);
        }
        
        private function addStageHandler(e:Event):void
        {
            removeEventListener(Event.ADDED_TO_STAGE , addStageHandler);

            var texture:Texture = Texture.fromBitmap(new Bird());
            var xml:XML = new XML(new BirdXml());
            _textureAtlas = new TextureAtlas(texture , xml);
            
            _bird = new MovieClip(_textureAtlas.getTextures("bird_"), 12);
            _bird.loop = true ;
            addChild(_bird);
            _bird.play();
            Starling.juggler.add(_bird);
        }
        
        public function play():void
        {
            _bird.play();
        }
        
        public function pause():void
        {
            _bird.pause();
        }
        
        public function stop():void
        {
            _bird.stop();
        }
    }
}

 

package com.vini123
{
    import com.vini123.game.Game;
    import com.vini123.ui.Button;
    
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    
    import starling.core.Starling;
    import starling.events.Event;
    
    
    /**
     * @author vini123
     * @email lichking_lin86@qq.com
     * @weixin vinifly
     * @date 2014-4-28 下午3:05:31
     * 
     */
    
    [SWF(width = "640" , height="350" , frameRate="30")]
    public class Main extends Sprite
    {
        private var _starling:Starling;
        private var _game:Game;
        
        private var _btnList:Vector.<Button> = new Vector.<Button>();
        private var _state:int = -1 ; //0状态表示在播放,1表示暂停,2表示停止
        
        public function Main()
        {
            _starling = new Starling(Game , stage) ;
            _starling.antiAliasing = 2 ;
            _starling.start();
            
            _starling.addEventListener(starling.events.Event.ROOT_CREATED , rootCreatedHandler);
        }
        
        private function rootCreatedHandler(e:starling.events.Event):void
        {
            _game = Starling.current.stage.getChildAt(0) as Game;
            _game.y = 10;
            
            addEventListener(flash.events.Event.ENTER_FRAME , enterHandler);
            
            _btnList.push(new Button("播放") , new Button("暂停") , new Button("停止") );
            
            var len:int = _btnList.length;
            
            for(var i:int = 0 ; i< len  ; i++)
            {
                addChild(_btnList[i]);
                _btnList[i].x = 10 + (_btnList[i].width + 10) * i;
                _btnList[i].y = stage.stageHeight - _btnList[i].height -10;
                
                _btnList[i].addEventListener(MouseEvent.CLICK , mouseEventHandler);
            }
            
            _state = 0;
        }
        
        private function enterHandler(e:flash.events.Event):void
        {
            _game.x += 2;
            
            if(_game.x >= stage.stageWidth)
            {
                _game.x = 0;
            }
        }
        
        private function mouseEventHandler(e:MouseEvent):void
        {
            resetSelect();
            switch((e.currentTarget as Button).label)
            {
                case "播放":
                    if(_state !=0)
                    {
                        _state = 0;
                        addEventListener(flash.events.Event.ENTER_FRAME , enterHandler);
                        _game.play();
                    }
                    _btnList[0].isSelect = true;
                    break;
                case "暂停":
                    removeEventListener(flash.events.Event.ENTER_FRAME , enterHandler);
                    _state = 1;
                    _game.pause();
                    _btnList[1].isSelect = true;
                    break
                case "停止":
                    removeEventListener(flash.events.Event.ENTER_FRAME , enterHandler);
                    _state = 2;
                    _game.stop();
                    _btnList[2].isSelect = true;
                    break;
                default:
                    break;
            
            }
        }
        
        private function resetSelect():void
        {
            var len:int = _btnList.length;
            
            for(var i:int = 0 ; i< len  ; i++)
            {
                _btnList[i].isSelect = false;
            }
        }
    }
}