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