抱歉,如果这是重复的并且我的 google-fu 很弱。
我的代码中有两个方法路由选项:
使用调用一堆对象属性的本地方法:
/* public class Manager */
public void onTouchEvent( Event event )
{
Vec2 touch = new Vec2( event.getX(), event.getY() );
for( GameObject o : mGameObjectList )
{
if ( isColliding( touch, o ) )
o.onTouchEvent(event);
}
}
public boolean isColliding( Vec2 touch, GameObject obj )
{
if ( ( obj.mPosition[ 0 ] - obj.mScale[ 0 ] ) < touch.x && touch.x < ( obj.mPosition[ 0 ] + obj.mScale[ 0 ] ) )
{
if ( ( obj.mPosition[ 1 ] - obj.mScale[ 1 ] ) < touch.y && touch.y < ( obj.mPosition[ 1 ] + obj.mScale[ 1 ] ) )
{
return true;
}
}
return false;
}
调用一个对象方法,然后使用本地属性:
/* public class Manager */
public void onTouchEvent( Event event )
{
for( GameObject o : mGameObjectList )
{
o.isColliding(event);
}
}
/* public class GameObject */
public boolean isColliding( Event event )
{
// code here to get touch from event ( or maybe pass in touch? )
if ( ( mPosition[ 0 ] - mScale[ 0 ] ) < touch.x && touch.x < mPosition[ 0 ] + ( mScale[ 0 ] ) )
{
if ( ( mPosition[ 1 ] - mScale[ 1 ] ) < touch.y && touch.y < mPosition[ 1 ] + ( mScale[ 1 ] ) )
{
onTouchEvent(event)
}
}
return false;
}
哪组方法在编程上会更好(最佳、优雅、简单等)?
更新:固定代码部分。对不起,这一切。