10-16-2011, 03:35 AM
#1 (permalink )
Registered Member
Join Date: Oct 2011
Posts: 5
Help to optimize my code, please?
I create game.
In this game i have many cycles and it slow game.
Code:
Code:
#import <UIKit/UIKit.h>
#import "PauseClass.h"
#import "GameOverClass.h"
@protocol GameViewControllerDelegate;
@interface GameClass : UIViewController <PauseClassControllerDelegate,GameOverClassControllerDelegate> {
UIImageView *space;
UIImageView *rocket;
UIImageView *coin;
UIImageView *healthpack;
UIImageView *ammo[9];
UIImageView *meteorSmall[18];
UIImageView *meteorNormal[9];
UIImageView *meteorGiant[6];
UIImageView *explosionSmall[18];
UIImageView *explosionNormal[9];
UIImageView *explosionGiant[6];
NSTimer *movePackTimer;
NSTimer *moveHealthTimer;
NSTimer *forCountDown;
UIButton *startButton;
UIButton *pause;
UILabel *scoreLabel;
UILabel *forGameStart;
UILabel *precentsOfLife;
UILabel *timeLabel;
NSInteger gameState;
NSInteger countdown;
NSInteger life;
NSInteger seconds;
NSInteger minuts;
NSInteger hours;
NSInteger ammoIndex;
NSInteger meteorSIndex;
NSInteger meteorNIndex;
NSInteger meteorGIndex;
NSInteger meteorSHealth[18];
NSInteger meteorNHealth[9];
NSInteger meteorGHealth[6];
Boolean meteorSCrash[18];
Boolean meteorNCrash[9];
Boolean meteorGCrash[6];
CGFloat score;
id<GameViewControllerDelegate> delegate;
}
@property (nonatomic, retain) IBOutlet UIImageView *space;
@property (nonatomic, retain) IBOutlet UIImageView *rocket;
@property (nonatomic, retain) UIImageView *healthpack;
@property (nonatomic, retain) UIImageView *coin;
@property (nonatomic, retain) NSTimer *moveCoinTimer;
@property (nonatomic, retain) NSTimer *moveHealthTimer;
@property (nonatomic, retain) NSTimer *forCountDown;
@property (nonatomic, retain) IBOutlet UIButton *startButton;
@property (nonatomic, retain) IBOutlet UIButton *pause;
@property (nonatomic, retain) IBOutlet UILabel *scoreLabel;
@property (nonatomic, retain) IBOutlet UILabel *forGameStart;
@property (nonatomic, retain) IBOutlet UILabel *precentsOfLife;
@property (nonatomic, retain) IBOutlet UILabel *timeLabel;
@property (nonatomic) NSInteger gameState;
@property (nonatomic) NSInteger countdown;
@property (nonatomic) NSInteger life;
@property (nonatomic) NSInteger secunds;
@property (nonatomic) NSInteger minuts;
@property (nonatomic) NSInteger hours;
@property (nonatomic) NSInteger ammoIndex;
@property (nonatomic) NSInteger meteorSIndex;
@property (nonatomic) NSInteger meteorNIndex;
@property (nonatomic) NSInteger meteorGIndex;
@property (nonatomic) CGFloat score;
@property (nonatomic, assign) id<GameViewControllerDelegate> delegate;
- (void)moveSpace;
- (void)gamePlay;
- (void)generateCoins;
- (void)moveCoins;
- (void)generateHealthpakcs;
- (void)moveHealthpacks;
- (void)generateFire;
- (void)generateSmallMeteors;
- (void)generateNormalMeteors;
- (void)generateGiantMeteors;
- (void)gameOver;
- (void)forGameStartTimer;
- (void)timeMethod;
- (IBAction)StartGame:(id)sender;
- (IBAction)PauseGame:(id)sender;
@end
@protocol GameViewControllerDelegate
- (void)gameViewControllerDismiss:(GameClass *)controller;
- (void)gameViewControllerDismissWithExitFromTheGame:(GameClass *)controller;
- (void)gameViewControllerDismissAndRetryTheGame:(GameClass *)controller;
@end
- GameClass.h
Problem:
To create asteroid field i use 3 UIImageView arrays (meteorSmall[18],meteorNormal[9],meteorGiant[6])
Code that slow work of game are - (void)gamePlay.
Many count of cycles are slowing down process.
Can you help me to optimize my code?
10-16-2011, 03:36 AM
#2 (permalink )
Registered Member
Join Date: Oct 2011
Posts: 5
Code:
- (void)gamePlay {
if (gameState == kGameStateRun) {
score +=0.03;
if(rocket.center.y <= 20+rocket.bounds.size.height/2) {
rocket.center = CGPointMake(rocket.center.x, rocket.center.y+rocket.bounds.size.height/4);
}
// "Ammo loop"
for (int i = 1 ; i <= 9 ; i++) {
ammo[i].center = CGPointMake(ammo[i].center.x, ammo[i].center.y-1);
if (ammo[i].center.y <= 0-50) {
[ammo[i] removeFromSuperview];
}
}
// End "Ammo loop"
// "Meteors loops" move
for (int i = 1 ; i <= 18 ; i++) {
if(meteorSmall[i].superview != nil) {
meteorSmall[i].center = CGPointMake(meteorSmall[i].center.x, meteorSmall[i].center.y+1.5);
if (meteorSmall[i].center.y >= 480+10) {
[meteorSmall[i] removeFromSuperview];
}
}
}
for (int i = 1 ; i <= 9 ; i++) {
if(meteorNormal[i].superview != nil) {
meteorNormal[i].center = CGPointMake(meteorNormal[i].center.x, meteorNormal[i].center.y+1.0);
if (meteorNormal[i].center.y >= 480+25) {
[meteorNormal[i] removeFromSuperview];
}
}
}
for (int i = 1 ; i <= 6 ; i++) {
if(meteorGiant[i].superview != nil) {
meteorGiant[i].center = CGPointMake(meteorGiant[i].center.x, meteorGiant[i].center.y+0.75);
if (meteorGiant[i].center.y >= 480+50) {
[meteorGiant[i] removeFromSuperview];
}
}
}
// End "Meteors loops" move
// "Meteor lops" rocket smash
CGRect rocketShip = CGRectMake(rocket.center.x-5, rocket.center.y-5, 10, 10);
for (int i = 1 ; i <= 18 ; i++) {
if(meteorSmall[i].superview != nil) {
if (CGRectIntersectsRect(meteorSmall[i].frame, rocketShip)) {
[meteorSmall[i] removeFromSuperview];
if(life <= 10) {
life = 0;
}
else {
life -= 10;
}
}
}
}
for (int i = 1 ; i <= 9 ; i++) {
if(meteorNormal[i].superview != nil) {
if (CGRectIntersectsRect(meteorNormal[i].frame, rocketShip)) {
[meteorNormal[i] removeFromSuperview];
if(life <= 25) {
life = 0;
}
else {
life -= 25;
}
}
}
}
for (int i = 1 ; i <= 6 ; i++) {
if(meteorGiant[i].superview != nil) {
if (CGRectIntersectsRect(meteorGiant[i].frame, rocketShip)) {
[meteorGiant[i] removeFromSuperview];
if(life <= 50) {
life = 0;
}
else {
life -= 50;
}
}
}
}
// End "Meteor loops" rocket smash
// "Meteor loops" laser fire
for (int i = 1 ; i <= 18 ; i++) {
if(meteorSmall[i].superview != nil) {
for (int j = 1 ; j <= 9 ; j++) {
if(ammo[j].superview != nil) {
if (CGRectIntersectsRect(meteorSmall[i].frame, ammo[j].frame)) {
[meteorSmall[i] removeFromSuperview];
explosionSmall[i].center = CGPointMake(meteorSmall[i].center.x, meteorSmall[i].center.y);
[self.view insertSubview:explosionSmall[i] aboveSubview:rocket];
[UIView beginAnimations:nil context:explosionSmall[i]];
[UIView setAnimationDuration:1];
explosionSmall[i].alpha = 1;
explosionSmall[i].frame = CGRectMake(meteorSmall[i].center.x-20, meteorSmall[i].center.y-20, 40, 40);
[UIView commitAnimations];
[UIView beginAnimations:nil context:explosionSmall[i]];
[UIView setAnimationDuration:1];
explosionSmall[i].alpha = 0;
[UIView commitAnimations];
score += 100;
}
}
}
}
}
for (int i = 1 ; i <= 9 ; i++) {
if(meteorNormal[i].superview != nil) {
for (int j = 1 ; j <= 9 ; j++) {
if(ammo[j].superview != nil) {
if (CGRectIntersectsRect(meteorNormal[i].frame, ammo[j].frame)) {
[meteorNormal[i] removeFromSuperview];
explosionNormal[i].center = CGPointMake(meteorNormal[i].center.x, meteorNormal[i].center.y);
[self.view insertSubview:explosionNormal[i] aboveSubview:rocket];
[UIView beginAnimations:nil context:explosionNormal[i]];
[UIView setAnimationDuration:1];
explosionNormal[i].alpha = 1;
explosionNormal[i].frame = CGRectMake(meteorNormal[i].center.x-30, meteorNormal[i].center.y-30, 60, 60);
[UIView commitAnimations];
[UIView beginAnimations:nil context:explosionNormal[i]];
[UIView setAnimationDuration:1];
explosionNormal[i].alpha = 0;
[UIView commitAnimations];
score += 250;
}
}
}
}
}
for (int i = 1 ; i <= 6 ; i++) {
if(meteorGiant[i].superview != nil) {
for (int j = 1 ; j <= 9 ; j++) {
if(ammo[j].superview != nil && ammo[j].alpha > 0.99) {
if (CGRectIntersectsRect(meteorGiant[i].frame, ammo[j].frame)) {
[meteorGiant[i] removeFromSuperview];
explosionGiant[i].center = CGPointMake(meteorGiant[i].center.x, meteorGiant[i].center.y);
[self.view insertSubview:explosionGiant[i] aboveSubview:rocket];
[UIView beginAnimations:nil context:explosionGiant[i]];
[UIView setAnimationDuration:1];
explosionGiant[i].alpha = 1;
explosionGiant[i].frame = CGRectMake(meteorGiant[i].center.x-30, meteorGiant[i].center.y-30, 60, 60);
[UIView commitAnimations];
[UIView beginAnimations:nil context:explosionGiant[i]];
[UIView setAnimationDuration:1];
explosionGiant[i].alpha = 0;
[UIView commitAnimations];
score += 500;
}
}
}
}
}
if (life == 0) {
gameState = kGameStateOver;
[self gameOver];
}
scoreLabel.text = [NSString stringWithFormat:@"%i",(int)score];
}
precentsOfLife.text = [NSString stringWithFormat:@"%i%%",life];
}
- GameClass.m - (void)gamePlay
10-16-2011, 03:37 AM
#3 (permalink )
Registered Member
Join Date: Oct 2011
Posts: 5
Code:
#import "GameClass.h"
#pragma mark Define block
#define kGameStateRun 1
#define kGameStateOver 2
#define kGameStatePause 3
#define kGameStateStarting 4
#define kEasyGame 0
#define kMediumGame 1
#define kHardGame 2
#define kMaxPowerHealthPack 25
#define kPacksSpeed 1
#define kDegreesToRadians(x) (M_PI) * (x) / 2
#pragma mark - Synthesize
@implementation GameClass
// Delegate
@synthesize delegate;
// Images
@synthesize space,rocket,healthpack,coin;
// Button
@synthesize pause,startButton;
// Labels
@synthesize scoreLabel,forGameStart,precentsOfLife,timeLabel;
// Intergers
@synthesize gameState,ammoIndex,countdown,life;
// Indexes of Meteors
@synthesize meteorSIndex,meteorNIndex,meteorGIndex;
// Time Intergers
@synthesize secunds,minuts,hours;
// Floats
@synthesize score;
// Timers
@synthesize moveCoinTimer,moveHealthTimer,forCountDown;
#pragma mark - Implementation
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *arrayWithExplosions = [NSArray arrayWithObjects:[UIImage imageNamed:@"explosion.png"],[UIImage imageNamed:@"explosion2.png"],[UIImage imageNamed:@"explosion3.png"], nil];
for (int i = 1; i <= 9 ; i++) {
ammo[i] = [[UIImageView alloc] init];
}
for (int i = 1; i <= 18 ; i++) {
meteorSmall[i] = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"smallMeteor.png"]];
meteorSmall[i].center = CGPointMake(0, 0);
meteorSHealth[i] = 2;
meteorSCrash[i] = FALSE;
explosionSmall[i] = [[UIImageView alloc] initWithImage:[arrayWithExplosions objectAtIndex:(arc4random()%3)]];
explosionSmall[i].frame = CGRectMake(0, 0, 30, 30);
explosionSmall[i].alpha = 0;
}
for (int i = 1; i <= 9 ; i++) {
meteorNormal[i] = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"normalMeteor.png"]];
meteorNormal[i].center = CGPointMake(0, 0);
meteorNHealth[i] = 5;
meteorNCrash[i] = FALSE;
explosionNormal[i] = [[UIImageView alloc] initWithImage:[arrayWithExplosions objectAtIndex:(arc4random()%3)]];
explosionNormal[i].frame = CGRectMake(0, 0, 50, 50);
explosionNormal[i].alpha = 0;
}
for (int i = 1; i <= 6 ; i++) {
meteorGiant[i] = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"giantMeteor.png"]];
meteorGiant[i].center = CGPointMake(0, 0);
meteorGHealth[i] = 10;
meteorGCrash[i] = FALSE;
explosionGiant[i] = [[UIImageView alloc] initWithImage:[arrayWithExplosions objectAtIndex:(arc4random()%3)]];
explosionGiant[i].frame = CGRectMake(0, 0, 70, 70);
explosionGiant[i].alpha = 0;
}
ammoIndex = 1;
meteorSIndex = 1;
meteorNIndex = 1;
meteorGIndex = 1;
life = 100;
secunds = 0;
minuts = 0;
hours = 0;
int randomForCoins = round(arc4random()%50);
int randomForHealth = round(arc4random()%50);
UIImage *healthimage = [UIImage imageNamed:@"healthpack.png"];
UIImage *coinimage = [UIImage imageNamed:@"coin.png"];
healthpack = [[UIImageView alloc] init];
healthpack.image = healthimage;
healthpack.frame = CGRectMake(160, -20, healthimage.size.width, healthimage.size.height);
coin = [[UIImageView alloc] init];
coin.image = coinimage;
coin.frame = CGRectMake(160, -20, coinimage.size.width, coinimage.size.height);
gameState = kGameStateStarting;
[NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(moveSpace) userInfo:nil repeats:YES];
[NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(gamePlay) userInfo:nil repeats:YES];
// generate Ammo & Health Packs
[NSTimer scheduledTimerWithTimeInterval:randomForCoins target:self selector:@selector(generateHealthpakcs) userInfo:nil repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:randomForHealth target:self selector:@selector(generateCoins) userInfo:nil repeats:NO];
// Fire from the gun (:D
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(generateFire) userInfo:nil repeats:YES];
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(generateSmallMeteors) userInfo:nil repeats:YES];
[NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(generateNormalMeteors) userInfo:nil repeats:YES];
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(generateGiantMeteors) userInfo:nil repeats:YES];
// Time Timer
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeMethod) userInfo:nil repeats:YES];
NSLog(@"%i",randomForCoins);
NSLog(@"%i",randomForHealth);
[healthimage release];
[coinimage release];
}
- (void)timeMethod {
if(gameState == kGameStateRun) {
NSString *secundStr;
NSString *minutStr;
NSString *hourStr;
secunds += 1;
if(secunds <= 9) {
secundStr = [NSString stringWithFormat:@"0%i",secunds];
}
else {
secundStr = [NSString stringWithFormat:@"%i",secunds];
}
if(minuts <= 9) {
minutStr = [NSString stringWithFormat:@"0%i",minuts];
}
else {
minutStr = [NSString stringWithFormat:@"%i",minuts];
}
if(hours <= 9) {
hourStr = [NSString stringWithFormat:@"0%i",hours];
}
else {
hourStr = [NSString stringWithFormat:@"%i",hours];
}
if (secunds == 59) {
secunds = 0;
minuts += 1;
if (minuts == 59) {
minuts = 0;
hours += 1;
}
}
if((secunds % 2) == 0) {
timeLabel.text = [NSString stringWithFormat:@"%@:%@:%@",hourStr,minutStr,secundStr];
}
else {
timeLabel.text = [NSString stringWithFormat:@"%@ %@ %@",hourStr,minutStr,secundStr];
}
}
}
#pragma mark - Main Logic
- (void)generateFire {
if(gameState == kGameStateRun) {
if (ammo[ammoIndex].superview == nil) {
if(ammoIndex != 9) {
ammo[ammoIndex].image = [UIImage imageNamed:@"fire.png"];
ammo[ammoIndex].frame = CGRectMake(10, 10, 10, 50);
ammo[ammoIndex].center = CGPointMake(rocket.center.x, rocket.center.y-25);
ammo[ammoIndex].alpha = 1;
[self.view insertSubview:ammo[ammoIndex] belowSubview:rocket];
ammoIndex++;
}
else {
ammo[ammoIndex].image = [UIImage imageNamed:@"fire.png"];
ammo[ammoIndex].frame = CGRectMake(10, 10, 10, 50);
ammo[ammoIndex].center = CGPointMake(rocket.center.x, rocket.center.y-25);
ammo[ammoIndex].alpha = 1;
[self.view insertSubview:ammo[ammoIndex] belowSubview:rocket];
ammoIndex = 1;
}
}
}
}
#pragma mark - Meteor Logic
- (void)generateSmallMeteors {
if (gameState == kGameStateRun) {
if (meteorSmall[meteorSIndex].superview == nil) {
if (meteorSIndex != 18) {
meteorSmall[meteorSIndex].frame = CGRectMake(10, 10, 20, 20);
meteorSmall[meteorSIndex].center = CGPointMake((arc4random()%320), 0-10);
[self.view insertSubview:meteorSmall[meteorSIndex] belowSubview:rocket];
meteorSIndex++;
}
else {
meteorSmall[meteorSIndex].frame = CGRectMake(10, 10, 20, 20);
meteorSmall[meteorSIndex].center = CGPointMake((arc4random()%320), 0-10);
[self.view insertSubview:meteorSmall[meteorSIndex] belowSubview:rocket];
meteorSIndex = 1;
}
}
}
}
- (void)generateNormalMeteors {
if (gameState == kGameStateRun) {
if (meteorNormal[meteorNIndex].superview == nil) {
if (meteorNIndex != 9) {
meteorNormal[meteorNIndex].frame = CGRectMake(10, 10, 40, 40);
meteorNormal[meteorNIndex].center = CGPointMake((arc4random()%320), 0-20);
[self.view insertSubview:meteorNormal[meteorNIndex] belowSubview:rocket];
meteorNIndex++;
}
else {
meteorNormal[meteorNIndex].frame = CGRectMake(10, 10, 40, 40);
meteorNormal[meteorNIndex].center = CGPointMake((arc4random()%320), 0-20);
[self.view insertSubview:meteorNormal[meteorNIndex] belowSubview:rocket];
meteorNIndex = 1;
}
}
}
}
- (void)generateGiantMeteors {
if (gameState == kGameStateRun) {
if (meteorGiant[meteorGIndex].superview == nil) {
if (meteorGIndex != 6) {
meteorGiant[meteorGIndex].frame = CGRectMake(10, 10, 60, 60);
meteorGiant[meteorGIndex].center = CGPointMake((arc4random()%320), 0-30);
[self.view insertSubview:meteorGiant[meteorGIndex] belowSubview:rocket];
meteorGIndex++;
}
else {
meteorGiant[meteorGIndex].frame = CGRectMake(10, 10, 60, 60);
meteorGiant[meteorGIndex].center = CGPointMake((arc4random()%320), 0-30);
[self.view insertSubview:meteorNormal[meteorGIndex] belowSubview:rocket];
meteorGIndex = 1;
}
}
}
}
#pragma mark - Game Play
...
Before - (void)gamePlay
10-16-2011, 03:38 AM
#4 (permalink )
Registered Member
Join Date: Oct 2011
Posts: 5
Code:
...
- (void)moveSpace {
if(gameState == kGameStateRun) {
if(space.center.y >= 0) {
space.center = CGPointMake(space.center.x, space.center.y+0.75);
}
if(space.center.y >= 480) {
space.center = CGPointMake(space.center.x, 0);
}
}
}
#pragma mark - Health packs
- (void)generateHealthpakcs {
if(gameState == kGameStateRun) {
[self.view insertSubview:healthpack belowSubview:rocket];
moveHealthTimer = [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(moveHealthpacks) userInfo:nil repeats:YES];
NSLog(@"Health Pack");
}
}
- (void)moveHealthpacks {
if (gameState == kGameStateRun) {
healthpack.center = CGPointMake(healthpack.center.x, healthpack.center.y+kPacksSpeed);
if(healthpack.center.y >= self.view.bounds.size.height+healthpack.bounds.size.height) {
int randomHealth = round(arc4random()%50);
[NSTimer scheduledTimerWithTimeInterval:randomHealth target:self selector:@selector(generateHealthpakcs) userInfo:nil repeats:NO];
[healthpack removeFromSuperview];
[moveHealthTimer invalidate];
healthpack.center = CGPointMake(round(random()%320), 0);
NSLog(@"%i",randomHealth);
}
if(CGRectIntersectsRect(rocket.frame, healthpack.frame)) {
int randomHealth = round(arc4random()%50);
[NSTimer scheduledTimerWithTimeInterval:randomHealth target:self selector:@selector(generateHealthpakcs) userInfo:nil repeats:NO];
score += 100;
NSString *scoreText = [NSString stringWithFormat:@"Score: %i",score];
[moveHealthTimer invalidate];
[healthpack removeFromSuperview];
healthpack.center = CGPointMake(round(random()%320), 0);
life += kMaxPowerHealthPack;
if(life > 100) {
life = 100;
}
scoreLabel.text = scoreText;
NSLog(@"%i",randomHealth);
}
}
}
#pragma mark - Coins
- (void)generateCoins {
if(gameState == kGameStateRun) {
[self.view insertSubview:coin belowSubview:rocket];
moveCoinTimer = [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(moveCoins) userInfo:nil repeats:YES];
NSLog(@"Coins");
}
}
- (void)moveCoins {
if(gameState == kGameStateRun) {
coin.center = CGPointMake(coin.center.x, coin.center.y+kPacksSpeed);
if(coin.center.y >= self.view.bounds.size.height+coin.bounds.size.height) {
int randomCoin = round(arc4random()%50);
[NSTimer scheduledTimerWithTimeInterval:randomCoin target:self selector:@selector(generateCoins) userInfo:nil repeats:NO];
[coin removeFromSuperview];
[moveCoinTimer invalidate];
coin.center = CGPointMake(round(random()%320), 0);
NSLog(@"%i",randomCoin);
}
if(CGRectIntersectsRect(rocket.frame, coin.frame)) {
int randomCoin = round(arc4random()%50);
[NSTimer scheduledTimerWithTimeInterval:randomCoin target:self selector:@selector(generateCoins) userInfo:nil repeats:NO];
score += 1000;
NSString *scoreText = [NSString stringWithFormat:@"Score: %i",score];
[moveCoinTimer invalidate];
[coin removeFromSuperview];
coin.center = CGPointMake(round(random()%320), 0);
scoreLabel.text = scoreText;
NSLog(@"%i",randomCoin);
}
}
}
#pragma mark - Other functions
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if(gameState == kGameStateRun) {
UITouch *touch = [[event touchesForView:rocket] anyObject];
CGPoint loc = [touch locationInView:self.view];
if(loc.x != 0 && loc.y != 0) {
rocket.center = CGPointMake(loc.x, loc.y);
}
}
}
- (IBAction)StartGame:(id)sender {
[sender setHidden:YES];
gameState = kGameStateRun;
}
- (IBAction)PauseGame:(id)sender {
if(gameState == kGameStateRun) {
gameState = kGameStatePause;
PauseClass *pauseClass = [[PauseClass alloc] initWithNibName:@"PauseWindow" bundle:nil];
pauseClass.pauseDelegate = self;
pauseClass.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:pauseClass animated:YES];
[pauseClass release];
countdown = 3;
}
}
- (void)pauseViewControllerDismiss:(PauseClass *)controller {
[controller dismissModalViewControllerAnimated:YES];
forGameStart.center = CGPointMake(160, 240);
forGameStart.text = @"3";
forGameStart.alpha = 1;
forGameStart.hidden = NO;
forCountDown = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(forGameStartTimer) userInfo:nil repeats:YES];
}
- (void)pauseViewControllerDismissAndSendToMainMenu:(PauseClass *)controller {
[controller dismissModalViewControllerAnimated:NO];
[delegate gameViewControllerDismissWithExitFromTheGame:self];
}
- (void)forGameStartTimer {
if(countdown != 1) {
countdown--;
forGameStart.text = [NSString stringWithFormat:@"%i",countdown];
}
else {
[forCountDown invalidate];
[UIView beginAnimations:nil context:forGameStart];
[UIView setAnimationDuration:1];
int randomm = arc4random() % 2;
if(randomm == 0) {
forGameStart.center = CGPointMake(0-100, forGameStart.center.y);
}
else if(randomm == 1) {
forGameStart.center = CGPointMake(320+100, forGameStart.center.y);
}
forGameStart.alpha = 0;
[UIView commitAnimations];
gameState = kGameStateRun;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
[super viewDidUnload];
for (int i = 1 ; i <= 9; i++) {
ammo[i] = nil;
}
for (int i = 1 ; i <= 18; i++) {
meteorSmall[i] = nil;
explosionSmall[i] = nil;
}
for (int i = 1 ; i <= 9; i++) {
meteorNormal[i] = nil;
explosionNormal[i] = nil;
}
for (int i = 1 ; i <= 6; i++) {
meteorGiant[i] = nil;
explosionGiant[i] = nil;
}
self.space = nil;
self.rocket = nil;
self.healthpack = nil;
self.coin = nil;
self.moveHealthTimer = nil;
self.moveCoinTimer = nil;
self.pause = nil;
self.startButton = nil;
self.scoreLabel = nil;
}
- (void)dealloc {
[super dealloc];
for (int i = 1 ; i <= 9 ; i++) {
[ammo[i] release];
}
for (int i = 1 ; i <= 18; i++) {
[meteorSmall[i] release];
[explosionSmall[i] release];
}
for (int i = 1 ; i <= 9; i++) {
[meteorNormal[i] release];
[explosionNormal[i] release];
}
for (int i = 1 ; i <= 6; i++) {
[meteorGiant[i] release];
[explosionGiant[i] release];
}
[space release];
[rocket release];
[healthpack release];
[coin release];
[moveHealthTimer release];
[moveCoinTimer release];
[pause release];
[startButton release];
[scoreLabel release];
}
- (void)gameOver {
gameState = kGameStateOver;
GameOverClass *gameOverClass = [[GameOverClass alloc] initWithNibName:@"GameOverWindow" bundle:nil];
gameOverClass.gameOverDelegate = self;
gameOverClass.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:gameOverClass animated:YES];
[gameOverClass release];
}
- (void)gameOverViewControllerDismiss:(GameOverClass *)controller {
[controller dismissModalViewControllerAnimated:NO];
[delegate gameViewControllerDismissWithExitFromTheGame:self];
}
- (void)gameOverViewControllerDismissAndRetryGame:(GameOverClass *)controller {
[controller dismissModalViewControllerAnimated:NO];
[delegate gameViewControllerDismissAndRetryTheGame:self];
}
#pragma mark - Orientation
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
After - (void)gamePlay
10-16-2011, 03:32 PM
#5 (permalink )
Registered Member
Join Date: Jan 2011
Location: South Florida, US
Posts: 357
Before the moderators and others begin attacking, let me point out a few things ...
a) You going to throw money at any of us? My boss pays me for this kind of thing, and I happen to like her. You ... I don't know you from Adam.
b) XCode has performance monitoring tools of all kinds. You can run the CPU sampler, you can push it through prof, and there are even samplers to watch your GUI performance. You should try some of those and then come back with "method XYZ is taking lots of time and I can't figure it out" versus "analyze my entire program for me"
c) NSLog is dog-slow.
d) You've wrapped loops around loops around loops. A "for 1..6" inside a "for 1..6" means 36 iterations. S**t adds up fast. I'm thinking you need to Google "Game Loop".
e) Try a book. "Beginning iPhone Games Development", published by Apress, goes through Asteroids first as a UIKit and then OpenGL and then adds sound effects, etc. Their source code is online (
Apress ). It's available as an eBook. I expect it will answer all your questions.
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Online Users: 389
10 members and 379 guests
7twenty7 , apatsufas , comicool , Creativ , Dalia , dansparrow , Duncan C , Murphy , pbart , Tomsky
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,127
Posts: 402,916
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55