3

私は C4 のアルファ リリースで作業しており、オブジェクト間でメッセージを送信しようとしていますが、うまくいきません。私は本当に簡単な例を試していますが、うまくいきません...私はこれを試しました:

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

しかし、私は何のメッセージも何も受け取らない...

これは私が持っているものです:

#import "MyShape.h"

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

1 に答える 1

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は次のようになります。

#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
于 2012-04-24T21:51:50.940 に答える