Genome2D探索:建立一个简单的Genome2D应用

原文:http://bbs.wefdc.com/thread-718-1-1.html 现在2D加速框架越来越多了, 目前比较火的, 资料最多的还是Starling. 最终我选择了Genome2D(以下简写为G2D),不为别的, 就为他的效率. 这次我们使用Flash Builder 4.6, 以建立一个IPHONE应用为例, 介绍下G2D框架的使用方法. 1.建立AS移动应用: 1.png 2.填写项目名称, 选择SDK版本为AIR 3.3 Beta1 2.png 3.点击下一步,选择平台为Apple iOS 3.png 4.点击下一步, 选择G2D的SWC文件. 4.png 5.选择0.9.1, 这个是此教程编写时最新版本 5.png 6. 点击完成后, 按F11键, 在弹出的对话框中(如下图所示), 选择on desktop及Apple iPhone4选项 6.png 7.打开STUDY_G2D-app.xml配置文件, 修改以下配置, renderMode这个一定要选择direct, 否则无法使用Stage3D, 当然G2D也无法使用了. 7.png 上面就是程序配置过程. 接下来进行代码编写
package
{
    import com.genome2d.components.GCamera;
    import com.genome2d.components.renderables.GSprite;
    import com.genome2d.context.GContextConfig;
    import com.genome2d.core.GNode;
    import com.genome2d.core.Genome2D;
    import com.genome2d.g2d;
    import com.genome2d.textures.GTexture;
    import com.genome2d.textures.GTextureBase;
    import com.genome2d.textures.GTextureFilteringType;
    import com.genome2d.textures.factories.GTextureFactory;

    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageQuality;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.events.StageOrientationEvent;
    import flash.system.Capabilities;
    import flash.text.TextField;
    import flash.text.TextFormat;
    import flash.text.TextFormatAlign;
    import flash.ui.Multitouch;
    import flash.ui.MultitouchInputMode;

    [SWF(frameRate="60", width="960", height="640", quality="LOW", backgroundColor="0x000000")]
    public class STUDY_G2D extends Sprite
    {
        /**
         * 主容器
         */        
        private var content:GNode;

        public function STUDY_G2D()
        {
            super();

            this.addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
        }

        /**
         * Event.ADDED_TO_STAGE事件 
         * @param event
         * 
         */        
        protected function addedToStageHandler(event:Event):void
        {
            stage.align = StageAlign.TOP_LEFT; 
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.quality = StageQuality.LOW;
            g2dInit();
        }

        /**
         * 配置G2D
         * 
         */        
        private function g2dInit():void
        {
            //设置默认纹理过滤
            GTextureBase.defaultFilteringType = GTextureFilteringType.NEAREST;
            //注册G2D初始化事件
            Genome2D.getInstance().onInitialized.addOnce(onGenomeInitialized);
            //注册G2D失败事件
            Genome2D.getInstance().onFailed.addOnce(onGenomeFailed);
            //G2D上下文配置
            var config:GContextConfig = new GContextConfig();
            //显示应用运行状态
            config.enableStats = true;
            //抗锯齿
            config.antiAliasing = 0;
            //初始化G2D
            Genome2D.getInstance().init(stage, config);
            //多点支持模式
            Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;

        }

        /**
         * 初始化失败
         * 
         */        
        private function onGenomeFailed():void
        {
            var dtf:TextFormat = new TextFormat("Arial", 12);
            dtf.align = TextFormatAlign.CENTER;
            dtf.bold = true;
            dtf.color = 0xFFFFFF;

            var failed:TextField = new TextField();
            failed.defaultTextFormat = dtf;
            failed.width = stage.stageWidth;
            failed.height = 30;
            failed.y = (stage.stageHeight-30)/2;
            failed.text = "Genome2D initialization failed device doesn't support Stage3D renderer.";
            addChild(failed);
        }

        /**
         * 初始化成功
         * 
         */        
        private function onGenomeInitialized():void
        {
            content = new GNode("content");
            Genome2D.getInstance().root.addChild(content);
            regeditCamera();
            startApp();
        }

        /**
         * 注册摄像机
         * 
         */        
        private function regeditCamera():void
        {
            var camera:GCamera = content.addComponent(GCamera) as GCamera;
            camera.mask = 1;
            camera.index = 0;
        }
        // 加入一个45度角的红色方块
        private function startApp():void
        {
            var node:GNode = new GNode();
            node.transform.rotation = 45*Math.PI/180;
//创建红色方块纹理
            var texture:GTexture = GTextureFactory.createFromColor("red", 0xFF0000, 100, 100);
            var sp:GSprite = node.addComponent(GSprite) as GSprite;
            sp.setTexture(texture);
            content.addChild(node);
        }

    }
}
大家看着很长, 其实没多少步骤. 首先注册Event.ADDED_TO_STAGE事件, 在此事件处理方法中设置舞台属性; 接下来初始化G2D, 设置G2D的属性以及注册G2D初始化事件; 在G2D初始化事件中新建一个根节点(GNode), 并加到G2D的root中; 注册并添加一个摄像机; 接下来就可以向根节点中加入各种需要3D加速的2D图像了. 关于GTextureBase.defaultFilteringType 纹理滤镜, 可以参考 纹理过滤模式中的Bilinear、Trilinear以及Anistropic Filtering这篇文章理解纹理滤镜. 按F11运行, 效果如下: 8.png

1 comment to Genome2D探索:建立一个简单的Genome2D应用

  • I just want to mention I am newbie to blogging and honestly enjoyed this web page. Most likely I¡¯m planning to bookmark your blog . You absolutely have great articles. Thanks a bunch for sharing your website.

    [Reply]

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre lang="" line="" escaped="">