《GETTING STARTED WITH CITRUS ENGINE, STARLING AND BOX2D》

FlashDevelop

项目右键->Properties->Compiler Options->SWC Include Libraries(lib/CEV3-1-3-ALL.swc)
Flash Player 11

public class Main extends StarlingCitrusEngine //使用Starling
{
    
    public function Main():void 
    {
        if (stage) init();
        else addEventListener(Event.ADDED_TO_STAGE, init);
    }
    
    private function init(e:Event = null):void 
    {
        removeEventListener(Event.ADDED_TO_STAGE, init);
        
        setUpStarling(true); //启动Starling
        state = new GameState(); //启动游戏场景

        sound.addSound("Hurt", "../sounds/hurt.mp3"); //添加声音
        sound.addSound("Kill", "../sounds/kill.mp3");
    }
        
}
public class GameState extends StarlingState //Starling场景
{

    [Embed(source="../res/Hero.xml", mimeType="application/octet-stream")]
    private var _heroConfig:Class;

    [Embed(source="../res/Hero.png")]
    private var _heroPng:Class;
        
    private var _ce:CitrusEngine; //用来播放音效
    
    public function GameState() 
    {
        super();
    }
    
    override public function initialize():void //覆盖initialize方法,先调用父类的initialize方法
    {
        super.initialize();
        
        _ce = CitrusEngine.getInstance(); //注册Citrus Engine
        
        var box2D:Box2D = new Box2D("box2D"); //添加物理世界
        box2D.visible = true;
        add(box2D);
        
        add(new Platform("bottom", {x:stage.stageWidth / 2, y:stage.stageHeight, width:stage.stageWidth}));
        add(new Platform("cloud", {x:250, y:250, width:170, oneWay:true}));
         
        var coin:Coin = new Coin("coin", { x:360, y:200, view:"../res/jewel.png" } ); //图片
        coin.onBeginContact.add(coinTouched);
        add(coin);
         
        var bitmap:Bitmap = new _heroPng();
        var texture:Texture = Texture.fromBitmap(bitmap);
        var xml:XML = XML(new _heroConfig());
        var sTextureAtlas:TextureAtlas = new TextureAtlas(texture, xml);
        var hero:Hero = new Hero("hero", { x:100, y:350, width:60, height:135 } );
        hero.view = new AnimationSequence(sTextureAtlas, ["walk", "duck", "idle", "jump", "hurt"], "idle"); //动画
        hero.onGiveDamage.add(heroAttack); //绑定音效
        hero.onTakeDamage.add(heroHurt);
        add(hero);
         
        var enemy:Enemy = new Enemy("enemy", {x:stage.stageWidth - 50, y:350, width:46, height:68, leftBound:20, rightBound:stage.stageWidth - 20});
        add(enemy);
    }

    private function heroHurt():void 
    {
        _ce.sound.playSound("Hurt", 1, 0);
    }
    
    private function heroAttack():void 
    {
        _ce.sound.playSound("Kill", 1, 0);
    }
        
    private function coinTouched(contact:b2Contact):void 
    {
        trace('coin touched by an object');
    }

}

使用console调试
Tab

set box2D visible false
set hero y 0

 

posted @ 2013-02-13 12:10  joojoosue  阅读(328)  评论(1编辑  收藏  举报