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
Entity Player
start console log
accel:0 - vel:0 - maxVel:30
up/down key pressed console log
accel:1 - vel:NaN - maxVel:30
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