Basketball beating game based on HTML5 and jquery

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.