This demo shows how to create physical bodies, apply forces to them, set transformations like scale, color
It uses collision detection via the PhysicsJS Engine and Howler.js for sound






object world object renderer number width number height number count boolean soundon object zik object button object textbutton method void OnInit() { log("launching new bubbles scene") soundon=app.getGlobal("soundon") if soundon=null soundon=true endif count=0 width=1000 app.setLogicalWidth(width) height=app.getLogicalHeight() world=app.createWorld() renderer=app.createRenderer() world.addRenderer(renderer) world.setBounds(0, 0, width, height) world.addBehavior("body-impulse-response") world.addBehavior("constant-acceleration") world.addBehavior("collision-detection") number r=rnd(255) number b=rnd(255) object back=renderer.createSprite("background","bubbles-demo/decor_pr_bulle_1600x1000.png",width/2,height/2,1600,1000) for i = 1 to 200 object ball = world.createBody("circle", "ball", "dynamic","bubbles-demo/bulle_256x256.png", rnd(width), rnd(height), .02 - rnd(.04), .02 - rnd(.04), 32, 32) ball.setTint(rgb(r,rnd(255),b)) ball.setAlpha(0.5+rnd(0.5)) next if soundon zik=app.playSound(["bubbles-demo/Akiam Dance.mp3"],true,0.3) soundon=true button=renderer.drawBitmapText("Sound Off","BMFonts/HeadLineA_1.fnt",50,50,200,50,0xffffff,1) button.makeTouchable() else button=renderer.drawBitmapText("Sound On","BMFonts/HeadLineA_1.fnt",50,50,200,50,0xffffff,1) button.makeTouchable() endif } method void setToogleMusic() { if soundon zik.stopPlaying() soundon=false button.setText("Sound On ") else zik.startPlaying() soundon=true button.setText("Sound Off ") endif app.setGlobal("soundon",soundon) } method void OnCollision(list collisions) { object c = collisions[rndInt(collisions.length-1)] if (c.bodyA.label == "ball") if (c.bodyA.getScale() > 0.35) c.bodyA.setScale(c.bodyA.getScale()*0.95); c.bodyA.setMass(c.bodyA.getMass()*0.95) endif endif if (c.bodyB.label == "ball") if (c.bodyB.getScale() < 4) c.bodyB.setScale(c.bodyB.getScale()*1.08) c.bodyA.setMass(c.bodyA.getMass()*1.08) else if soundon app.playSound(["bubbles-demo/1.ogg"],false,0.8) endif number color=c.bodyB.getTint() world.removeBody(c.bodyB) renderer.particleBurst("bubbles-demo/goutelette_30x50.png",c.bodyB.getPositionX(),c.bodyB.getPositionY(),150,0.2,color) count=count+1 endif endif } method void OnUpdate() { if button.state="down" setToogleMusic() endif list bodies = world.findBodies("ball") for i=1 to 10 object body=bodies[rndInt(10 - 1) ] if body!=null body.applyForce(.0008-rnd(.00017),.05+rnd(.04)) endif next if bodies.length<50 app.loadScene("Bubbles") endif }