Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by trigoletto

I'm starting on impact.js and a silly error has been giving me some trouble. My entity velocity starts 0, when I press arrow down or arrow up it changes to NaN and when I release it goes back to 0. Why is my entity velocity not increasing?

Entity Cars
ig.module(
'game.entities.cars'
)

.requires (
'impact.entity'
)

.defines(function(){

var march;
var maxVelPerMarch = new Array();

EntityCars = ig.Entity.extend({
march: 0,
maxVelPerMarch: Array(30,60,90,120,160),

size: {x:96, y:96},
collides:ig.Entity.COLLIDES.ACTIVE,
animSheet: new ig.AnimationSheet('media/playercar.png',96,96),
    gravityFactor:0,
maxVel: {x:5, y:maxVelPerMarch[march]},
friction: {x:0.1,y:0.1},

vel:{y:0,x:0},

init: function(x,y,settings) {
            this.parent(x,y,settings);
            this.addAnim( 'car', 1, [0]);
},

update: function() {
    this.maxVel.y = maxVelPerMarch[march];
    this.parent();
},


});

});

Entity Player

ig.module(
'game.entities.player'
)

.requires (
'game.entities.cars'
)

.defines(function(){

EntityPlayer = EntityCars.extend({  
init: function(x,y,settings) {
            this.parent(x,y,settings);
},

update: function() {

        // Accelerate
        if( ig.input.pressed('accelerate') ) {
            this.accel.y=-1;

        } else if( ig.input.released('accelerate') ) {
            this.accel.y=0;
        }

        // Brake
        if( ig.input.pressed('brake') ) {
            this.accel.y=1;

        } else if( ig.input.released('brake') ) {
            this.accel.y=0;
        }

        console.log("accel:"+this.accel.y+" - vel:"+this.vel.y + " - maxVel:"+this.maxVelPerMarch[this.march]);
    this.parent();
},

});
});

start console log
accel:0 - vel:0 - maxVel:30

up/down key pressed console log
accel:1 - vel:NaN - maxVel:30

1 decade ago by dominic

Typo. You're setting this.maxVel.y to undefined:
this.maxVel.y = maxVelPerMarch[march];

What you wanted:
this.maxVel.y = this.maxVelPerMarch[this.march];

And you're defining maxVelPerMarch twice. Once as a local, empty array in your module, and once as a class instance array with 5 values in EntityCars. maxVelPerMarch will access the empty local array and this.maxVelPerMarch the class instance array.

JavaScript also has this nice JSON syntax to define Arrays and Objects:
EntityCars = ig.Entity.extend({
    maxVelPerMarch: [30,60,90,120,160],

    ...

1 decade ago by trigoletto

Silly error indeed :P

You have been very helpful Dominic, ty
Page 1 of 1
« first « previous next › last »