LimeJS Demo学习2 box2d使用

来源:互联网 发布:linux卸载scim 编辑:程序博客网 时间:2024/05/16 15:49
在LimeJS中使用box2d其实是将box2d中实体的位置来设置Lime中的形状实体
goog.provide('test.box2d');goog.require('box2d.BodyDef');goog.require('box2d.BoxDef');goog.require('box2d.CircleDef');goog.require('box2d.CircleShape');goog.require('box2d.PolyDef');goog.require('box2d.Vec2');goog.require('box2d.World');goog.require('lime');goog.require('lime.Circle');goog.require('lime.CoverNode');goog.require('lime.Director');goog.require('lime.Layer');goog.require('lime.Scene');goog.require('lime.fill.LinearGradient');test.WIDTH = 600;test.HEIGHT = 400;test.start = function() {    /*    There is no box2d integration in LimeJS yet. This file only    shows that box2d is in correct path.    */    // limeJS并没有集成box2d//directortest.director = new lime.Director(document.body, test.WIDTH, test.HEIGHT);    //创建时间线test.director.makeMobileWebAppCapable();      var gamescene = new lime.Scene();  //创建视图var layer = new lime.Layer; //创建图层layer.setPosition(100, 0);  //图层位置为(100,0)gamescene.appendChild(layer); //将图层添加到视图// set active scenetest.director.replaceScene(gamescene);   //替换画布/*    // new API proposalvar physics = new lime.Physics(layer).setGravity(0,10);physics.worldcircle.enablePhysics(physics).setAngularDamping(.1).setDesity(.5);circle.enablePhysics(physics,bodyDef);circle.getPhysicsBody();var phdata = new lime.PhysicsData(lime.ASSETS.filename);var circle = phdata.createShape('icecream',physics).setPosition(100,0).setFill();*/var gravity = new box2d.Vec2(0, 100);            //创建重力系数var bounds = new box2d.AABB();                  //创建边界bounds.minVertex.Set(-test.WIDTH, -test.HEIGHT);         //设置边界bounds.maxVertex.Set(2*test.WIDTH,2*test.HEIGHT);      var world = new box2d.World(bounds, gravity, false);    //创建游戏世界    var circle = (new lime.Circle)                //创建球形        .setFill(new lime.fill.LinearGradient().addColorStop(0.49,200,0,0).addColorStop(.5,0,0,250))       //线性颜色填充    .setSize(8, 8);         //大小    layer.appendChild(circle); //将球形添加到图层var cbodyDef = new box2d.BodyDef;      //定义box2d游戏引擎中的实体cbodyDef.position.Set(200, 0);                //实体位置设定    cbodyDef.angularDamping = .001;          //角速度衰减值,由于空气摩擦等var circleDef = new box2d.CircleDef;    //球形circleDef.radius = 15;         //半径circleDef.density = 1;//密度circleDef.restitution =0.8;      //碰撞恢复,碰撞之后恢复0.8的速度值circleDef.friction = 1;             //设置摩擦系数  cbodyDef.AddShape(circleDef);       //给实体添加形状var circle_body = world.CreateBody(cbodyDef);     //创建实体    var ground = new box2d.PolyDef;               // 创建墙壁ground.restitution = .9                     //墙壁的碰撞恢复ground.density = 0;//墙壁密度,因为密度为0,所以是不会动的ground.friction = 1;                      //墙壁的摩擦系数//ground.extents.Set(30, 10);//box version    ground.SetVertices([[-30,-5],[30,-10],[30,10],[-30,10]]); // actually not a box    //设置墙壁顶点    var gbodyDef = new box2d.BodyDef;    //定义墙壁实体    gbodyDef.position.Set(220, 300);      //设置墙壁实体位置    gbodyDef.AddShape(ground);             //给实体添加形状定义    var ground_body = world.CreateBody(gbodyDef);   //创建墙壁实体    var box = (new lime.Sprite)         //创建箱子形状        .setFill(0,100,0)    .setSize(60, 20);    layer.appendChild(box);         //将箱子添加到图层中    lime.scheduleManager.schedule(function(dt) {    //call function  every frame                   world.Step(dt / 1000, 3);              //刷新世界,这个函数调用与api中有点差别,api中有三个参数,而这里只有两个参数。dt参数告诉计算机计算多少秒之后的世界,第二个参数为迭代次数,影响精度,值越大精度越高,但是速度会变慢。        var pos = circle_body.GetCenterPosition().clone();    //获得球实体的位置        var rot = circle_body.GetRotation();            //获得球体的旋转角度        circle.setRotation(-rot/Math.PI*180);   //设置circle形状的角度        circle.setPosition(pos);           //设置circle形状的位置        var pos = ground_body.GetCenterPosition().clone();     //   获得墙壁位置        box.setPosition(pos);  //设置盒子的位置    },this);              };goog.exportSymbol('test.start', test.start);

0 0
原创粉丝点击