我看到您发布的代码存在一个主要问题。
默认情况下,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
嘿大卫,我回答之前的2个问题:(1)是“变形”和“anotherhape”这两个对象的类MyShape? (2)你是否试图使用touchesBegan方法首次触摸另一个对象时反应其中一个对象? – 2012-04-23 17:04:06
1)是 2)是。例如:如果我有广场,我想改变第二个广场的颜色,当我按第一个和viceversa。 – davidpenuela 2012-04-24 21:35:21