You're quite close with the implementation of your init method. I would restructure it in the following way:
- (id)initWithColor:(UIColor *)aColor origin:(CGPoint)aPoint {
self = [super init];
if(self != nil) {
CGRect frame = CGRectMake(0,0, 100, 100);
[self rect:frame];
self.lineWidth = 0.0f;
self.fillColor = aColor;
self.origin = aPoint;
}
return self;
}
A couple things to note:
- When subclassing it's always good to call the init method of the object's superclass
- It's good practice to wrap the
init
of your subclass in anif
statement, checking to see if the super class init returned properly. - Create a frame for your new object and call
rect:
onself
directly. - There is an
origin
point in every visible C4 object, so instead of usingx
andy
values directly, you can set the origin with aCGPoint
(theorigin
is the top-left corner).
You then need to add this method to your .h
file:
@interface MyShape : C4Shape
-(id)initWithColor:(UIColor *)aColor origin:(CGPoint)aPoint;
@end
Finally, you can create your shape in your C4WorkSpace
like this:
MyShape *m = [[MyShape alloc] initWithColor:[UIColor darkGrayColor]
origin:CGPointMake(100, 100)];
And, if you add a line to your tapped method you can check the origin point of the button:
-(void)heardTap:(NSNotification *)aNotification {
MyShape *notificationShape = (MyShape *)[aNotification object];
C4Log(@"%4.2f,%4.2f",notificationShape.center.x,notificationShape.center.y);
C4Log(@"%4.2f,%4.2f",notificationShape.origin.x,notificationShape.origin.y);
C4Log(@"%@",notificationShape.strokeColor);
}
While you can work with x
and y
values as properties, I recommend working with CGPoint
structures. It's almost the same, except as you progress from C4 into Objective-C you'll notice that CGPoint
and other CG
geometry structures are used everywhere.