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

Thread: Random Rocks
View Single Post
Old 02-14-2010, 02:07 AM   #48 (permalink)
Sesa
Registered Member
 
Join Date: Nov 2009
Posts: 91
Sesa is on a distinguished road
Default

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

Last edited by Sesa; 02-14-2010 at 02:10 AM.
Sesa is offline   Reply With Quote
 

» Advertisements
» Online Users: 738
17 members and 721 guests
7twenty7, BuzzingDanZei, chiataytuday, drckmchaels, erdinc27, Fahad.bhutta, falco1973, iAppDeveloper, iOS.Lover, MAMN84, MarkC, m_rozzi, QuantumDoja, Sophie100, t14hoangson, teebee74, tim0504
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,474
Threads: 94,040
Posts: 402,625
Top Poster: BrianSlick (7,978)
Welcome to our newest member, m_rozzi
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:11 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.