我正在尝试通过这样的 NSNotification 发送 CGPoint
-(void)setPosition:(CGPoint)point
{
NSString *pointString = NSStringFromCGPoint(point);
NSDictionary *dict = [[NSDictionary alloc]
initWithObjectsAndKeys:@"p", pointString, nil];
[[NSNotificationCenter defaultCenter]
postNotificationName:@"BownceSpriteDidSetPosition"
object:self
userInfo:dict];
[super setPosition:CGPointMake(point.x, point.y)];
}
我已经像这样实现了观察者
-(void) init
{
if((self = [self init])){
[[NSNotificationCenter defaultCenter]
addObserver:self selector:@selector(setViewPointCenter:)
name:@"BownceSpriteDidSetPosition"
object:nil];
// I wondered wether 'object' should be something else???
// more code etc....
}
return self
}
-(void) setViewPointCenter:(NSNotification *)notification
{
NSString * val = [[notification userInfo] objectForKey:@"p"];
CGPoint point = CGPointFromString(val);
// trying to debug
NSString debugString = [NSString stringWithFormat:@"YPOS -----> %f", point.y];
NSLog(debugString);
CGPoint centerPoint = ccp(240, 160);
viewPoint = ccpSub(centerPoint, point);
self.position = viewPoint;
}
但似乎 CGPoint 是空的,或者 (0,0) 可能。无论哪种方式,它都没有达到预期的效果,并且 debugString 显示 point.y 为 0.0。
从我找到的所有示例中,我觉得我做得很好。但显然我不是。任何人都可以将我推向正确的方向并指出我的错误吗?