Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8453271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:43:38+00:00 2026-06-10T11:43:38+00:00

Apologies if this is a duplicate and my google-fu weak. I have two options

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:43:39+00:00Added an answer on June 10, 2026 at 11:43 am

    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.

    /* class GameObject */
    public boolean contains(int x, int y)
    {  
        //Your use of parentheses here was really confusing!  
        return mPosition[0] - mScale[0] < x && x < mPosition[0] + mScale[0]
            && mPosition[1] - mScale[1] < y && y < mPosition[1] + mScale[1];
    
        /* alternatively:
        return Math.abs(x - mPosition[0]) < mScale[0]
            && Math.abs(y - mPosition[1]) < mScale[1];
        */
    }
    
    /* class Manager */
    public void onTouchEvent( Event event )
    {
        for( GameObject o : mGameObjectList )
        {
            if(o.contains(event.getX(), event.getY()))
            {
                o.onTouchEvent(event);
            }
        }
    }
    

    I’m not sure the conversion to Vec2 is productive (or consistent) here. Why is the the touch point promoted to a Vec2, yet GameObject::mPosition is an array?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Frstly my apologies if this is a duplicate question. I have tried to find
My apologies if this a duplicate. I have been given the task to add
(apologies if this is a duplicate ... i posted but saw no evidence that
Apologies, this is almost certainly a duplicate of this question but as that one
Apologies if this is a duplicate, 20 minutes of searching didn't yield this exact
My apologies if this is a duplicate; I may not know the proper terms
Apologies if this seems like a duplicate post... Thomas Warner kindly answeres an earlier
I apologize if this is a duplicate. I am writing a class that needs
My apologies if this is a duplicate post post but after some searching, I
I apologize if this is a duplicate but I couldn't find any solutions that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.