1 Replies - 2560 Views - Last Post: 01 April 2013 - 07:46 PM

#1 lifeinbinary  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 158
  • Joined: 15-February 11

Basic proper way to write and use a function... new to objective c

Posted 01 April 2013 - 07:28 PM

I have been writing in Java and c++ for a few years but having problems getting into objective c. Could someone translate this one function from Java to Objective-C so I can see how it works. I'll post what I have tried also.

Java function

public boolean isInPoly(LinkedList<Point> points, Point test)
    {
        int i,j;
        Boolean c = false;

        for(i = 0, j = points.size()-1; i < points.size(); j = i++ )
        {
            if( ((points.get(i).y > test.y) != 
                    (points.get(j).y > test.y)) && (test.x < (points.get(j).x - points.get(i).x) * (test.y - points.get(i).y) / (points.get(j).y - points.get(i).y) + points.get(i).x))
                c = !c;
        }
        return c;
    }



In my .h file I wrote:
-(bool) isInPoly: (NSArray *) withPoints : (CGPoint) andTestingPoint;



And in my .m file I tried using it different ways but mostly just having problems accessing points and their properties (x and y values) in my NSArray. Here it is...
- (bool)isInPoly:(NSArray *) points:(CGPoint) testPoint
{
    int i, j;
    bool c = FALSE;
    for (i = 0, j = [points count-1]; i < [points count]; j = i++) {
        if ( (([points objectAtIndex:i].x > testPoint.y) != ([points objectAtIndex:j].y > testPoint.y)) &&
            (testPoint.x < ([points objectAtIndex:j].x - [points objectAtIndex:i].x) * (testPoint.y - [points objectAtIndex:i].y) / ([points objectAtIndex:j].y - [points objectAtIndex:i].y) + [points objectAtIndex:i].x) )
            c = !c;
    }
    return c;
    
}



I'm getting a warning for the method call "points used as the name of previous parameter rather than as part of the selector" and errors everywhere saying "property x not found on object of type (id)... my NSArray was instantiated using:
Territory *nwoe = [[Territory alloc] init];
nwoe.points = [NSArray arrayWithObjects:ccp(132,768-190), ccp(258,768-190), ccp(224,768-248), ccp(74,768-244), nil]; 



And Territory being a class:
//
//  Territory.h
//  2210
//
//  Created by Sam Thibodeau on 2013-03-30.
//  Copyright 2013 __MyCompanyName__. All rights reserved.
//

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

@interface Territory : CCNode {
    
}

@property (retain, nonatomic) NSString *name;
@property (retain, nonatomic) NSString *imageName;
@property (nonatomic) CGPoint location;
@property (retain, nonatomic) CCSprite *sprite;
@property (retain, nonatomic) NSArray *points;

@end




I'm really new to objective c so please no shortcuts or tricks... just the proper way of doing things for now :)/>/>

Thank you so much guys!

Is This A Good Question/Topic? 0
  • +

Replies To: Basic proper way to write and use a function... new to objective c

#2 lifeinbinary  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 158
  • Joined: 15-February 11

Re: Basic proper way to write and use a function... new to objective c

Posted 01 April 2013 - 07:46 PM

Ok I fixed by changing initialization of NSArray to:

nwoe.points = [NSArray arrayWithObjects:
                   [NSValue valueWithCGPoint:ccp(132,768-190)],
                   [NSValue valueWithCGPoint:ccp(258,768-190)],
                   [NSValue valueWithCGPoint:ccp(224,768-248)],
                   [NSValue valueWithCGPoint:ccp(74,768-244)], nil];



and my function call to:

- (bool)isInPoly:(NSArray *) points:(CGPoint) testPoint
{
    int i, j;
    bool c = FALSE;
    for (i = 0, j = [points count] - 1; i < [points count]; j = i++) {
        if ( (([[points objectAtIndex:0] CGPointValue].x > testPoint.y) != ([[points objectAtIndex:j] CGPointValue].y > testPoint.y)) &&
            (testPoint.x < ([[points objectAtIndex:j] CGPointValue].x - [[points objectAtIndex:i] CGPointValue].x) * (testPoint.y - [[points objectAtIndex:i] CGPointValue].y) / ([[points objectAtIndex:j] CGPointValue].y - [[points objectAtIndex:i] CGPointValue].y) + [[points objectAtIndex:i] CGPointValue].x) )
            c = !c;
    }
    return c;
    
}



It is still warning about "points used as the name of previous parameter rather than as part of the selector". Couls someone please just explain this NSValue and also how to properly name my function and parameters the objective c way and then how to properly call it without getting this warning?

Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1