2012-04-23 11 views
3

私はC4のアルファ版を使用しています。オブジェクト間でメッセージを送信しようとしていますが、動作させることはできません。 オブジェクト間の通信 - C4Framework

この

は私が持っているものです...

[ashape listenFor:@"touch" from:anothershape andRunMethod:@"receive"]; 

が、私は任意のメッセージや何を得るいけない:私はこれを試してみた...イムは本当に簡単な例をしようが、私はそれを動作させるカント:

#import "MyShape.h" 

@implementation MyShape 
-(void)receive { 
    C4Log(@"this button"); 
} 
@end 
+1

私は答える前にヘビーデイヴィッド、2つの質問:(1) "ashape"と "anothershape"クラスMyShapeの両方のオブジェクトですか? (2)touchesBeganメソッドを使って最初に触れたときに、オブジェクトの1つを反応させようとしていますか? –

+0

1)はい 2)はい。たとえば:私は四角形にする必要がある場合、私は最初の1つを押すと、2番目の正方形の色を変更したいとviceversa。 – davidpenuela

答えて

1

投稿したコードに大きな問題が1つあります。

デフォルトでは、C4のすべての表示オブジェクトは、タップされたときにtouchesBegan通知を送信します。あなたのコードでは@"touch"を聞いていますが、@"touchesBegan"はあなたが聞くべきものです。あなたのようなメソッドを使用することができます

変えるカラー方法は、実装が容易である...あなたのMyShape.mファイルでは、:物事がうまく働いて得るために

-(void)changeColor { 
    CGFloat red = RGBToFloat([C4Math randomInt:255]); 
    CGFloat green = RGBToFloat([C4Math randomInt:255]); 
    CGFloat blue = RGBToFloat([C4Math randomInt:255]); 

    self.fillColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0f]; 
} 

、あなたのC4WorkSpace.mはなっているはずですlike:

#import "C4WorkSpace.h" 
#import "MyShape.h" 

@implementation C4WorkSpace { 
    MyShape *s1, *s2; 
} 

-(void)setup { 
    s1 = [MyShape new]; 
    s2 = [MyShape new]; 

    [s1 rect:CGRectMake(100, 100, 100, 100)]; 
    [s2 rect:CGRectMake(300, 100, 100, 100)]; 

    [s1 listenFor:@"touchesBegan" fromObject:s2 andRunMethod:@"changeColor"]; 
    [s2 listenFor:@"touchesBegan" fromObject:s1 andRunMethod:@"changeColor"]; 

    [self.canvas addShape:s1]; 
    [self.canvas addShape:s2]; 
} 
@end