一,鼠标拖拽刚体
想轻松拖动游戏里的道具?赶紧试试luvylegend.js这个神奇辅助器!首先在LSprite函数中添个setBodyMouseJoint(True),搞定后,鼠标就可以拖动物品啦~然后回头看看上次加的那段代码,把这些和拖拽有关的部分加进去就行。现在,操作是不是变得简单多了!
javascript 先开启bodyMouseJoint功能,启动摇摆!function add(){ var rand = Math.random(); if(rand < 0.33){ cLayer = new LSprite(); cLayer.x = 50 + Math.random()*700; cLayer.y = 50; backLayer.addChild(cLayer); bitmap = new LBitmap(new LBitmapData(imglist["bird1"])); cLayer.addChild(bitmap); cLayer.addBodyCircle(bitmap.getWidth()*0.5,bitmap.getHeight()*0.5,bitmap.getWidth()*0.5,1,.5,.4,.5); cLayer.setBodyMouseJoint(true); }else if(rand < 0.66){ cLayer = new LSprite(); backLayer.addChild(cLayer); bitmap = new LBitmap(new LBitmapData(imglist["bird2"])); cLayer.addChild(bitmap); var shapeArray = [ [[0,54],[27,0],[54,54]] ]; cLayer.addBodyVertices(shapeArray,27,27,1,.5,.4,.5); cLayer.box2dBody.SetPosition(new LGlobal.box2d.b2Vec2((50 + Math.random()*700)/LGlobal.box2d.drawScale,50/LGlobal.box2d.drawScale)); cLayer.setBodyMouseJoint(true); }else{ cLayer = new LSprite(); cLayer.x = 50 + Math.random()*700; cLayer.y = 50; backLayer.addChild(cLayer); bitmap = new LBitmap(new LBitmapData(imglist["stage01"])); cLayer.addChild(bitmap); cLayer.addBodyPolygon(bitmap.getWidth(),bitmap.getHeight(),1,5,.4,.2); } }快来看!我们把鸟儿玩出花样,还有可以用鼠标随意拖动。快来试试这新鲜玩意儿!
二,碰撞检测
为了实现碰撞检测,我们可以使用以下代码来添加碰撞检测事件:
LGlobal.box2d.setEvent(LEvent.BEGIN_CONTACT,beginContact);function onCollision(event){
别偷懒,记得提醒大家,“撞到头了”+说哪位A哥们出事了+加上哪个倒霉的B哥们也不例外。
}
function beginContact(contact){ if(contact.GetFixtureA().GetBody().GetUserData().name == "bird" && contact.GetFixtureB().GetBody().GetUserData().name == "bird"){ trace("bird and bird"); } trace("bird and other"); };只要你碰到它,那就算成”开始接触”了,然后就会立刻启动”onCollision”这个功能。
这个代码就能搞定物体碰撞的问题,不论它们动还是不动。真的,你可以去试试,lufylegend.js里就有个功能能告诉我们哪个部分碰到了。
看,这个办法能让小鸟避免一头撞到另一只。两只小鸟互相碰撞时,屏幕上会出现“鸟和鸟”;遇到其他圆形物体或者实物的话,那就是“鸟和其他”咯~
三,刚体之间的各种链接
来聊聊神奇的运动物体连接!虽然现在Lufy Legend.js还没这个功能,但以后肯定有的。那咱先学学怎么自己搞这种连结呗。
1.距离链接(b2DistanceJointDef)
这部《B2DistanceJointDef》就像个法宝,它可以帮你搞定两个硬邦邦东西间的间距。怎样使用?听我细细给你说来:
直接创建个b2DistanceJointDef就好!
距离线连接搞定,把物体A和物体B的世界中心放进去就行。
function add(){ cLayer = new LSprite(); cLayer.name = "bird"; cLayer.x = 50 + Math.random()*700; cLayer.y = 50; backLayer.addChild(cLayer); bitmap = new LBitmap(new LBitmapData(imglist["bird1"])); cLayer.addChild(bitmap); cLayer.addBodyCircle(bitmap.getWidth()*0.5,bitmap.getHeight()*0.5,bitmap.getWidth()*0.5,1,.5,.4,.5); cLayer.setBodyMouseJoint(true); return cLayer; } var bird1 = add(); var bird2 = add(); var distanceJointDef = new LGlobal.box2d.b2DistanceJointDef(); distanceJointDef.Initialize(bird1.box2dBody, bird2.box2dBody, bird1.box2dBody.GetWorldCenter(), bird2.box2dBody.GetWorldCenter()); LGlobal.box2d.world.CreateJoint(distanceJointDef);赶紧搞个链接然后把距离调节好。
2.旋转链接(b2RevoluteJointDef)
来看看那个叫B2RevoluteJointDef的好东西!它能让两铁球转得跟齿轮一样,顺着穿越它们中间的那根轴子。想知道怎么用,那还不容易,就那么回事儿!
我们来新建一个叫做revoluteJoint的类!
快来看看这厉害的“革命组合”旋转变压器(Revolutionate Joint)能咋样,它就像个超级连线器,把A和B两人的身体中心部分接在一起了!
好了,干活儿,创建一个合规的关节joint出来!
var bird = new LSprite(); bird.name = "bird"; bird.x = 200; bird.y = 200; backLayer.addChild(bird); bitmap = new LBitmap(new LBitmapData(imglist["bird1"])); bird.addChild(bitmap); bird.addBodyCircle(bitmap.getWidth()*0.5,bitmap.getHeight()*0.5,bitmap.getWidth()*0.5,0); var pig = new LSprite(); pig.name = "pig"; pig.x = 200; pig.y = 150; backLayer.addChild(pig); bitmap = new LBitmap(new LBitmapData(imglist["pig2"])); pig.addChild(bitmap); pig.addBodyCircle(bitmap.getWidth()*0.5,bitmap.getHeight()*0.5,bitmap.getWidth()*0.5,1,.5,.4,.5); var revoluteJointDef = new LGlobal.box2d.b2RevoluteJointDef(); revoluteJointDef .Initialize(pig.box2dBody, bird.box2dBody, bird.box2dBody.GetWorldCenter()); LGlobal.box2d.world.CreateJoint(revoluteJointDef );3.滑轮链接(b2PulleyJointDef)
你知道吗?B2PulleyJointDef这个东西其实就是个滑轮,用着特简单。
新建个b2PulleyJointDef类来弄个滑轮关节!
动手!先把线穿过滑轮,然后把两个东西挂到两边的钉子上去。记得要用地上的和东西表面的钉子稳住它们哦~别忘了调整好它们的大小比例,重要着!
新弄个滑轮中间接一下咯。
除了前面我们说过的连接方法,实际上还有好多其他的,比如b2GearJoint、b2PrismaticJoint、b2LineJoint和b2WeldJoint之类的。下次这些都会直接合并进lufylegend.js库内了,到时我会详细解释给你们听的。感兴趣的话,你们就自己去搜一下相关内容研究看看。
var bird = new LSprite(); bird.name = "bird"; bird.x = 200; bird.y = 200; backLayer.addChild(bird); bitmap = new LBitmap(new LBitmapData(imglist["bird1"])); bird.addChild(bitmap); bird.addBodyCircle(bitmap.getWidth()*0.5,bitmap.getHeight()*0.5,bitmap.getWidth()*0.5,1,.5,.4,.5); bird.setBodyMouseJoint(true); var bird01 = new LSprite(); bird01.name = "bird"; bird01.x = 400; bird01.y = 150; backLayer.addChild(bird01); bitmap = new LBitmap(new LBitmapData(imglist["bird1"])); bird01.addChild(bitmap); bird01.addBodyCircle(bitmap.getWidth()*0.5,bitmap.getHeight()*0.5,bitmap.getWidth()*0.5,1,.5,.4,.5); bird01.setBodyMouseJoint(true); var anchor1 = bird.box2dBody.GetWorldCenter(); var anchor2 = bird01.box2dBody.GetWorldCenter(); var groundAnchor1 = new LGlobal.box2d.b2Vec2(anchor1.x, anchor1.y - (100 / LGlobal.box2d.drawScale)); var groundAnchor2 = new LGlobal.box2d.b2Vec2(anchor2.x, anchor2.y - (100 / LGlobal.box2d.drawScale)); var ratio = 1.0; var pulleyJointDef = new LGlobal.box2d.b2PulleyJointDef(); pulleyJointDef.Initialize(bird.box2dBody, bird01.box2dBody, groundAnchor1, groundAnchor2, anchor1, anchor2, ratio); pulleyJointDef.maxLengthA = 300 / LGlobal.box2d.drawScale; pulleyJointDef.maxLengthB = 300 / LGlobal.box2d.drawScale; LGlobal.box2d.world.CreateJoint(pulleyJointDef);最后给出这两次内容的所有源代码供大家参考。
记住我说的那个代码,就是给你看看的那种。想在自己电脑上用?那就下个luftylegend.js还有box2dweb,设置好了就能顺利搞定!
原文链接:https://www.icz.com/technicalinformation/web/2024/04/14726.html,转载请注明出处~~~
评论0