Local var insists to be NaN


#1

i did this func

var ExisInternalView = pc.createScript('exisInternalView');



ExisInternalView.attributes.add('zoomSpeed', { type:'number', title:'Zoom Speed'});

// initialize code called once per entity
ExisInternalView.prototype.initialize = function() {
    var dist=0;
    console.log(this.dist);

    this.app.mouse.on(pc.EVENT_MOUSEWHEEL, this.onMouseWheel, this);
    this.on('destroy', function(){
        
        this.app.mouse.off(pc.EVENT_MOUSEWHEEL, this.onMouseWheel,this);
    });
    
};

ExisInternalView.prototype.onMouseWheel = function (event) {
    this.dist -= event.wheel * this.zoomSpeed * (this.dist * 0.1);
    console.log(this.dist);

};

but for some odd reason, although i believe i defined the var ‘dist’ properly, when i scroll the wheel,
dist is printed in the initilize as undefined (but it was defined 1 line ago)
and the OnMouseWheel its been printed as NaN

am i missing something?


#2

this.dist is undefined when you try to multiply it by 0.1

Change

ExisInternalView.prototype.initialize = function() {
    var dist=0;
    console.log(this.dist);

to

ExisInternalView.prototype.initialize = function() {
    this.dist=0;
    console.log(this.dist);

#3

if i got this, marking it as var made it only exist in the scope of the init?


#4

That’s correct

2020202