【问题标题】:NSDictionary to CGPointNSDictionary 到 CGPoint
【发布时间】:2012-05-24 12:49:22
【问题描述】:

我正在开发一款游戏,如果子弹分别在单击和双击上,我会尝试发射两种不同类型的子弹。

这是我在触摸开始方法中所做的:

- (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..?

知道我该怎么做吗?

任何帮助将不胜感激。

【问题讨论】:

    标签: iphone cocos2d-iphone nsdictionary cgpoint


    【解决方案1】:

    如果您使用的是NSValue,您可以使用CGPointValue 获得CGPoint

    例如

    NSDictionary * touchloc = [NSDictionary dictionaryWithObject:[NSValue valueWithCGPoint:location] forKey:@"location"];
        CGPoint points = [[touchloc valueForKey:@"location"] CGPointValue];
    

    【讨论】:

    • 对我来说,保存和返回 cgpoint 到 nsdictionary 的正确函数如下: NSDictionary * touchloc = [NSDictionary dictionaryWithObject:[NSValue valueWithPoint:cgPointPositionTouchPoint] forKey:@"location"]; CGPoint points = [[touchloc valueForKey:@"location"] pointValue];
    【解决方案2】:

    CGPointCreateDictionaryRepresentationCGPointMakeWithDictionaryRepresentation

    【讨论】:

      【解决方案3】:

      对我来说,保存和返回 cgpoint 到 nsdictionary 的正确函数如下:

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

      【讨论】:

        【解决方案4】:

        文档说 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];    
        

        【讨论】:

        • :非常感谢,解释的更清楚了。实际上.. 作为 Objective C 的新手.. 这是我第一次使用 NSDictionary,所以有点困惑。
        猜你喜欢
        • 2010-12-08
        • 1970-01-01
        • 1970-01-01
        • 2012-03-04
        • 1970-01-01
        • 2011-08-07
        • 2011-01-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多