oo js for as3 devs

22
OO JS for AS3 Devs Object-Oriented JavaScript for ActionScript 3 Developers

Upload: jason-hanson

Post on 18-Nov-2014

1.746 views

Category:

Technology


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: OO JS for AS3 Devs

OO JS for AS3 Devs

Object-Oriented JavaScript for ActionScript 3 Developers

Page 2: OO JS for AS3 Devs

Pirates vs Ninjas

Pirate Ninja

Page 3: OO JS for AS3 Devs

JS vs AS3

1.Object2.Number3.String4.Boolean5.Object6.Object7.Array (Object) 8.Date9.Function10.Function11.Prototype12.<Script src=""/>13.Function

1.Object2.Number | int | unit3.String4.Boolean5.Array6.Dictionary7.Array8.Date9.Function10.Class11.Prototype12.Import13.Getter / Setter

Page 4: OO JS for AS3 Devs

No Strong Types in JS

var myVar1;var myVar1 = 10;var myVar1, myVar2, myVar3;var myVar = [];myVar.push("foo");const EVENT_NAME = "name"

var myVar:Objectpublic myVarpublic function myFunct():Stringfunction myFunct():Stringpackage {}, class {} extends, implements, final,internal, public, private

GOOD PUPPY 

BAD PUPPY 

Page 5: OO JS for AS3 Devs

'Classes' in JS    

• Everything is an Object or Function• Functions are pushed to the limits• A JS Class is just a Function• Function 'scope' rules are used for to create public,

private spaces• Inheritance is wacky (will get to this later)• Prototype is wacky (will get to this later)

 

Page 6: OO JS for AS3 Devs

function myClass(){    this.add = function(a, b)        {        return a + b;    };}var c = new myClass();c.add(1, 1);

TEST IN FIREBUG

Page 7: OO JS for AS3 Devs

Getters / Settersfunction Puppy(){    var _evil = 1000;    this.getEvil = function()    {        return _evil;    };    this.setEvil = function(value)    {        _evil  = value;    }}var evilPuppy = new Puppy();evilPuppy.getEvil();

Page 8: OO JS for AS3 Devs

Self Executing Functionsfunction Puppy(){    var _evil = 999;    this.getEvil = function()    {        return _evil;    };    (function init(value)    {        _evil = v;    })(45)}var evilPuppy = new Puppy();evilPuppy.getEvil();

Page 9: OO JS for AS3 Devs

Scope is a Pain!

function level_1(){    var good = 20;    this.evil = 99;    var timeout = setTimeout(function()        {            console.log(good);            console.log(evil);            console.log(this.evil);        }, 500);}var x = new level_1();

Page 10: OO JS for AS3 Devs

Scope is a Pain - Part 2

function level_1(){    var good = 20;    this.evil = 99;    var scope = this;    var timeout = setTimeout(function()        {            console.log(good);            console.log(scope.evil);        }, 500);}var x = new level_1();

Page 11: OO JS for AS3 Devs

Inheritance

Tons of ways to do Inheritance I will only cover one wayNo "extends" in JSCompletely different way of thinking about itSpecial property on Object named Prototype

Page 12: OO JS for AS3 Devs

function Animal(){    Animal.prototype.evil = 1000;    Animal.prototype.getColor = function()    {        return "Blood Red";    }}function Puppy() {}

Puppy.prototype = Animal.prototype;var p = new Puppy();p.getColor();

Page 13: OO JS for AS3 Devs

function Animal(){    Animal.prototype.evil = 1000;    Animal.prototype.getColor = function()    {        return "Blood Red";    }}function Puppy() {}new Animal(); //(GOTCHA!)Puppy.prototype = Animal.prototype;var p = new Puppy();p.getColor();

Page 14: OO JS for AS3 Devs

function Animal(){    Animal.prototype.color = "Blood Red";    Animal.prototype.getColor = function()    {        return "Evil " + this.color;    }};var a = new Animal();console.log("Animal Color: " + a.getColor());function Puppy() {    this.color = "Blue";}Puppy.prototype = Animal.prototype;var p = new Puppy();console.log("Puppy Color: " + p.getColor());

Page 15: OO JS for AS3 Devs

HTML5 <canvas> tag 

<body><canvas id="gameCanvas" width="600" height="600"></canvas></body>

Page 16: OO JS for AS3 Devs

Get Pointer to <canvas> from JS

// javaScriptvar canvas = document.getElementById('gameCanvas');

var context = canvas.getContext('2d');

Page 17: OO JS for AS3 Devs

Draw on <canvas> with JS

// javaScriptcontext.beginPath();context.moveTo(0,0);context.lineTo(0,100);context.lineTo(100,100);context.lineTo(100,0);context.closePath();context.strokeStyle = "#000000";context.fill();context.fillStyle = "#CCCCCC";context.fill();

Page 18: OO JS for AS3 Devs

"Animation" (in quotes)

var intervalId = setInterval(drawCanvas, 1000 / 30);

function drawCanvas(){    // clear everything    context.clearRect(0,0, myWidth, myHeight);

    // redraw everything    // Yes, you better know exactly     // where every pixel is at all times so     // you can redraw it at 30 FPS    // :(}

Page 19: OO JS for AS3 Devs

"Animation" (in quotes)

var intervalId = setInterval(smartDrawCanvas, 1000 / 30);

function smartDrawCanvas(){    // Clear only part of the canvas    context.clearRect(25, 25, 50, 50);

    // redraw SOME of the canvas    // TODO ....}

Page 20: OO JS for AS3 Devs

Mouse Events on <canvas>

• Cannot listen to events on any children on the <canvas>

• Think of <canvas> as AS3 Bitmap• You can only listen on the main <canvas> object• You need to know where every 'interactive' object is

the <canvas> at all times.• For clicks you need to calculate what was under the

pixel that was clicked on• Observer pattern helps with this work

Page 21: OO JS for AS3 Devs

Gotchas

**JS** (for each works in FF, but not Chrome)for (var x in myArray){    var obj = myArray[x];    obj.selected = false;}

**AS3**for each (var x:Object in myArray){    x.selected = false;}

Page 22: OO JS for AS3 Devs

Gotchas

var timeout = setTimeout(function()    {                var x = passThruVar1;        // x is undefined!!!    }, 500, passThruVar1, passThruVar2);