Relationship Between Objects - C4Framework

I am working with the alpha version of C4 and I am trying to send messages between objects, but I cannot get it to work. I am trying with a very simple example, but I cannot get it to work ... I tried this:

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

but I do not receive any messages or anything ...

this is what i have:

#import "MyShape.h"

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

I see one major problem with the code you submitted.

By default, all visible objects in C4 send a notification touchesBeganwhen they are listened to. In your code you are listening @"touch", while @"touchesBegan"- this is what you should listen to.

The changing color method is easy to implement ... In your MyShape.m file, you can use a method, for example:

-(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
+1

All Articles