Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-24-2012, 07:03 AM   #1 (permalink)
Registered Member
 
Join Date: Jan 2012
Posts: 4
cyberclaw2000 is on a distinguished road
Default tennis game glitch, can not find it in the code.

oke so i made a tennis game.

but somehow when i start the game it will not bounce back the ball for the first time, and for as long as the player does not hit the ball, the cpu wont move either.

so score would always be with starting 1-1.
can someone help me with this?

.m file
Code:
#import "TheOlympicTournementsViewController.h"

static sqlite3_stmt *statement = nil;

@implementation TheOlympicTournementsViewController
@synthesize OvervieuwHighscore;
@synthesize ScoreToSubmit;
@synthesize CongratsScore;
@synthesize FillInNameScore;
@synthesize btnSubmitScore;
@synthesize lblTimer;
@synthesize TheTimer;
@synthesize count;

#define kGameStateRunning 1
#define kGameStatePaused  2

#define kBallSpeedX 10
#define kBallSpeedY 15
#define kCompMoveSpeed 15
#define kScoreToWin 3

@synthesize ball,racquet_yellow,racquet_green,player_score,computer_score,gameState,ballVelocity,tapToBegin;

NSTimer *timerIdee;

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
	if(gameState == kGameStatePaused) {
		tapToBegin.hidden = YES;
		gameState = kGameStateRunning;
	}else if(gameState == kGameStateRunning){
        [self touchesMoved:touches withEvent:event];
    }
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView: touch.view];
    CGPoint xLocation = CGPointMake(location.x,racquet_yellow.center.y);

    
    racquet_yellow.center =xLocation;
}

-(void) gameLoop {
	if(gameState == kGameStateRunning) {
        
		ball.center = CGPointMake(ball.center.x + ballVelocity.x , ball.center.y + ballVelocity.y);
		
		if(ball.center.x > self.view.bounds.size.width || ball.center.x < 0) {
			ballVelocity.x = -ballVelocity.x;
		}
		
		if(ball.center.y > self.view.bounds.size.height || ball.center.y < 0) {
			ballVelocity.y = -ballVelocity.y;
		}
        if(CGRectIntersectsRect(ball.frame, racquet_yellow.frame)){
            if(ball.center.y < racquet_yellow.center.y){
                ballVelocity.y = -ballVelocity.y;
            }
        }
        
        if(CGRectIntersectsRect(ball.frame, racuet_green.frame)){
            if(ball.center.y > racuet_green.center.y){
                ballVelocity.y = -ballVelocity.y;
            }
        }
        //simple ai    
        if(ball.center.y<= self.view.center.y){
            if(ball.center.x < racuet_green.center.x){
                CGPoint compLocation = CGPointMake(racuet_green.center.x - kCompMoveSpeed*(playLevel*0.25), racuet_green.center.y);
                racuet_green.center = compLocation;
            }
            if(ball.center.x > racuet_green.center.x){
                CGPoint compLocation = CGPointMake(racuet_green.center.x + kCompMoveSpeed*(playLevel*0.25), racuet_green.center.y);
                racuet_green.center = compLocation;
            }
        }
        //begin scoring game logic
        if(ball.center.y <= 0){
            player_score_value++;
            TotalScore = TotalScore + 200;
            scoreText.text= [NSString stringWithFormat:@"Total score: %d",TotalScore];
            
            playLevel++;
            [self reset:(player_score_value>=kScoreToWin)];
        }
        if(ball.center.y > self.view.bounds.size.height){
            computer_score_value++;
            
            TotalScore = TotalScore-100;
            
                scoreText.text= [NSString stringWithFormat:@"Total score: %d",TotalScore];
            if (!playLevel<=0) {
            playLevel--;
            }
            [self reset:(computer_score_value>=kScoreToWin)];
        }
        
	} else {
		if(tapToBegin.hidden) {
			tapToBegin.hidden = NO;
		}
	}
}

-(void)UpdateTimer:(NSTimer *)theTimer
{
    
    count = Timer_score_value++;
    
    NSString *s = [[NSString alloc] initWithFormat:@"Time: %d", count];
    lblTimer.text = s;
    TotalScore = TotalScore -5;
    scoreText.text= [NSString stringWithFormat:@"Total score: %d",TotalScore];
    [s release];
}

-(void)reset:(BOOL)newGame{
    self.gameState = kGameStatePaused;
    ball.center = self.view.center;
    if(newGame){
        [timerIdee invalidate];
        if(computer_score_value>player_score_value){
            tapToBegin.text=@"Computer Wins!";
            playLevel =1;
        }else {
            tapToBegin.text=@"Player Wins!";
            playLevel=1;
            OvervieuwHighscore.hidden=false;
            ScoreToSubmit.hidden=false;
            btnSubmitScore.hidden=false;
            CongratsScore.hidden=false;
            FillInNameScore.hidden=false;
            ScoreToSubmit.text=scoreText.text;
        }
        
    }else{
        tapToBegin.text=@"Tap to begin";
    }
    
    player_score.text = [NSString stringWithFormat:@"%d", player_score_value];
    computer_score.text= [NSString stringWithFormat:@"%d",computer_score_value];
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
    
	self.gameState = kGameStatePaused;
	ballVelocity = CGPointMake(kBallSpeedX,kBallSpeedY);
	[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(gameLoop) userInfo:nil repeats:YES];
    timerIdee =[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(UpdateTimer:) userInfo:nil repeats:YES];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}

- (void)dealloc {
    [lblTimer release];
    [ScoreToSubmit release];
    [CongratsScore release];
    [FillInNameScore release];
    [OvervieuwHighscore release];
    [btnSubmitScore release];
    [super dealloc];
    [ball release];
    [racuet_yellow release];
    [racuet_green release];
    [tapToBegin release];
    [computer_score release];
    [player_score release];
    
}


- (IBAction)btnBack:(id)sender {
    [self dismissModalViewControllerAnimated:YES];
}


- (BOOL)createEditableCopyOfDatabaseIfNeeded {
    // First, test for existence.
    BOOL success;
    BOOL newInstallation;
    NSFileManager *fileManager = [[NSFileManager alloc] init];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"ToTDB.sqlite"];
    success = [fileManager fileExistsAtPath:writableDBPath];
    newInstallation = !success;
    if (success) {
        [fileManager release];
        return newInstallation;
    }
    // The writable database does not exist, so copy the default to the appropriate location.
    //NSLog(@"database does not exist");
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"ToTDB.sqlite"];
    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
    [fileManager release];
    if (!success) {
        NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
    }
    
    return newInstallation;
}
- (IBAction)SubmitScore:(id)sender {

    [self createEditableCopyOfDatabaseIfNeeded];
    //Opden de database.
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"ToTDB.sqlite"];
    
    if(sqlite3_open([writableDBPath UTF8String], &database) == SQLITE_OK) {
        
        if (statement == nil){
            
            const char *sql = "insert into highscoreTennis (naam, score) Values(?,?);"; 
            sqlite3_bind_text(statement, 1, [FillInNameScore.text UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(statement, 2, [ScoreToSubmit.text  UTF8String], -1, SQLITE_TRANSIENT);
            
            
            if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) != SQLITE_OK)
            {
                NSAssert(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));
                
            }
            else
            {
                
                if(SQLITE_DONE != sqlite3_step(statement))
                {
                    NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
                    
                    
                    sqlite3_reset(statement);
                }
                else
                {  
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Gelukt" message:@"Score Toegevoegd" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
                    
                    [alert show];
                    [alert release];
                }                
            }  
         }
    }
    else
    {
        
        NSAssert(0, @"Failed to open database with message '%s'.", sqlite3_errmsg(database));
        sqlite3_close(database);
        
    }
}

- (void)viewDidUnload {
    [self setLblTimer:nil];
    [self setScoreToSubmit:nil];
    //[self setCongratsScore:nil];
    [self setFillInNameScore:nil];
    [self setOvervieuwHighscore:nil];
    [self setBtnSubmitScore:nil];
    [super viewDidUnload];
}
@end
cyberclaw2000 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 397
12 members and 385 guests
7twenty7, Atatator, condor304, FrankWeller, glenn_sayers, iphonedevshani, MAMN84, mraalex, PowerGoofy, QuantumDoja, tim0504, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,674
Threads: 94,122
Posts: 402,907
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Atatator
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:51 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0