stampit-org / stampit

OOP is better with stamps: Composable object factories.
https://stampit.js.org
MIT License
3.02k stars 102 forks source link

API ??? #318

Closed Brian151 closed 7 years ago

Brian151 commented 7 years ago

The API docs are still very confusing, and I have a tad bit of a problem:

var Game = function() {
    this.state = "standby";
    var self = this;
    this.timingTick = 33; //approx 30 FPS
    this.timingDraw = 0;
    this.deltaTime = 0;
    this.deltaTime2 = 0;
    this.timeNow = Date.now();
    this.timeLast = this.timeNow;
    this.ticks = 0;
    this.draws = 0;
}

how do i handle the timeLast with stamps? I currently have...

var Game = stampit.props(
{
    state : "boot",
    timingTick : 33,
    timingDraw : 0,
    deltaTime : 0,
    deltaTime2 : 0,
    timeNow : Date.now(),

}
)

I'm assuming I need to use the initializers? but, the API docu is not terribly clear how I actually must do this...

koresar commented 7 years ago

Here you go:

var Game = stampit.init(function (){
    this.state = "standby";
    var self = this;
    this.timingTick = 33; //approx 30 FPS
    this.timingDraw = 0;
    this.deltaTime = 0;
    this.deltaTime2 = 0;
    this.timeNow = Date.now();
    this.timeLast = this.timeNow;
    this.ticks = 0;
    this.draws = 0;
})

Now you can create instances of your Game:

var game = Game();
console.log(game.timeLast); // 1502324424317
koresar commented 7 years ago

Of course, if you need some default properties you do it your way:

var Game = stampit
.props({
    state: "standby",
    timingTick: 33, //approx 30 FPS
    timingDraw: 0,
    deltaTime: 0,
    deltaTime2: 0,
    timeNow: null, // will initialize on creation
    timeLast: null, // will initialize on creation
    ticks: 0,
    draws: 0
})
.init(function (){
    this.timeNow = Date.now();
    this.timeLast = this.timeNow;
})

Fell free to ask any questions.

Brian151 commented 7 years ago

most of them won't need to be initalized, but yep... thx!