Skip to main content

swipe event using jquery and easeljs with a cool example

swipe events are most popular events for the android and ios devices.

so here i come with a example where i am using jquery and easeljs.

what you need to run this example 
============================================================================

a images floder which contains two pictures one for background and another of a moving  object
==========================================================================

files required (jquery and wipetouch files)
--------------------------------------------------



download easeljs (coolest javascript library )


copy and paste the entrie code in html file be sure your directory structure is like mine or you can create you own 
=======================================================================
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
//lib floder contains two files
<script src="lib/jquery-1.7.2.min.js"></script>
<script src="lib/jquery.wipetouch.js"></script>

<script type="text/javascript" src="CreateJS-EaselJS/lib/easeljs-0.5.0.min.js"></script>
<script type="text/javascript" src="CreateJS-PreloadJS/lib/preloadjs-0.2.0.min.js"></script>
<script>
//variables
var totalLoaded=0;
var speed=0;

//functions
function init()
{

canvas=document.createElement('canvas');
canvas.width=1280;
canvas.height=768;
document.body.appendChild(canvas);
//game stage
createGamestage();

}


function createGamestage()
{

stage = new createjs.Stage(canvas);
stage.autoClear = false;
//works for both either pc or tab ios and android devices
createjs.Touch.enable(stage);
stage.snapToPixelEnabled =true;
manifest = [
{src:"images/background.png", id:"bg"},
{src:"images/reindeer.png", id:"reindeer"},


];
//preload all images and sound
preloader = new createjs.PreloadJS();
    preloader.onProgress = handleProgress;
    preloader.onComplete = handleComplete;
    preloader.onFileLoad = handleFileLoad;
    preloader.loadManifest(manifest);
//frame rate of bee game
addEnterFrame();
}
function addEnterFrame()
{
createjs.Ticker.setFPS(60);
createjs.Ticker.useRAF = true;
createjs.Ticker.addListener(stage);
}
function handleFileLoad(event) {
   
switch(event.type)
{
case createjs.PreloadJS.IMAGE:
//loading images
if(event.id=="reindeer")
{
var img = new Image();
  img.src = event.src;
      img.onload = handleLoadComplete;
var reindeerSprite=new createjs.SpriteSheet({
    // image to use
    images: [img],
    // width, height & registration point of each sprite
  frames: {width: 93, height: 120, regX: 46.5, regY:60},
   animations: {  
         run: [0, 4, "run",4]
   }
});
//adding a flipped animation or you can use xsacale property

window[event.id]  = new createjs.BitmapAnimation(reindeerSprite);
createjs.SpriteSheetUtils.addFlippedFrames(reindeerSprite, true, false, false)
}
else
{
var img = new Image();
    img.src = event.src;
      img.onload = handleLoadComplete;
    window[event.id] = new createjs.Bitmap(img);
}
break;
}
}

function handleProgress(event)
{
//use event.loaded to get the percentage of the loading

}

function handleComplete(event) {
       
}
function handleLoadComplete(event)
{
totalLoaded++;
if(manifest.length==totalLoaded)
{
//when all files loaded game or application is ready to start
 gameBegin();
}
 }
 function gameBegin()
 {
//adding  background to my game
stage.addChild(bg);
//adding objects
addreindeer();
addstatutsTest();
 }
function addreindeer()
{
stage.addChild(reindeer);
reindeer.x=400;
reindeer.y=600;
reindeer.gotoAndStop(0);
//adding swipe event using juery
$(canvas).wipetouch(
{
/*
read the lib/jquery.wipetouch for more details how wipe events works
wipeMove=swipe in any direction
wipeUp=swipe in up dierection
wipeDown=swipe in down direction
wipeLeft=swipe in left direction
wipeRight=swipe in right direction
*/

//wipeMove: function(result) { wipeStatus(result); },

wipeRight: function(result) { wipeStatus( result,"right"); },

wipeLeft: function(result) { wipeStatus( result,"left"); },

wipeUp: function(result) { wipeStatus( result,"up"); },

//wipeDown: function(result) { wipeStatus( result); },

});
reindeer.onTick=moveLeftRight;
}
function wipeStatus(result,direction)
{
statusTxt.text="Speed is"+" "+result.speed+" "+"Direction of Swipe is"+" "+direction;
if(direction=="right")
{
speed=result.speed;
reindeer.gotoAndPlay("run");
}
else if(direction=="left")
{
speed=-result.speed;
reindeer.gotoAndPlay("run_h");
}
else if(direction=="up")
{
jumpdeer();
}
 
}
function jumpdeer()
{


}
function addstatutsTest()
{
statusTxt = new createjs.Text("Swipe in left or right direction to move", "40px Verdana", "#000");
statusTxt.x = 100;
statusTxt.y = 100;
stage.addChild(statusTxt);
}
function moveLeftRight()
{
if(reindeer.x>900)
{
statusTxt.text="Swipe in left or right direction to move";
reindeer.gotoAndStop(0);
reindeer.x=900;
speed=0;
}
else if(reindeer.x<100)
{
statusTxt.text="Swipe in left or right direction to move";
reindeer.gotoAndStop(0);
reindeer.x=100;
speed=0;
}
reindeer.x+=speed;
}
//random
function random_number(min,max) {

    return (Math.round((max-min) * Math.random() + min));
}

</script>
<style type="text/css">
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
</style>
</head>
<body onload="init()">
</body>
</html>



Comments

Popular posts from this blog

Creating a word Scramble game where you can drag and drop the word and create a complete word. (drag and drop word Scramble game).

Creating a word game using  phaser game engine which is a javascript based gaming engine  where u can drag and drop world like Word Scramble Game and complete a given word. your index.html file look like below < html > < head > < script src = "src/phaser.min.js" ></ script > < script src = "src/wordGame.js" ></ script > < script src = "src/main.js" ></ script > </ head > < body > </ body > </ html > 1.    <script src="src/phaser.min.js"></script> required to run the  phaser game engine. 2. rest of the two files deals with game logics      <script src="src/wordGame.js"></script>       <script src="src/main.js"></script> you can download whole project from https://github.com/manishchauhan/wordgame/tree/master just copy and paste the project in your local server and run ...

A simple binary search tree with generic approach tree can store any type of data. (DFS and BFS)

 1. a binary search tree in c++ using a generic approach with both BFS and DFS approach    working tree    https://www.onlinegdb.com/edit/HkuQLdA-w code=> some properties of binary search tree-> 1. binary search tree is not always a balanced tree.  2. Inorder, traversing gives you sorted data.  3.  best is o(log n) and worst is O(n).  4. left node data is always less than root data and right node data always be greater than root data.  5. red-black tree STL map is a balanced binary search tree. *******************************************************************************/ #include <stdio.h> #include<iostream> #include<queue> using namespace std; template < typename T > class Node {   //can store any type of data private:   //genric data or store any type of data   T data;   //reference of left pointer   Node *left = nullptr;   //reference of right pointer   Node *rig...

starting with three.js and react with a very simple example

 1. three.js is undoubtedly the best library to create interactive content for the web in 3d. link https://threejs.org/ a working sample of three with react can be found below https://stackblitz.com/edit/react-7n5qf9?file=src%2FApp.js why choose threejs 1. lightweight  2.fast 3.big community  https://discourse.threejs.org/ 4. even you can use unity or unreal to publish html5 content but i don't think that would be acceptable in many cases. 5. open-source project. i created a running sample with React  https://stackblitz.com/edit/react-7n5qf9?file=src%2FApp.js