can anyone please help me with this Collision code: i want to make a collision between the elephant and the flakeImage, but it dosent work.
Code:
//
// RainViewController.m
// Rain
//
// Created by Unknowen on 16/01/10.
// Copyright __MyCompanyName__ 2010. All rights reserved.
//
#import "RainViewController.h"
@implementation RainViewController
@synthesize flakeImage;
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
[[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1/40.0)];
[[UIAccelerometer sharedAccelerometer] setDelegate:self];
Elephant.image = [UIImage imageNamed:@"lemmling_Cartoon_elephant.png"];
[self.view addSubview:Elephant];
// load our flake image we will use the same image over and over
flakeImage = [UIImage imageNamed:@"STone.png"];
// start a timet that will fire 20 times per second
[NSTimer scheduledTimerWithTimeInterval:(2) target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
}
- (void)onTimer
{
// build a view from our flake image
UIImageView* flakeView = [[UIImageView alloc] initWithImage:flakeImage];
// use the random() function to randomize up our flake attributes
int startX = round(random() % 320);
int endX = round(random() % 320);
double scale = 1 / round(random() % 100) + 1.0;
double speed = 1 / round(random() % 100) + 1.0;
// set the flake start position
flakeView.frame = CGRectMake(startX, -100.0, 70.0 * scale, 70.0 * scale);
flakeView.alpha = 1.0;
// put the flake in our main view
[self.view addSubview:flakeView];
[UIView beginAnimations:nil context:flakeView];
// set up how fast the flake will fall
[UIView setAnimationDuration:10 * speed];
// set the postion where flake will move to
flakeView.frame = CGRectMake(endX, 500.0, 70.0 * scale, 70.0 * scale);
// set a stop callback so we can cleanup the flake when it reaches the
// end of its animation
[UIView setAnimationDidStopSelector:@selector(onAnimationComplete:finished:context:)];
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
[self CheckCollision];
}
-(void)CheckCollision {
if (CGRectIntersectsRect(flakeImage.frame, Elephant.frame)) {
UIAlertView *Alert = [[UIAlertView alloc] initWithTitle:@"Game Over" message:@"GANE OVER" delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil];
[Alert show];
[Alert release]; //Collision Help me please
}
}
- (void)onAnimationComplete:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
UIImageView *flakeView = context;
[flakeView removeFromSuperview];
// open the debug log and you will see that all flakes have a retain count
// of 1 at this point so we know the release below will keep our memory
// usage in check
NSLog([NSString stringWithFormat:@"[flakeView retainCount] = %d", [flakeView retainCount]]);
[flakeView release];
}
- (void)moveBoxWithY:(float)yAmount
{
CGPoint boxCenter = Elephant.center;
boxCenter.x += yAmount;
if (boxCenter.x < 100.0)
boxCenter.x = 100.0;
if (boxCenter.x > 22.0)
boxCenter.x = 22.0;
Elephant.center = boxCenter;
}
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
float sensitivity = 25.0f;
float yDistance = acceleration.y *sensitivity;
[self moveBoxWithY:yDistance];
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
- (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 {
[flakeImage release];
[super dealloc];
}
@end
//
// RainViewController.h
// Rain
//
// Created by Unknowen on 16/01/10.
// Copyright __MyCompanyName__ 2010. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface RainViewController : UIViewController <UIAccelerometerDelegate> {
IBOutlet UIImageView *Elephant;
UIImage* flakeImage;
}
@property (nonatomic, retain) UIImage* flakeImage;
- (void)onTimer;
- (void)onAnimationComplete:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context;
-(void)CheckCollision;
@end
//Sesa