The crash repots telling me EXC_BAD_ACCESS so I know I have a crappy pointer(s) in this code but I have no clue how to fix it.
Cloud is a UIImageView Subclass, a CADisplayLink is a timer that fires a method 60 times per second.
My Header
My Implementation
if I just call [self spawnCloud] and then give it a position where the [possibleActions objectAtIndex:arc4random_uniform(possibleActions.count)] is it will work, so I know something is wrong with what I'm doing in the NSMutable Array. I did get EXC_BAD_ACCESS so I do have a bad pointer somewhere in the code
Cloud is a UIImageView Subclass, a CADisplayLink is a timer that fires a method 60 times per second.
My Header
Code:
@interface NormalGame : NSObject{
NSMutableArray* cloudArray;
NSMutableArray* possibleActions;
Cloud* cloud;
CADisplayLink* displayLink;
NSInteger spawnMore;
}
My Implementation
Code:
typedef void(^ActionBlock)(void);
-(void)spawnCloud{
NSString* string = @"cloud.png";
cloud = [[Cloud alloc] initWithName:string];
[self.view insertSubview:cloud atIndex:0];
[cloudArray addObject:cloud];
}
-(id)init{
self = [super init];
if (self) {
displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(gameLoop:)];
[displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode: NSRunLoopCommonModes];
cloudArray = [[NSMutableArray arrayWithCapacity:10] retain];
possibleActions = [NSMutableArray arrayWithCapacity:2];
[possibleActions addObject:^{
[self spawnCloud];
CGPoint cloudPoint1;
cloudPoint1.x = 40;
cloudPoint1.y = 600;
cloud.center = cloudPoint1;
}];
[possibleActions addObject:^{
[self spawnCloud];
CGPoint cloudPoint1;
cloudPoint2.x = 280;
cloudPoint2.y = 600;
cloud.center = cloudPoint2;
}];
[possibleActions retain];
}
-(void)gameLoop:(CADisplayLink *)sender{
spawnMore++;
if (spawnMore >= 280) {
spawnMore -= 280;
ActionBlock block = [possibleActions objectAtIndex:arc4random_uniform(possibleActions.count)];
block();
}
for (cloud in cloudArray){
if (cloud.center.y >= -100) {
cloud.center = CGPointMake(cloud.center.x, cloud.center.y - 3;
}
else if (cloud.center.y >= -100) {
[cloud removeFromSuperview];
[cloudArray removeObject:cloud];
}
}
if I just call [self spawnCloud] and then give it a position where the [possibleActions objectAtIndex:arc4random_uniform(possibleActions.count)] is it will work, so I know something is wrong with what I'm doing in the NSMutable Array. I did get EXC_BAD_ACCESS so I do have a bad pointer somewhere in the code
Last edited: