2

このような 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 であることを示しています。

私が見つけたすべての例から、私はそれをうまくやっているように見えます。しかし、明らかにそうではありません。誰かが私を正しい方向に動かし、私の間違いを指摘できますか?

4

3 に答える 3

4

あなたの問題はここにあります:

NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"p", pointString, nil];

そのはず:

NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:pointString, @"p", nil];

セレクターでは「オブジェクト」が「キー」の前に来るため、項目を ObjectA、KeyForObjectA、ObjectB、KeyForObjectB などとしてリストします。

また、この辞書を割り当て/初期化しますが、決して解放しないため、この辞書をリークしています(ガベージコレクションを使用していないと仮定しています)。

于 2009-10-20T23:09:06.280 に答える
4

オブジェクトとキーが辞書で逆になっています。それは読むべきです

 NSDictionary *dict = [[NSDictionary alloc] 
                         initWithObjectsAndKeys:pointString,@"p", nil];

はい、それはあなたが期待する方法とはまったく逆であり、これは私が辞書を作成する約3回ごとに私を悩ませます.

于 2009-10-20T23:10:15.967 に答える