Domanda

Sto cercando di inviare un CGPoint attraverso un NSNotification come questo

-(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)];
}

E ho implementato l'osservatore come questo

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

Ma sembra che CGPoint è vuoto, o (0,0) forse. In entrambi i casi, non sta avendo l'effetto desiderato, e la debugString è mostrando punto.Ý essere 0.0.

Da tutti gli esempi che ho trovato, sembra a me come io sto facendo tutto bene. Ma ovviamente non lo sono. Qualcuno mi può spingere nella giusta direzione e sottolineare il mio errore?

È stato utile?

Soluzione

Hai la tua oggetti e chiavi invertiti nel dizionario. Dovrebbe essere visualizzato

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

Sì, è esattamente al contrario del modo in cui ci si aspetterebbe che fosse e questo mi morde su ogni terza volta che creo un dizionario.

Altri suggerimenti

Il tuo problema è qui:

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

Dovrebbe essere:

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

"Oggetti" viene prima di "chiavi" nel selettore, in modo da elencare i tuoi articoli come Objecta, KeyForObjectA, ObjectB, KeyForObjectB, ecc.

Si sta anche perdendo questo dizionario, dal momento che si alloc / init, ma non rilasciarlo (sto supponendo che non si sta usando la raccolta dei rifiuti).

Nella nuova sintassi Objective-C è meglio usare:

NSDictionary *dict = @{@"p": [NSValue valueWithCGPoint:point]};

più facile da understend ed esso uso NSValue invece di NSString.

C'è anche un problema con la rimozione di osservatore. Nel codice, si usa solo [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setViewPointCenter:) name:@"BownceSpriteDidSetPosition" object:nil]; ma mai chiamare [[NSNotificationCenter defaultCenter] removeObserver:self];, tuute le esigenze in grado di produrre brutto incidente, che sarebbe difficile da eseguire il debug. Io suggerisco voi utilizzando https://github.com/AllinMobile/AIMObservers che impedisce questo tipo di incidente. Si potrebbe riscrivere il codice in questo modo:

__weak __typeof(self) weakSelf = self;
self.observer = [AIMNotificationObserver observeName:@"BownceSpriteDidSetPosition" onChange:^(NSNotification *notification) {
   NSValue *valueOfPoint =  [notification userInfo][@"p"];
   CGPoint point = [valueOfPoint CGPointValue];
   CGPoint centerPoint = ccp(240, 160);
   viewPoint = ccpSub(centerPoint, point);
   //use weakSelf to avoid strong reference cycles
   weakSelf.position = viewPoint;
}];
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top