Genome2D探索:添加鼠标事件

原文:http://bbs.wefdc.com/thread-1492-1-1.html 书接上文, 我们继续在startApp方法中加入两行代码:
node.mouseEnabled = true; node.onMouseClick.add(nodeMouseClick);
首先启用鼠标监听功能, 然后添加鼠标单击事件 注意, G2D的事件和AS3的事件是有区别的. G2D采用的是Singal事件流机制. 想更多了解这种机制的, 可以点击此链接 接下来我们完成事件监听函数:
private functionnodeMouseClick(signal:GMouseSignal):void { node.transform.setColor(Math.random(),Math.random(),Math.random(),1); }
这个函数的参数是G2D内置的GMouseSignal类 我们在这个函数中实现的效果是更改物体的色彩.保留透明度不变, 随机修改RGB这3个数值. P.S. 这次我们使用了最新的G2D的Genome2D.0.9.1.1121.swc, 注意有改动的地方是: //G2D上下文配置 var config:GContextConfig = new GContextConfig(new Rectangle(0,0,960,640)); GContextConfig类初始化时传Rectangle的参数,设定舞台. 全部代码如下:
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.signals.GMouseSignal;
    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.geom.Rectangle;
    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="0xFFFFFF")]
    public class STUDY_G2D_AS extends Sprite
    {
        /**
         * 主容器
         */        
        private var content:GNode;

        public function STUDY_G2D_AS()
        {
            super();

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

        /**
         * Event.ADDED_TO_STAGE事件 
         * @param event
         * 
         */        
        protected function addedToStageHandler(event:Event):void
        {
            this.removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
            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(new Rectangle(0,0,960,640));
            //显示应用运行状态
            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;
        }

        private var node:GNode;
        private var degree:int=0;
        private const n:Number = Math.PI/180;

        private function startApp():void
        {
            node = new GNode();
            var texture:GTexture = GTextureFactory.createFromColor("white", 0xFFFFFF, 100, 100);
            var sp:GSprite = node.addComponent(GSprite) as GSprite;
            sp.setTexture(texture);
            content.addChild(node);

            node.mouseEnabled = true;
            node.onMouseClick.add(nodeMouseClick);

            Genome2D.getInstance().onPreUpdate.add(starUpdateHandler);
        }

        private function nodeMouseClick(signal:GMouseSignal):void
        {
            node.transform.setColor(Math.random(),Math.random(),Math.random(),1);
        }

        private function starUpdateHandler(deltaTime:Number):void
        {
            degree++;
            node.transform.rotation = n*degree;
        }

    }
}
效果如下: STUDY_G2D_AS_2

58 comments to Genome2D探索:添加鼠标事件

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="">