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-11-2011, 10:13 AM   #1 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 22
rabbigarfinkel is on a distinguished road
Default Help with dodge game?

Hello,

The main part of my app is a simple "dodge the bouncing sprite" code. (See the bold text in my .m below.) It usually works fine in all orientations. But often enough, the sprite does not bounce on a diagonal and instead bounces back and forth on the edges in a vertical or horizontal line. (Other times, the sprite simply disappears off the edge of the screen.) This makes the game far less challenging, and I want to prevent it.

What can I add or change to ensure that the sprite will always bounce on a diagonal no matter what orientation the device is in?

Thank you!

My .h:

Code:
#import <UIKit/UIKit.h>

@interface dodgeViewController : UIViewController {
	
	IBOutlet UIImageView *rabbi;
	IBOutlet UIImageView *pig;

	IBOutlet UILabel *aveiros;
	IBOutlet UILabel *mitzvos;
	NSTimer *myTimer;
	BOOL orientationIsPortrait;
	BOOL orientationIsLandscape;
	
	CGPoint pos;
	
}

-(void)checkcollision;
-(void)onTimer;

@property (nonatomic,assign) BOOL orientationIsPortrait;
@property (nonatomic,assign) BOOL orientationIsLandscape;



@property (nonatomic, retain) UIImageView *pig;
@property (nonatomic, retain) UILabel *aveiros;
@property (nonatomic, retain) UILabel *mitzvos;
@property (nonatomic, retain) UIImageView *rabbi;

@end
My .m:

Code:
#import "dodgeViewController.h"

@implementation dodgeViewController
@synthesize pig,rabbi,aveiros,mitzvos;
int hit = 0;
int x,y;
int sins=0;
int mitzvot=0;




-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
	
	UITouch *touch = [[event allTouches] anyObject];
	CGPoint location = [touch locationInView:touch.view];
	rabbi.center = location;
	
	[self checkcollision];
	
}

-(void)onTimer {
	
	UIInterfaceOrientation toOrientation = self.interfaceOrientation;
	
	if ((toOrientation == UIInterfaceOrientationPortrait) || (toOrientation == UIInterfaceOrientationPortraitUpsideDown))
	
	{
		
	
	x = 1 + arc4random()%320;
	y = 1 + arc4random()% 480;
		
	pig.center = CGPointMake(pig.center.x+pos.x,pig.center.y+pos.y);
	if (pig.center.x > 320 || pig.center.x < 0)
		pos.x = -pos.x;
	if (pig.center.y > 480 || pig.center.y < 0)
		pos.y = -pos.y;
		
		NSLog (@"Portrait x coordinate is equal to %i",x);
		NSLog (@"Portrait y coordinate is equal to %i",y);
	
	[self checkcollision];
		
	}
	
	else {
		if ((toOrientation == UIInterfaceOrientationLandscapeLeft) || (toOrientation == UIInterfaceOrientationLandscapeRight))
			
			
			hit = 0;														   
		x = 1 + arc4random()%480;
		y = 1 + arc4random()%320;
		
		pig.center = CGPointMake(pig.center.x+pos.x,pig.center.y+pos.y);
		
		if (pig.center.x > 480 || pig.center.x < 0)
			pos.x = -pos.x;
		if (pig.center.y > 320 || pig.center.y < 0)
			pos.y = -pos.y;
		
		NSLog (@"Landscape x coordinate is equal to %i",x);
		NSLog (@"Landscape y coordinate is equal to %i",y);
		
		
		[self checkcollision];
		
		
	}
	
	}

	
	





-(void)checkcollision {
	
	if (CGRectIntersectsRect(rabbi.frame,pig.frame)) {
		
		++sins;
		
		hit = 1;
		if (hit == 1) {
		aveiros.text = [NSString stringWithFormat:@"The number of hits is %d",sins];
		}
		hit = 0;
		
		
				
		
		
				
					
	}
			
}


-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
	
	[self touchesBegan:touches withEvent:event];
	
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	pos = CGPointMake(20.0,10.0);
	myTimer = [NSTimer scheduledTimerWithTimeInterval:0.04 target:self selector:@selector (onTimer) userInfo:nil repeats:YES];
	

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	// Return YES for supported orientations
	return YES;
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}

- (void)dealloc {
		[pig release];
		[rabbi release];
		[aveiros release];
		[mitzvos release];
		[super dealloc];
}
	

@end
rabbigarfinkel is offline   Reply With Quote
Old 01-11-2011, 12:35 PM   #2 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 651
kapps11 is on a distinguished road
Default

woooowww oops i didnt see that your code scrolled down lol. Is your game supposed to be controller by changing orientation? im confused

Last edited by kapps11; 01-11-2011 at 12:37 PM.
kapps11 is offline   Reply With Quote
Reply

Bookmarks

Tags
collision, iphone dodge, landscape, portrait, sprite

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: 363
9 members and 354 guests
7twenty7, blueorb, iAppDeveloper, iGamesDev, Mah6447, Morrisone, mottdog, sacha1996, Touchmint
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,667
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, host number one
Powered by vBadvanced CMPS v3.1.0

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