208

http://jsfiddle.net/goldrunt/jGL84/42/ this is from line 84 in this JS fiddle. There are 3 different effects which can be applied to the balls by uncommenting lines 141-146. The 'bounce' effect works as it should, but the 'asplode' effect does nothing. Should I include the 'shrink' function inside the asplode function?

// balls shrink and disappear if they touch
var shrink = function(p) {
    for (var i = 0; i < 100; i++) {
        p.radius -= 1;
    }
    function asplode(p) {
        setInterval(shrink(p),100);
        balls.splice(p, 1);
    }
}
MattO
  • 1,753
  • 3
  • 12
  • 16
  • 12
    `asplode` is not declared in the global scope (or in particular, not defined in a scope accessible to `update`); check our your console. – apsillers Dec 12 '13 at 18:11
  • 39
    Fortunately, that's `balls.splice()` with a `p`. – m59 Dec 12 '13 at 18:17
  • 1
    you have error `Uncaught ReferenceError: asplode is not defined`. Function `asplode()` is not visible. – Anto Jurković Dec 12 '13 at 18:18
  • After pulling asplode out of the function block it was in, you get other errors in the console. – rlemon Dec 12 '13 at 18:18
  • 2
    `asplode` isn't in the right scope, `setInterval` should receive a function reference, `splice` needs an index - or maybe the world is just shrinking with you http://jsfiddle.net/5f85b – bendytree Dec 12 '13 at 18:41
  • Your Counter isn't increasing after 10 anymore! – Lithilion Dec 13 '13 at 00:28
  • 1
    @Panique is correct... http://stackoverflow.com/questions/11066050/why-are-my-balls-disappearing. You even commented on it then :) – brbcoding Dec 18 '13 at 00:46
  • 3
    It's a totally different question. The only thing they have in common is balls. And JavaScript. And attention. Oh and, please, if you want to make jokes, at least be tasteful. (But they're going to get deleted regardless.) – BoltClock Dec 18 '13 at 08:58

2 Answers2

65

Your code has a few problems.

First, in your definition:

var shrink = function(p) {
    for (var i = 0; i < 100; i++) {
        p.radius -= 1;
    }

    function asplode(p) {
         setInterval(shrink(p),100);
        balls.splice(p, 1);
    }
}

asplode is local to the scope inside shrink and therefore not accessible to the code in update where you are attempting to call it. JavaScript scope is function-based, so update cannot see asplode because it is not inside shrink. (In your console, you'll see an error like: Uncaught ReferenceError: asplode is not defined.)

You might first try instead moving asplode outside of shrink:

var shrink = function(p) {
    for (var i = 0; i < 100; i++) {
        p.radius -= 1;
    }
}

function asplode(p) {
     setInterval(shrink(p),100);
     balls.splice(p, 1);
}

However, your code has several more problems that are outside the scope of this question:

  • setInterval expects a function. setInterval(shrink(p), 100) causes setInterval to get the return value of immediate-invoked shrink(p). You probably want

    setInterval(function() { shrink(p) }, 100)
    
  • Your code for (var i = 0; i < 100; i++) { p.radius -= 1; } probably does not do what you think it does. This will immediately run the decrement operation 100 times, and then visually show the result. If you want to re-render the ball at each new size, you will need to perform each individual decrement inside a separate timing callback (like a setInterval operation).

  • .splice expects a numeric index, not an object. You can get the numeric index of an object with indexOf:

    balls.splice(balls.indexOf(p), 1);
    
  • By the time your interval runs for the first time, the balls.splice statement has already happened (it happened about 100ms ago, to be exact). I assume that's not what you want. Instead, you should have a decrementing function that gets repeatedly called by setInterval and finally performs balls.splice(p,1) after p.radius == 0.

Community
  • 1
  • 1
apsillers
  • 112,806
  • 17
  • 235
  • 239
21
setInterval(shrink(p),100);

This doesn't do what you think it does. This calls shrink, passes it p, and then passes the result to setInterval. shrink(p) returns undefined, so this line doesn't actually put anything on an interval.

You probably want:

setInterval(function(){
    shrink(p)
}, 100);
halfer
  • 19,824
  • 17
  • 99
  • 186
gen_Eric
  • 223,194
  • 41
  • 299
  • 337