public function collide():Boolean {
var tanks:Array = Global.tanks;
// collision detection between bullets and tanks
for (var i:int = tanks.length - 1; i >= 0; i--)
{
// tanks won't hit themselves
if (sender.id == tanks[i].id || sender.type == tanks[i].type) {
continue;
}
if (new Point(global_x, global_y).collideRect(tanks[i].getRect())) {
// check tank's hp points
if (tanks[i].hp > 0) {
tanks[i].hp -= this.damage;
}
if (tanks[i].hp <= 0) {
if (tanks[i].type == 'enemy_tank') {
Global.current_enemies_num--;
// gain experience
(this.sender as AllyTank).gainExp(tanks[i].containing_exp);
}
tanks[i].remove();
Global.dying_tanks = Global.dying_tanks.concat(tanks.splice(i, 1));
}
return true;
}
}
...
想将循环内的代码封装为Bullet::hitTank(), 但是这里面的代码有continue和return true, 分别影响for循环及collide()
如果封装成方法, 怎么才能使这段代码原有的影响不会改变
想到一个方法是方法返回string, 然后检查返回值做出相应操作: 比如返回'hit_self'的话就continue, 返回'hit_complete'就返回return true
坏处就是无法快速得知这个方法所有的返回值(仅通过看函数原型)
考虑更好的方法