- 注册时间
- 2013-2-13
- 最后登录
- 2023-10-13
- 阅读权限
- 200
- 积分
- 9304
- 精华
- 1
- 帖子
- 2411
|
发表于 2013-4-14 23:03:52
|显示全部楼层
今天有位朋友问我,addBodyPolygon 之后怎么用键盘控制物体,我在这里简单说一下,其他朋友如果也有同样的问题的话,可以作为参考。
其实在box2d中也没有什么特别的,要控制一个刚体的移动,只需要重新设置它的坐标即可,
那么一个LSprite对象在addBodyPolygon变为刚体之后,被添加的刚体的b2Body可以通过box2dBody属性来得到,
假设这个LSprite对象是A,
那么,这个刚体在物理世界里的坐标为A.box2dBody.GetPosition(),相应的x坐标为A.box2dBody.GetPosition().x,y坐标为A.box2dBody.GetPosition().y。
重新设定一个刚体的坐标的时候,使用SetPosition函数。
比如,我要让一个刚体想右移动一个单位,那么只需要下面- A.box2dBody.SetPosition(new LGlobal.box2d.b2Vec2(A.box2dBody.GetPosition().x+1,A.box2dBody.GetPosition().y));
复制代码 我这里所说的一个单位,是指box2d世界中的一个单位,并不是像素值。
使用SetPosition设定刚体位置的时候,参数也是box2d世界中的单位。
而box2d世界中的单位和像素值之间的换算单位是LGlobal.box2d.drawScale,也就是
像素坐标=box2d中的坐标/LGlobal.box2d.drawScale;
当然,重新设定了坐标之后,要唤醒刚体- A.box2dBody.SetAwake(true);
复制代码 下面是一个使用鼠标控制刚体移动的例子,URL为
http://lufylegend.com/demo/test/2.html
点击[←]键或者[→]键来改变画面中的物体的位置。
代码如下:- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>鼠标控制刚体移动</title>
- <script type="text/javascript" src="http://lufylegend.com/js/Box2dWeb-2.1.a.3.min.js"></script>
- <script type="text/javascript" src="http://lufylegend.com/js/lufylegend-1.5.1.min.js"></script>
- </head>
- <body>
- <div id="mylegend">loading……</div>
- <script type="text/javascript">
- init(10,"mylegend",800,480,main);
- var backLayer,wallLayer,ctrlBox;
- var KEY = {LEFT:37,RIGHT:39};
- var imgData = [
- {name:"face",path:"http://lufylegend.com/images/html5/lufylegend/ps/face.jpg"}
- ];
- function main(){
- LLoadManage.load(imgData, null, gameInit);
- }
- function gameInit(result){
- LGlobal.box2d = new LBox2d();
- backLayer = new LSprite();
- addChild(backLayer);
- //添加一个框子
- wallLayer = new LSprite();
- wallLayer.x = 400;
- wallLayer.y = 0;
- backLayer.addChild(wallLayer);
- wallLayer.addBodyPolygon(800,10,0);
- wallLayer = new LSprite();
- wallLayer.x = 0;
- wallLayer.y = 240;
- backLayer.addChild(wallLayer);
- wallLayer.addBodyPolygon(10,480,0);
- wallLayer = new LSprite();
- wallLayer.x = 400;
- wallLayer.y = 480;
- backLayer.addChild(wallLayer);
- wallLayer.addBodyPolygon(800,10,0);
- wallLayer = new LSprite();
- wallLayer.x = 800;
- wallLayer.y = 240;
- backLayer.addChild(wallLayer);
- wallLayer.addBodyPolygon(10,480,0);
- backLayer.graphics.drawRect(1,"#ffffff",[0, 0, 800, 5],true,"#000000");
- backLayer.graphics.drawRect(1,"#ffffff",[0, 475, 800, 5],true,"#000000");
- backLayer.graphics.drawRect(1,"#ffffff",[0, 0, 5, 480],true,"#000000");
- backLayer.graphics.drawRect(1,"#ffffff",[795,0, 5, 480],true,"#000000");
-
- //添加刚体,键盘控制的就是这个刚体
- ctrlBox = new LSprite();
- var faceBitmap = new LBitmap(new LBitmapData(result["face"]));
- ctrlBox.addChild(faceBitmap);
- ctrlBox.x = 250;
- ctrlBox.y = 200;
- ctrlBox.addBodyPolygon(faceBitmap.getWidth(),faceBitmap.getHeight(), 1, 2, .4, .2);
- backLayer.addChild(ctrlBox);
- //添加其他刚体
- for (var i = 0; i < 5; i++) {
- var box = new LSprite();
- faceBitmap = new LBitmap(new LBitmapData(result["face"],130*Math.random(),130*Math.random(),100,100));
- box.addChild(faceBitmap);
- box.x = 50 + 700*Math.random();
- box.y = 100*Math.random();
- box.addBodyPolygon(faceBitmap.getWidth(),faceBitmap.getHeight(), 1, 2, .4, .2);
- backLayer.addChild(box);
- }
- LEvent.addEventListener(LGlobal.window,LKeyboardEvent.KEY_UP,onkeyup);
- }
- function onkeyup(e){
- if(e.keyCode == KEY.LEFT){
- ctrlBox.box2dBody.SetPosition(new LGlobal.box2d.b2Vec2(ctrlBox.box2dBody.GetPosition().x-1,ctrlBox.box2dBody.GetPosition().y));
- }else if(e.keyCode == KEY.RIGHT){
- ctrlBox.box2dBody.SetPosition(new LGlobal.box2d.b2Vec2(ctrlBox.box2dBody.GetPosition().x+1,ctrlBox.box2dBody.GetPosition().y));
- }
- //唤醒刚体
- ctrlBox.box2dBody.SetAwake(true);
- }
- </script>
- </body>
- </html>
复制代码 问题解决了,如果大家有问题的话,可以留言咱们继续讨论。
|
|