Today, we share a game of basketball jumping based on HTML5 and jquery. This example is similar to the previously shared HTML5 gravity-induced ball crash animation. Use the mouse to drag the basketball, the basketball bounce on the page, you can see the effect in the demo. As follows:
Online preview Source Download
The implemented code.
This example is mainly JS code. JS needs to refer to both the jquery and phaser.js libraries. The JS code on the page is as follows:
(function () { varW =window.innerwidth; varh =Window.innerheight; varGame =NewPhaser.game (W, H, Phaser.canvas, ", {preload:preload, create:create, update:update}); varTotalballs = 8; varballs, Ballstartx, Ballstarty, Ballendx, Ballendy; functionrandom (min, max) {returngame.rnd.integerInRange (min, max); } functionpreload () {Game.load.image (' Basketball ', ' https://s3-us-west-2.amazonaws.com/s.cdpn.io/74196/basketball.png '); } functionCreate () {Game.physics.startSystem (Phaser.Physics.ARCADE); Game.scale.scaleMode=Phaser.ScaleManager.RESIZE; Balls=Game.add.group (); for(vari = 0; i < totalballs; i++) {setTimeout (function () { varBall = balls.create (random (0, Game.world.width), -100, ' basketball '); Game.physics.arcade.enable (ball); Ball.scale.setTo (0.2, 0.2); Ball.body.velocity.x= Random (-50, 50); Ball.body.gravity.y= 1000; Ball.body.bounce.y= 0.5; Ball.body.bounce.x= 0.5; Ball.body.collideWorldBounds=true; Ball.inputenabled=true; Ball.input.enableDrag (true); Ball.input.start (0,true); Ball.events.onDragStart.add (grab, ball); Ball.events.onDragStop.add (toss, ball); }, 200 *i); } } functionUpdate () { for(vari = 0; i < balls.length; i++) { varThisball =Balls.getat (i); varVX =thisball.body.velocity.x; varVY =thisball.body.velocity.y; if(ThisBall.body.bottom = = =game.world.bounds.bottom) {if(thisball.body.velocity.x > 0) {thisball.body.velocity.x= VX-1; } Else if(Thisball.body.velocity.x < 0) {thisball.body.velocity.x= VX + 1; } } } } functiongrab () {Ballstartx= This. body.position.x; Ballstarty= This. body.position.y; This. body.moves =false; This. Body.velocity.setTo (0, 0); This. body.allowgravity =false; } functionToss () {ballendx= This. body.position.x; Ballendy= This. body.position.y; This. body.moves =true; This. body.allowgravity =true; varVX = (ballendx-ballstartx) * 10; varVY = (ballendy-ballstarty) * 10; This. Body.velocity.setTo (VX, VY); } } ());
via:http://www.w2bc.com/article/14341
Basketball beating game based on HTML5 and jquery