2012-04-23 57 views
3

我正在与C4的alpha版本,我试图发送消息之间的对象,但我不能让它的工作。林用一个非常简单的例子,尝试,但我不能让它工作...我已经试过这样:对象之间的通信 - C4Framework

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

,但我没有得到任何消息或什么...

这是我有:

#import "MyShape.h" 

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

嘿大卫,我回答之前的2个问题:(1)是“变形”和“anotherhape”这两个对象的类MyShape? (2)你是否试图使用touchesBegan方法首次触摸另一个对象时反应其中一个对象? – 2012-04-23 17:04:06

+0

1)是 2)是。例如:如果我有广场,我想改变第二个广场的颜色,当我按第一个和viceversa。 – davidpenuela 2012-04-24 21:35:21

回答

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