http://jsfiddle.net/goldrunt/jGL84/42/
这是这个JS小提琴的第84行。有3种不同的效果,可以应用到球取消注释行141-146。“反弹”效果发挥了应有的作用,但“爆炸”效果没有任何作用。我是否应该在asplode函数中包含“收缩”函数?
// 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);
}
}
您的代码有一些问题。
首先,在你的定义中:
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是收缩内作用域的本地,因此不能被更新中试图调用它的代码访问。JavaScript作用域是基于函数的,所以更新不能看到asplode,因为它不在收缩中。(在你的控制台中,你会看到这样的错误:Uncaught ReferenceError: asplode is not defined)
你可以先试着离开心理医生:
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);
}
然而,你的代码有几个问题超出了这个问题的范围:
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.
您的代码有一些问题。
首先,在你的定义中:
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是收缩内作用域的本地,因此不能被更新中试图调用它的代码访问。JavaScript作用域是基于函数的,所以更新不能看到asplode,因为它不在收缩中。(在你的控制台中,你会看到这样的错误:Uncaught ReferenceError: asplode is not defined)
你可以先试着离开心理医生:
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);
}
然而,你的代码有几个问题超出了这个问题的范围:
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.