Porting some Obj-C code with NSValue and NSDictionary::objectForValue to cocos2d-x


#1

It seems that cocos2d-x has no any build-in analogs for NSValue, so I have a some troubles with converting next function:

- (BOOL)isEmpty:(id)target :(CGPoint)position {
    BOOL result = YES;   
    Trail *trail = [_objects objectForKey:[NSValue valueWithCGPoint:position]]; // objectForKey receives "id" parameter, not "const &string" 
    if(trail && [trail.target isEqual:target]) {
        result = NO;
    }
    return result;
}

Anybody has any ideas about this?


#2

could you store them in a ccdictionary with a string representation of the point?
use this as the key:

cocos2d::CCString::createWithFormat(“f,f”,point.x,point.y)->getCString()


#3

it looks like working solution, many thanks :slight_smile:


#4

No problem.
Looks like a bit of a confusing thing you are doing,
why are you storing things with the point as the key?
I am worried that there might be rounding errors and the float might not always be the same.
You could round it to integers and store that (replacing f with i)
which might mean it isn’t out by 0.001 or something the next time you search for it.
But that depends on why you are doing it,
just checking.

Dan


#5

I’m just looking for ways to port original Objective-C code from iOS cocos2d to C++.

Here is a original method ‘addObject’:

- (void)addObject:(CCNode*)object tileCoords:(CGPoint)tileCoords {
    NSValue * key = [NSValue valueWithCGPoint:tileCoords];
    [self removeObjectForKey:key];

    object.position = [self tileCoordToPosition:tileCoords];
    [self addChild:object];
    [_objects setObject:object forKey:key];
}

As you can see, value (object) really has CGPoint-based key and I just have to reproduce the same logic on my side.


#6

Okay, looks like, being to do with grids, it should be fine.
Just wasnt sure if it was a more complicated position.

anyway, I’d use my solution if i were you, unless anyone else knows a better way.
Goodluck