Cocos2D:通过 CCCallFuncND 传递 CGPoint
使用操作调用方法的正确方法是什么?传递 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将 CGPoint(或任何非 id 类型,如 C 结构体)发送到以 id 作为参数的方法(任何使用 PerformSelector 的方法)的正确方法是将其包装在 NSValue 对象中:
在被调用的方法中您可以通过将数据指针转换为 NSValue* 并调用 getValue 从 NSValue 对象中检索点:
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:
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:
GamingHorror 关于将 CGPoint 包装在 NSValue 中的建议是正确的。
但有一个比使用
valueWithByte:objCType:
方法更简单的方法:valueWithCGPoint:
,假设您正在为 iOS 而不是 MacOS 编码。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 can also deal with CGSize and CGRect using similar way.
您无法使用该语法类型转换为
CGPoint
。尝试...我尝试过...
但没有成功,我想也是如此。尝试我的第一个建议,看看是否适合您。它确实为我编译了,但我不确定它将如何执行,这可能取决于您的具体情况。
You can't typecast to a
CGPoint
with that syntax. Try....I tried...
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.
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.