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 6330405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:51:48+00:00 2026-05-24T17:51:48+00:00

I’m creating a CGRect within a class (Path). I’ve verified that the the class

  • 0

I’m creating a CGRect within a class (Path). I’ve verified that the the class is creating the rectangle. Now, I’ve built a method into the class that should, in theory, return true when asked if a touch location is within the rectangle. Problem is, it always returns false. I’ve verified that the method is getting the touch, as needed. Relevant code below:

Path.h

#import <Foundation/Foundation.h>
#import "cocos2d.h"

@interface Path : CCSprite {
    CGPoint startP;
    CGPoint endP;
    CGRect pathRect;
    CGPoint touchP;
}

-(id)initWithPoints:(CGPoint)sP :(CGPoint)eP;
-(bool)touchWithinBounds:(CGPoint)touch;

@property (nonatomic, assign) CGPoint startP;
@property (nonatomic, assign) CGPoint endP;
@property (nonatomic, assign) CGRect pathRect;
@end

Path.mm

#import "Path.h"

@implementation Path

@synthesize startP;
@synthesize endP;
@synthesize pathRect;

-(id) initWithPoints:(CGPoint)sP :(CGPoint)eP {
    if ((self = [super init])) {
        startP = sP;
        endP = eP;
        pathRect = CGRectMake(startP.x-2, endP.y, 5, 480);
    }
    return self;
}

-(bool)touchWithinBounds:(CGPoint)touch {
    touchP = touch;
    if(CGRectContainsPoint([self pathRect], touch)) {
        return true;
    } else {
        return false;
    }
}

-(void) draw {
    glColor4f(1.0f,1.0f,1.0f,1.0f);
    glLineWidth(5.0f);
    ccDrawLine(startP, endP);
}

-(void) dealloc {
    [super dealloc];
}

@end

In my main game scene, I initialize a path and add it to a mutable array:

paths = [[NSMutableArray alloc] init];
        Path *path1 = [[[Path alloc] initWithPoints:ccp(winSize.width/3, 0.0) :ccp(winSize.width/3, winSize.height)] autorelease];
        [self addChild:path1 z:0];
        [paths addObject:path1];

And then in ccTouchesBegan, I send the touch location to touchWithinBounds. But instead of reporting true when the rectangle is touched, it always reports false:

UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView: [touch view]];
    location = [[CCDirector sharedDirector] convertToGL: [touch locationInView:touch.view ]];
    for (Path *path in paths) {
        CCLOG(@"%d", [path touchWithinBounds:location]);

Is there some kind of connection I’m missing between the pieces, or am I misunderstanding how the interaction works all-together?

Thanks

  • 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-05-24T17:51:49+00:00Added an answer on May 24, 2026 at 5:51 pm

    Figured it out. I had the points I was originating my rectangle at wrong.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a jquery bug and I've been looking for hours now, I can't
I've got a string that has curly quotes in it. I'd like to replace
I am currently running into a problem where an element is coming back from
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post

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.