0

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

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

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

4 回答 4

15

如果您正在使用NSValue,您可以CGPoint使用CGPointValue.

例如

NSDictionary * touchloc = [NSDictionary dictionaryWithObject:[NSValue valueWithCGPoint:location] forKey:@"location"];
    CGPoint points = [[touchloc valueForKey:@"location"] CGPointValue];
于 2012-05-24T12:55:14.690 回答
1

CGPointCreateDictionaryRepresentationCGPointMakeWithDictionaryRepresentation

于 2012-05-24T12:51:50.077 回答
1

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

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

CGPoint points = [[touchloc valueForKey:@"location"] pointValue];
于 2014-09-08T09:40:00.663 回答
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];    
于 2012-05-24T13:03:45.463 回答