Cocos2D:通过 CCCallFuncND 传递 CGPoint

发布于 2024-12-06 16:14:09 字数 802 浏览 1 评论 0原文

使用操作调用方法的正确方法是什么?传递 CGPoint 参数的方法本身应该是什么样子?我尝试在网上查找示例,但运气不佳,所以我一直在猜测。

我尝试过这样调用它:

    CGPoint spriteCoord = saveStation.sprite.position;

    id a1=[CCMoveTo actionWithDuration:.4 position:ccp(saveStation.sprite.position.x,saveStation.sprite.position.y)];
    id actionSaveStationReaction = [CCCallFuncND actionWithTarget:self selector:@selector(saveStationReaction : data:) data:&spriteCoord];

    [hero.heroSprite runAction:[CCSequence actions:a1, actionSaveStationReaction, nil]];

以及方法本身:

-(void) saveStationReaction:(id)sender data:(void *)data {

    CGPoint spriteCoord = (void *)data; //error: Invalid initializer 

    NSLog(@"spriteCoord x = %f", spriteCoord.x);
    NSLog(@"spriteCoord y = %f", spriteCoord.y);

}

What is the appropriate way to call a method with an action, and what should the method itself look like for passing a CGPoint parameter? I've tried to look up examples online without much luck, so I've been pretty much guessing.

What I have tried is this for calling it:

    CGPoint spriteCoord = saveStation.sprite.position;

    id a1=[CCMoveTo actionWithDuration:.4 position:ccp(saveStation.sprite.position.x,saveStation.sprite.position.y)];
    id actionSaveStationReaction = [CCCallFuncND actionWithTarget:self selector:@selector(saveStationReaction : data:) data:&spriteCoord];

    [hero.heroSprite runAction:[CCSequence actions:a1, actionSaveStationReaction, nil]];

And the method itself:

-(void) saveStationReaction:(id)sender data:(void *)data {

    CGPoint spriteCoord = (void *)data; //error: Invalid initializer 

    NSLog(@"spriteCoord x = %f", spriteCoord.x);
    NSLog(@"spriteCoord y = %f", spriteCoord.y);

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

陌上青苔 2024-12-13 16:14:09

将 CGPoint(或任何非 id 类型,如 C 结构体)发送到以 id 作为参数的方法(任何使用 PerformSelector 的方法)的正确方法是将其包装在 NSValue 对象中:

NSValue* value = [NSValue valueWithBytes:&spriteCoord objCType:@encode(CGPoint)];

在被调用的方法中您可以通过将数据指针转换为 NSValue* 并调用 getValue 从 NSValue 对象中检索点:

-(void) saveStationReaction:(id)sender data:(void *)data {

    CGPoint spriteCoord;
    [((NSValue*)data) getValue:&spriteCoord];

    NSLog(@"spriteCoord x = %f", spriteCoord.x);
    NSLog(@"spriteCoord y = %f", spriteCoord.y);

}

The proper way to send a CGPoint (or any non-id type like C structs) to a method that takes an id as parameter (any method that uses performSelector) is by wrapping it in an NSValue object:

NSValue* value = [NSValue valueWithBytes:&spriteCoord objCType:@encode(CGPoint)];

In the method that is being called you can retrieve the point from the NSValue object by casting the data pointer to NSValue* and calling getValue:

-(void) saveStationReaction:(id)sender data:(void *)data {

    CGPoint spriteCoord;
    [((NSValue*)data) getValue:&spriteCoord];

    NSLog(@"spriteCoord x = %f", spriteCoord.x);
    NSLog(@"spriteCoord y = %f", spriteCoord.y);

}
弃爱 2024-12-13 16:14:09

GamingHorror 关于将 CGPoint 包装在 NSValue 中的建议是正确的。

但有一个比使用 valueWithByte:objCType: 方法更简单的方法:valueWithCGPoint:,假设您正在为 iOS 而不是 MacOS 编码。

NSValue *spriteCoordValue = [NSValue valueWithCGPoint:spriteCoord];
[CCCallFuncND actionWithTarget:self selector:@selector(saveStationReaction:data:) data:spriteCoordValue];

// and ..
-(void) saveStationReaction:(id)sender data:(void *)data {

    CGPoint spriteCoord = [(NSValue *)data CGPointValue]; 

    NSLog(@"spriteCoord x = %f", spriteCoord.x);
    NSLog(@"spriteCoord y = %f", spriteCoord.y);

}

NSValue 也可以使用类似的方式处理 CGSize 和 CGRect

GamingHorror's suggestion on wrapping the CGPoint in an NSValue is spot-on.

But there's a simpler way than using valueWithByte:objCType: method: valueWithCGPoint:, assuming you are coding for iOS and not MacOS.

NSValue *spriteCoordValue = [NSValue valueWithCGPoint:spriteCoord];
[CCCallFuncND actionWithTarget:self selector:@selector(saveStationReaction:data:) data:spriteCoordValue];

// and ..
-(void) saveStationReaction:(id)sender data:(void *)data {

    CGPoint spriteCoord = [(NSValue *)data CGPointValue]; 

    NSLog(@"spriteCoord x = %f", spriteCoord.x);
    NSLog(@"spriteCoord y = %f", spriteCoord.y);

}

NSValue can also deal with CGSize and CGRect using similar way.

迟月 2024-12-13 16:14:09

您无法使用该语法类型转换为 CGPoint。尝试...

CGPoint *spriteCoord = data;

CGFloat ptX = spriteCoord->x;
CGFloat ptY = spriteCoord->y;

我尝试过...

CGPoint* spriteCoord = (CGPoint)data;

但没有成功,我想也是如此。尝试我的第一个建议,看看是否适合您。它确实为我编译了,但我不确定它将如何执行,这可能取决于您的具体情况。

You can't typecast to a CGPoint with that syntax. Try....

CGPoint *spriteCoord = data;

CGFloat ptX = spriteCoord->x;
CGFloat ptY = spriteCoord->y;

I tried...

CGPoint* spriteCoord = (CGPoint)data;

which didn't work and I guess expectedly so. Try my first suggestion and see if that works for you. It did compile for me but I'm not sure how it will execute and that may depend on your particular situation.

↘人皮目录ツ 2024-12-13 16:14:09

CGPoint 是一个结构体,而不是一个对象,因此您不能将其直接传递给任何 CCCallFunc。有几种方法可以处理这个问题,但最快的方法是使用 NSStringFromCGPoint,传递字符串,然后使用 CGPointFromString

CGPoint is a struct, not an object, so you can't pass it directly to any of the CCCallFunc's. There are several ways of dealing with this, but the quickest converts the CGPoint to NSString using NSStringFromCGPoint, passing the string, then converting it back to a CGPoint using CGPointFromString.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文