Skip to main content

inheritance using EaselJS: a example of bouncing balls create with easejs

its really easy to extend a class in easeljs here is a example with source code
ball class extended from the container class with its serval own properties such as vx,vy and gravity
 

=====================================================

for new guys copy this code in a javascript file rename that file as ball.js
 

//ball class

(function (window) {
    function ball(icon,x,y) {
        this.initialize(icon,x,y);
    }
    //Inheritance from Container
    ball.prototype = new createjs.Container(); 
ball.prototype.icon=null;
ball.prototype.vx=2;
ball.prototype.vy=0;
ball.prototype.gravity = .4;
    ball.prototype.initialize = function (icon) {
  this.icon=icon;
  this.addChild(icon);
    }
     ball.prototype.onTick = function () {
               // apply gravity
this.vy += this.gravity;
// move ball
this.x+=this.vx;
this.y += this.vy;
if (this.x>950) {
this.x = 950;
this.vx = -this.vx;
}
// hit left wall?
if (this.x <= 10) {
this.x = 10;
this.vx = -this.vx;
}
if (this.y >700) {
this.y =700;
this.vy = -this.vy;

}
    }
    window.ball= ball;
} (window));

copy and paste this source code in a blank html document
--------------------------------------------------------------------

//main file or index file
<!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>

<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 src="ball.js"></script>
<script>
//variables
var totalLoaded=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;
stage.mouseEventsEnabled = true;
stage.snapToPixelEnabled =true;
manifest = [
                               // need background and  myball images inside images floder
{src:"images/bg.png", id:"bg"},
{src:"images/ball.png", id:"myball"},
];
//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:
//image loaded
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)
{
 gameBegin();
}
 }
 function gameBegin()
 {
//add background
stage.addChild(bg);
addfloor();
 }
function addfloor()
{
stage.onClick=addball;

}
function addball()
{
//creating a clone of ball object
var clone=myball.clone();
clone.regX=clone.image.width/2;
clone.regY=clone.image.height/2;
//passing that clone to tball function
var tball=new ball(clone);
tball.x=stage.mouseX;
tball.y=stage.mouseY;
tball.vx=random_number(2,10);
stage.addChild(tball);

}
//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

Better Memory management with PixiJS or How to manage cpu and cpu memory in PixiJS.

PixiJS is my favorite framework when i am looking for a web games specially for mobile or desktop  PixiJS is fast blazing fast and you can get a decent FPS even on older device.   so here is my optimization techniques for PixiJs 1. manage your sprites in a better way use spritesheet to reduce the draw calls create big sprite sheet which contain multiple sprites can be draw in gpu with a single draw call. use TexturePacker  https://www.codeandweb.com/texturepacker  best tool when its comes to spritesheet 2. for floating point calculation round off calculation for example let  speed = 0.75 ; let  position = 100 ; console . log ( Math . round ( speed * position )) 3. don't create very big canvas when u need a big canvas size game just try to create a small canvas and translate it. 4. its very important one managing TextureCache in memory you can get all TextureCache list by using  Object.entries(PIXI.utils.TextureCache); so even you use ap...

adding particles Effect in pixijs using https://pixijs.io/pixi-particles-editor/

adding particle in pixijs is very easy using the below tool more information can be found below https://github.com/pixijs/pixi-particles https://pixijs.io/pixi-particles-editor/ required packages  /// < reference path = "node_modules/pixi-particles/ambient.d.ts" /> import 'pixi-particles' code of particle delcare a     global variable   private emitter ?: Emitter ; const img = PIXI . Texture . from ( "./assets/images/particle.png" ); this . emitter = new Emitter ( this ,[ img ],{ "alpha" : { "start" : 0.62 , "end" : 0.39 }, "scale" : { "start" : 0.1 , "end" : 0.9 , "minimumScaleMultiplier" : 1.25 }, "color" : { "start" : "#ffff8f" , "end" : ...