NSDictionary到CGPoint

时间:2012-05-24 12:49:23

标签: iphone cocos2d-iphone nsdictionary cgpoint

我正在开发一款游戏,我试图点击两个不同的类型,如果单击和双击子弹分别。

继续我的触摸开始的方法:

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    for( UITouch *touch in touches ) 
    {

        CGPoint location = [touch locationInView: [touch view]];
        location = [[CCDirector sharedDirector] convertToGL: location];

        NSLog(@"TOUCH LOCATION IN TOUCH BEGAN  = (%f , %f)", location.x , location.y);

        NSUInteger tapCount = [touch tapCount];

        switch (tapCount)
        {
            case 1:
            {
                NSDictionary * touchloc = [NSDictionary dictionaryWithObject:[NSValue valueWithCGPoint:location] forKey:@"location"];
                [self performSelector:@selector(startTimer:) withObject:touchloc afterDelay:3];
                break;
            }   
            case 2:
            {
                [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(startTimer) object:nil];
                [self performSelector:@selector(removeBall) withObject:nil afterDelay:1];
                break;
            }

            default:
            {
                break;
            }
        }
  }

现在在我的perform selector(startTimer:)中,我获得了NSPoint所涉及的点的坐标(因为我正在使用NSDictionary) 我想知道的是......我们如何将这些点转换为CGPoints ..?

知道我该怎么办?

任何帮助都将得到真正的赞赏。

4 个答案:

答案 0 :(得分:15)

如果您使用的是NSValue,则可以使用CGPointCGPointValue返回NSDictionary * touchloc = [NSDictionary dictionaryWithObject:[NSValue valueWithCGPoint:location] forKey:@"location"]; CGPoint points = [[touchloc valueForKey:@"location"] CGPointValue];

e.g。

{{1}}

答案 1 :(得分:1)

CGPointCreateDictionaryRepresentationCGPointMakeWithDictionaryRepresentation

答案 2 :(得分:0)

文档说NSPoint是:

typedef struct _NSPoint {
    CGFloat x;
    CGFloat y;
} NSPoint;

和CGPoint是:

struct CGPoint {
     CGFloat x;
    CGFloat y;
};
typedef struct CGPoint CGPoint;

所以它们是等效的结构,你应该能够简单地转换为从一个转换为另一个。两者都不是对象类型,因此您不能直接存储在字典中,而是必须在NSValue中将它们打包以将它们转换为对象。您可能从字典中获取NSValue对象,并且NSValue具有两种类型的访问器,因此您可以在没有强制转换的情况下直接获得所需的对象:

CGPoint a = [somePoint CGPointValue];
NSPoint b = [somePoint pointValue];    

答案 3 :(得分:0)

对我来说,保存和将cgpoint返回到nsdictionary的正确功能如下:

NSDictionary * touchloc = [NSDictionary dictionaryWithObject:[NSValue valueWithPoint:cgPointPositionTouchPoint] forKey:@"location"];

CGPoint points = [[touchloc valueForKey:@"location"] pointValue];