I'm working on a big game, but i can't seem to get started. When i run this code, i can see it reaches the main view, but when i use the simulator i just get a blank image. Any advice? Also i dragged the main view from another project as it would seem that no one has any idea how to create a MainView
#import "JKGAppDelegate.h"
@implementation JKGAppDelegate
@synthesize window = _window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
mainView = [[MainView alloc] initWithFrame: [UIScreen mainScreen].applicationFrame];
mainView.backgroundColor = [UIColor grayColor];
[self.window addSubview: mainView];
[self.window makeKeyAndVisible];
return YES;
}
- (void) startGameLoop {
NSString *deviceOS = [[UIDevice currentDevice] systemVersion];
bool forceTimerVariant = TRUE;
if (forceTimerVariant || [deviceOS compare: @"3.1" options: NSNumericSearch] == NSOrderedAscending) {
//33 frames per second -> timestep between the frames = 1/33
NSTimeInterval fpsDelta = 0.0303;
timer = [NSTimer scheduledTimerWithTimeInterval: fpsDelta
target: self
selector: @selector( loop )
userInfo: nil
repeats: YES];
} else {
int frameLink = 2;
timer = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget: self selector: @selector( loop )];
[timer setFrameInterval: frameLink];
[timer addToRunLoop: [NSRunLoop currentRunLoop] forMode: NSDefaultRunLoopMode];
}
NSLog(@"Game Loop timer instance: %@", timer);
}
- (void) stopGameLoop {
[timer invalidate];
timer = nil;
}
- (void) loop {
[mainView setNeedsDisplay]; //triggers MainView's drawRect:-method
}
- (void) applicationDidBecomeActive: (UIApplication *) application {
[self startGameLoop];
}
- (void) applicationWillResignActive: (UIApplication *) application {
[self stopGameLoop];
}
- (void)applicationDidEnterBackground:(UIApplication *)application {}
- (void)applicationWillEnterForeground:(UIApplication *)application {}
- (void)applicationWillTerminate:(UIApplication *)application {}
- (void) dealloc {
[self stopGameLoop];
}
@end
#import "MainView.h"
int W=1024;
int H=768;
@implementation MainView
-(int) getRndBtw: (int) bottom and: (int) top{
int rnd = bottom + (arc4random()% (top+1-bottom));
return rnd;
}
- (void) drawRect: (CGRect) rect {
W = rect.size.width;
H = rect.size.height;
static int cnt = 0;
cnt++;
NSLog(@"Game Loop: %i", cnt);
//if (!carBlueImage) {
carBlueImage = [UIImage imageNamed: @"derp.png"];
static int y = 0;
y += 3;
if (y > H) y = -100;
[carBlueImage drawAtPoint: CGPointMake(y, H/2)];
for(int i = 0; i < 50; i++){
int x;
int y;
x = [self getRndBtw:0 and:W];
y = [self getRndBtw:0 and:H];
[carBlueImage drawAtPoint:CGPointMake(x, y)];
x = [self getRndBtw:0 and:H];
y = [self getRndBtw:0 and:W];
NSLog(@"ADT");
[carBlueImage drawAtPoint:CGPointMake(x, y)];
CGContextRef gc = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(gc, 1, 1, 1, 1);
CGContextMoveToPoint(gc, 0, 0);
CGContextAddLineToPoint(gc, W, H);
CGContextStrokePath(gc);
}
NSMutableArray *arrayOfImageViews = [NSMutableArray array];
for (int i = 0; i < 30; i++) {
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"map.png"]];
//[self.view addSubview:imageView];
[arrayOfImageViews addObject:imageView];
}
}
@end
JKGAppDelegate.h
@property(nonatomic,strong) UIViewController *viewController;
JKGAppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
mainView = [[MainView alloc] initWithFrame: [UIScreen mainScreen].applicationFrame];
mainView.backgroundColor = [UIColor grayColor];
self.viewController = [[UIViewController alloc] initWithNibName:@"viewController"];
[self.viewController.view addSubview:mainView];
self.window.rootViewController = self.viewController;
[self.window addSubview: mainView];
[self.window makeKeyAndVisible];
return YES;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.