Jump to content

Passing Scope Correctly?


jjwallace
 Share

Recommended Posts

I think i am doing something wrong with passing scope, can someone point me in the right direction here?

//Game.js

var myGame = function(){

    var fish = [];

    BasicGame.Game.prototype = {
        create: function () { 
        
            //makeFish is being called from another class

            makeFish: function(game, superThis, x, y, fishSize, angle, pSize){
                    fish.push(new Fish(game, superThis, fish.length, x, y, fishSize, angle, pSize));
            }
            
            function eatSound(){
                var biteMe = Math.floor((Math.random() * 3) + 1);
                console.log('fishscope: ' + bite1);

                if(biteMe == 1){bite1.play();}
                if(biteMe == 2){bite2.play();}
                if(biteMe == 3){bite3.play();}
            }
            
        }
    }
}

Other Class

//Fish.js

var Fish = function (game, superThis, index, x, y, size, angle, pSize) {

}

Fish.prototype.die = function(superThis){
    
    superThis.eatSound();
    
}

ERROR: Cannot read property 'eatSound' of undefined

Link to comment
Share on other sites

Why do you call "new" to a function? Just use it Fish(params) with no "new".

And by the way you create die() function to the prototype of a function. Not sure what are you trying to achieve?

And the error says, no property 'eatSound' in superThis. What do you send as argument for superThis in makeFish() function?

What scope, scope of what?

JS ES5 has only function scope and variable hoisting(all variables are stuck at the top of the function.)

And where do you call die() ?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...