我发现您发布的代码存在一个主要问题。
默认情况下,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