【发布时间】:2014-03-14 14:08:32
【问题描述】:
我有一批精灵(带纹理的 OpenGL ES 2.0 Quads),我循环移动它们。这是我的代码的简化版本:
//'sprite' & other values have been are assumed to exist for the purpose of the question
public void moveQuadBatch(){
//Loop for as many sprites as there are to draw
for (loop = 0; loop < sprite.number; loop++){
moveQuadNumber(loop); //this method will move the sprite that corresponds to the number loops so we can move through the entire batch and move each individual sprite
}
}
现在,对于某些批次,有一个倒数计时器或其他一些条件(而对于其他批次则没有,如上)。因此,我为这些对象创建了一个类似的方法,如下所示:
public void moveQuadBatchWithCheck(){
//Loop for as many sprites as there are to draw
for (loop = 0; loop < sprite.number; loop++){
//Only do this if the particular sprite's countdown/delay counter has reached 0 (counting down from another method not seen here)
if (sprite.delay[loop]<0){
moveQuadNumber(loop); //this method will move the sprite that corresponds to the number loops so we can move through the entire batch and move each individual sprite
}
}
}
但是,我对此并不完全满意,因为有很多代码重复。除了使用这两种方法之外,有什么方法可以使用第一种方法并以某种方式将附加检查“滑流”到 for 循环中?或者以其他方式减少我在这里的重复?这是一个简单的例子,还有其他的,目前我有多种方法都非常相似。
编辑
如前所述,以上内容有所简化。我可以有一些 for 循环来检查另一个值(例如延迟除外),有些可以检查 2 个条件。
【问题讨论】:
-
为方法添加一些参数。根据参数的值,您可以采取不同的操作。
-
您能举个例子解释一下@B.J.Smegma 吗?谢谢
-
查看 Claudiu 的回答。
标签: java for-loop foreach conditional-statements code-duplication