1

I am working with a C4 app, and have created a subclass of C4Shape. I am having trouble accessing the canvas from within the subclass but I'm not sure how to check it, or how to get access to it from another object.

This is the code I have so far:

#import "Platform.h"

@implementation Platform {
    CGPoint o;
    C4Timer *timer;
    int speed;
}

-(void) setup {
    speed = 10;
    [self rect:CGRectMake(0, 0, 100, 100)];

    timer = [C4Timer automaticTimerWithInterval:1.0f/30
                                         target:self
                                         method:@"push"
                                        repeats:YES];
    o = self.center;
}

+(id) platformWithRange:(CGRect)s {
    Platform * bb = [Platform new];
    bb.range = s;
    return bb;
}

-(void) push {
    // check boundaries
    o.x-= speed;
    if( 0 >= o.x - 50 ) {
        o.x = range.size.width;
    }
}
@end
C4 - Travis
  • 4,502
  • 4
  • 31
  • 56
tailedmouse
  • 365
  • 3
  • 16

1 Answers1

1

Have a look at the second part of this answer: https://stackoverflow.com/a/15885302/1218605

You can create a property on your subclass into which you will set the canvas from the main workspace.

@implemenation C4WorkSpace

-(void)setup {
    CustomSubclass *obj = [CustomSubclass new];
    obj.canvas = self.canvas;
}

@end
Community
  • 1
  • 1
C4 - Travis
  • 4,502
  • 4
  • 31
  • 56
  • Thanks! That's really cool, but I was wondering if there is anyway to do this with in the class, because this way, wouldn't I have to move all the shapes and include all the movement in the main canvas? – tailedmouse Nov 04 '13 at 03:14
  • If you design your class properly, they can be independent. If you follow the above trick, and that of the linked answer, all you need to do for each instance of the class is initialize it, then pass it a reference to the canvas. Once the instance has the reference it can use that to figure out where it is in relation to the canvas. – C4 - Travis Nov 04 '13 at 05:17