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






// insert your global variables here object renderer number width number height list points list stones object snake number count=0 number npoint=10 number snakew=140 number segmentw number counter=0 number currentAngle number desiredAngle // this method is called on scene start // if you have multiples files per scene, only one of this method is allowed method void OnInit () { height=500 app.setLogicalHeight(height) width=app.getLogicalWidth() log("width:"+width) renderer=app.createRenderer() object back=renderer.createSprite("background","snake-demo/Desert_1600x1000.png",width/2,480,1600,1000) segmentw=snakew/npoint for i=0 to npoint-1 // create points for the rope sprite object point=createPoint(i*segmentw,0) point.i=i points.push(point) next snake=renderer.createRopeSprite("snake","snake-demo/snake_573x61.png",points,width*.4,height*0.65,snakew,25) // same as normal sprite with list of points as extra parameter object stone1=renderer.createSprite("background","snake-demo/rocher_200x175.png",90,height*0.8,40,40) object stone2=renderer.createSprite("background","snake-demo/rocher_200x175.png",width-80,height*0.6,40,40) object stone3=renderer.createSprite("background","snake-demo/rocher_200x175.png",220,height*0.66,40,40) object stone4=renderer.createSprite("background","snake-demo/rocher_200x175.png",width-250,height*0.82,40,40) stones.push(stone1) stones.push(stone2) stones.push(stone3) stones.push(stone4) currentAngle=0 desiredAngle=0 snake.setAngle(currentAngle) snake.vx=1 // initial speed of snake snake.vy=0 } // this method is called each frame // if you have multiples files per scene, only one of this method is allowed method void OnUpdate() { counter=counter+0.1 foreach object point in points // apply a math function to each point so snake oscillate point.y=math.sin((point.i*0.5)+counter)*10 point.x=point.i*segmentw+math.cos((point.i*0.3)+counter)*npoint next number x=snake.getPositionX() number y=snake.getPositionY() number v1x=snake.vx number v1y=snake.vy if desiredAngle=currentAngle snake.setPosition(x+snake.vx,y+snake.vy) x=snake.getPositionX() y=snake.getPositionY() // check collision with stones object stone=renderer.collideList(snake,"rectangle",stones,"rectangle",true,false) // check collisions between snake, of type rectangle, with the stones of type rectangle, prevent from overlaping but not bouncing if stone!=null switch stone.side // according to side of collision modify speed vx and vy of snake case "left" case "right" snake.vx=0 if ywidth-50 // check bounds of screen and modify speed vx and vy of snake snake.vx=0 if yheight-50 snake.vy=0 if xcurrentAngle currentAngle=currentAngle+0.1 else currentAngle=currentAngle-0.1 endif if yheight-55 y=y-1 endif if x>width-55 x=x-1 endif if x<55 x=x+1 endif snake.setPosition(x,y) if math.abs(currentAngle-desiredAngle)<=0.2 currentAngle=desiredAngle endif // log("cur:"+currentAngle+" des:"+desiredAngle+" dif:"+math.abs(currentAngle-desiredAngle)+" "+math.PI) snake.setAngle(currentAngle) endif }

Technologies