Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
361 views
in Technique[技术] by (71.8m points)

javascript - 为什么我的球(物体)不会缩小/消失?(Why aren't my ball (objects) shrinking/disappearing?)

http://jsfiddle.net/goldrunt/jGL84/42/ this is from line 84 in this JS fiddle.(http://jsfiddle.net/goldrunt/jGL84/42/这是来自这个JS小提琴的第84行。)

There are 3 different effects which can be applied to the balls by uncommenting lines 141-146.(通过取消注释线141-146,可以将3种不同的效果应用于球。) The 'bounce' effect works as it should, but the 'asplode' effect does nothing.('反弹'效果可以正常工作,但'asplode'效果无效。) Should I include the 'shrink' function inside the asplode function?(我应该在asplode函数中包含'shrink'函数吗?) // 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); } }   ask by MattO translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.(asplodeshrink范围内的本地,因此在您尝试调用它时update中的代码无法访问。) JavaScript scope is function-based, so update cannot see asplode because it is not inside shrink .(JavaScript范围是基于函数的,因此update无法看到asplode因为它不在内部shrink 。) ( In your console , you'll see an error like: Uncaught ReferenceError: asplode is not defined .)(( 在您的控制台中 ,您将看到如下错误: Uncaught ReferenceError: asplode is not defined 。)) You might first try instead moving asplode outside of shrink :(您可能首先尝试在非shrink之外移动asplode :) 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需要一个函数。) setInterval(shrink(p), 100) causes setInterval to get the return value of immediate-invoked shrink(p) .(setInterval(shrink(p), 100)使setInterval获取立即调用的 shrink(p)返回值 。) You probably want(你可能想要) setInterval(function() { shrink(p) }, 100) Your code for (var i = 0; i < 100; i++) { p.radius -= 1; }(你的代码for (var i = 0; i < 100; i++) { p.radius -= 1; }) for (var i = 0; i < 100; i++) { p.radius -= 1; } probably does not do what you think it does.(for (var i = 0; i < 100; i++) { p.radius -= 1; }可能不会做你认为它。) This will immediately run the decrement operation 100 times, and then visually show the result.(这将立即运行减量操作100次, 然后直观地显示结果。) 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).(如果要以每个新大小重新渲染球,则需要在单独的定时回调中执行每个单独的减量(如setInterval操作)。) .splice expects a numeric index, not an object.(.splice需要一个数字索引,而不是一个对象。) You can get the numeric index of an object with indexOf :(您可以使用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).(当你的间隔第一次运行时, balls.splice语句已经发生了( balls.splice它发生在大约100ms前)。) 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 .(相反,你应该有一个由setInterval重复调用的递减函数,最后在p.radius == 0之后执行balls.splice(p,1) 。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...