Apologies if this is a duplicate and my google-fu weak.
I have two options for method routing in my code:
Use a local method that calls a bunch of object attributes:
/* 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;
}
Call an object method which then uses local attributes:
/* 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;
}
Which set of methods would be better programmatically ( optimal, elegant, simple, etc. )?
Update: fixed code sections. Sorry about that all.
I would write this in terms of a
GameObject::containsPoint(x, y)method. That way, it does not need to know about touch events, but nor does your touch class need to know about calculating intersections.EDIT:
Here’s how I’d do it.
I’m not sure the conversion to
Vec2is productive (or consistent) here. Why is the the touch point promoted to aVec2, yetGameObject::mPositionis an array?