zoukankan      html  css  js  c++  java
  • 简单的键盘控制

    通过键盘控制物体的移动,改变物体的线速度即可以改变它的运动状态。通过监听舞台的MouseEvent.MOUSE_DOWN和MouseEvent.MOUSE_UP事件来移动物体。当mouseDown时,设置一个Boolean值记下按下的方向键,在Event.ENTER_FRAME事件中根据设置的Boolean值来移动物体,具体如下:

    1.添加监听

    addEventListener(Event.ENTER_FRAME, onFrame);
    stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDownHandle);
    stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUpHandle);

    2.创建物体,创建时设置一个名称,因为现实对象为MovieClip,所以可以动态增加其属性。

    createCircle(this, 30, 300, 20, 0, false, new Circle());
    private function createCircle(container:DisplayObjectContainer,xpos:int,ypos:int,radius:int,rotation:Number,isStatic:Boolean,obj:MovieClip):void
    {
       var shapeDef:b2CircleDef = new b2CircleDef();
       isStatic?shapeDef.density = 0:shapeDef.density = 5;
       shapeDef.restitution = 0.1;
       shapeDef.friction = 0.4;
       shapeDef.radius = radius / _physcale;
       var bodyDef:b2BodyDef = new b2BodyDef();
       bodyDef.angle = rotation;
       bodyDef.position.Set(xpos / _physcale, ypos / _physcale);
       bodyDef.userData = obj;
       var body:b2Body = _world.CreateBody(bodyDef);
       body.CreateShape(shapeDef);
       body.SetMassFromShapes();
       obj.scaleX = radius*2 / obj.width;
       obj.scaleY = radius * 2 / obj.height;
       obj.name = "hero";
       obj.body = body;
       container.addChild(obj);
    }

    3.处理事件

    private function onKeyDownHandle(e:KeyboardEvent):void 
    {
         setBoolean(e.keyCode, true);
    }
      
    private function onKeyUpHandle(e:KeyboardEvent):void 
    {
         setBoolean(e.keyCode,false);
    }

    private function setBoolean(keyCode:int, flag:Boolean ):void 
    {
         switch(keyCode)
         {
            case 37:
               isLeft = flag;
               break;
            case 38:
               isUp = flag;
               break;
            case 39:
               isRight = flag;
               break;
         }

    }

    private function onFrame(e:Event):void 
    {
         moveBody();
       
         _world.Step(_timeStep, _iteration);
       
         for (var bb:b2Body = _world.m_bodyList; bb; bb = bb.m_next )
         {
            if (bb.m_userData is DisplayObject)
            {
                bb.m_userData.x = bb.GetPosition().x * _physcale;
                bb.m_userData.y = bb.GetPosition().y * _physcale;
                bb.m_userData.rotation = bb.GetAngle() * 180 / Math.PI;
            }
         }
    }

    private function moveBody():void 
    {
         var child:MovieClip = this.getChildByName("hero") as MovieClip;
         var body:b2Body = child.body as b2Body;
       
         if (isLeft)
         {
            body.WakeUp();
            body.m_linearVelocity.x -= 0.5;
         }
         if (isRight)
         {
            body.WakeUp();
            body.m_linearVelocity.x += 0.5;
         }
         if (isUp)
         {
            if (body.m_linearVelocity.y > -1)
            {
               body.WakeUp();
               body.ApplyImpulse(new b2Vec2(0, -30), body.GetWorldCenter());
            }
         }

    }

  • 相关阅读:
    深入浅出AQS之组件概览
    深入浅出AQS之条件队列
    深入浅出AQS之共享锁模式
    深入浅出AQS之独占锁模式
    Android中RecyclerView用法,一步一步教你如何使用RecyclerView以及带你走过编码中可能会出现的坑~
    检测jquery是否正确引入
    对于使用JDBC连接mysql数据时The server time zone value '¤¤°ê¼Ð·Ç®É¶¡'...的异常问题解决。
    MYSQL8.0以上版本ROOT密码报错及修改
    MYSQL安装教程
    linux centos安装nginx1.7.4
  • 原文地址:https://www.cnblogs.com/ywxgod/p/1699991.html
Copyright © 2011-2022 走看看