Jump to content

variable help!


dthrasher90
 Share

Recommended Posts

  function qbPass() {


        var x = 1;

        // Math.floor((Math.random() * 3) + 1);
        console.log(x);
        switch (x) {

            case 1:

            var tweenFootballA = game.add.tween(football).to({
                x: '+100',
                y: '-150'
            }, 3000);
                tweenFootballA.start();
                tweenFootballA.onComplete.add(function newlocation (lineOfScrimmage){
                  var lineOfScrimmage = football.x;
                  console.log("anon function = ", lineOfScrimmage);
        

                  return lineOfScrimmage;
                });
                lineOfScrimmage = football.y;
                console.log(lineOfScrimmage);




                // function los(lineOfScrimmage){
                //   lineOfScrimmage = football.y;
                //   console.log("line of scrimmage = ", lineOfScrimmage);
                // }
                console.log('1');

                break;

Need some help here guys!  How can I return this lineOfScrimmage variable with the updated position of football.y?

Link to comment
Share on other sites

Yeah, not sure what you did either, but, you might be doing something slightly nasty but luckily getting the result you want.

From your code snippet, you have two very distinct `lineOfScrimmage` variables:

tweenFootballA.onComplete.add(function newlocation (lineOfScrimmage){
  // #1
  var lineOfScrimmage = football.x;
  console.log("anon function = ", lineOfScrimmage);
        
  return lineOfScrimmage;
});

// #2
lineOfScrimmage = football.y;
console.log(lineOfScrimmage);

#1 in your snippet above is a local variable, local to the `newlocation` function (nice job naming that btw, so many people don't bother but its great for stack traces), as you pass this as a callback to an event emitter there is no way to get it out of there, also, you actually create a third variable by passing los into that function as an argument, however, I don't think you can stipulate what the event emitter will call your function with so its probably redundant there (you'd have to check docs for the tween event handlers, usually you can't do it, and theoretically you shouldn't be able to).

#2 is a global variable, technically omitting 'var' (or 'let', or 'const') is probably an error but, JS being what it is, allows this but, crucially, tacks this variable globally on to window, its the same as doing `window.los = 'foo'`. This means you are leaking a global here when you probably don't mean to be. Also, from your snippet `football` does not exist, but I'm guessing its available somewhere in some code you didn't show (which is odd as it looks like you're showing the start of the function, maybe its being hoisted from further in the function? or is global?) as you'll get an error trying to access a property on it.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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