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

sdkIQ for iPhone
($4.99)

Your First iPhone App
($1.99)

iPhone Code Generator
($9.99)

Dual Matches
($0.99)

Calcuccino Programmers' Calculator
($2.99)

SDKtoday
(free)

Want your application or service advertised on iPhone Dev SDK?

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

Reply
 
LinkBack Thread Tools Display Modes
Old 01-06-2010, 07:41 PM   #26 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,070
Default

I could go up by fours if flakeCounter is declared as a pointer - int* instead of int.

Otherwise... do you move or remove the rock after you find a collision? If not, then you may have the same collision again on the next frame and your flakeCounter will increase very quickly, maybe too quickly for you to see.
__________________
~~
-- Available Now: Dead Panic, a strategic zombie shooter!(iPhone)
-- New Blog Post: A Simple Observer Pattern for iPhone / Cocoa Games
smasher is offline   Reply With Quote
Old 01-06-2010, 07:50 PM   #27 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 50
Default

Quote:
Originally Posted by smasher View Post
I could go up by fours if flakeCounter is declared as a pointer - int* instead of int.

Otherwise... do you move or remove the rock after you find a collision? If not, then you may have the same collision again on the next frame and your flakeCounter will increase very quickly, maybe too quickly for you to see.
That actually maybe the case. I am not removing the image after it has collided with the other image. How would I just remove that ONE image and not all of them.
__________________
kdp8791 is offline   Reply With Quote
Old 01-06-2010, 08:10 PM   #28 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,070
Default

You only have one rock, myRock, right? you could remove it with [myRock removeFromSuperview] .

You probably just want to move it back to the top of the screen though, since you only have the one, right? If you remove it you have no rock on the screen.
__________________
~~
-- Available Now: Dead Panic, a strategic zombie shooter!(iPhone)
-- New Blog Post: A Simple Observer Pattern for iPhone / Cocoa Games
smasher is offline   Reply With Quote
Old 01-06-2010, 08:59 PM   #29 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 50
Default

Quote:
Originally Posted by smasher View Post
You only have one rock, myRock, right? you could remove it with [myRock removeFromSuperview] .

You probably just want to move it back to the top of the screen though, since you only have the one, right? If you remove it you have no rock on the screen.
Thats right. I only have one myRock on the screen, when one image floats and disappears another appears. If I add [myRock removeFromSuperview]; to the if(CGRectIntersectsRect) then when the images collide I get a EXC_BAD_ACCESS
__________________
kdp8791 is offline   Reply With Quote
Old 01-06-2010, 10:37 PM   #30 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,070
Default

Right - if you remove the rock and no one is retaining it, it'll get dealloc'ed. Calling a method on a destroyed object will usually give you an EXC_BAD_ACCESS. That's why I said you might want to move it to the top instead.

If you still want to remove it, do this:

Code:
[myRock removeFromSuperview];
myRock=nil;
Now myRock points to nil, and you can call any methods you want - it'll have no effect and will always return zero. Or can check if (myRock==nil) to see if the rock exists or not before taking an action.
__________________
~~
-- Available Now: Dead Panic, a strategic zombie shooter!(iPhone)
-- New Blog Post: A Simple Observer Pattern for iPhone / Cocoa Games
smasher is offline   Reply With Quote
Old 01-06-2010, 10:38 PM   #31 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 50
Default

Got it to work. You are amazing! Thank you so much! For all of the help!
__________________
kdp8791 is offline   Reply With Quote
Old 02-06-2010, 05:20 AM   #32 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 90
Default

How can i just, make a collision with a character, who is moved by the accelerometer, and the fallingimages? Should i use CGRectInterSectsRect?

Thanks in advance

/Sesa
Sesa is offline   Reply With Quote
Old 02-06-2010, 06:38 AM   #33 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 50
Default

Quote:
Originally Posted by Sesa View Post
How can i just, make a collision with a character, who is moved by the accelerometer, and the fallingimages? Should i use CGRectInterSectsRect?

Thanks in advance

/Sesa
Yes, use CGRectIntersectsRect.
__________________
kdp8791 is offline   Reply With Quote
Old 02-06-2010, 08:00 AM   #34 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 90
Default

Its dosent work CGRectIntersectsRect: When the falling images hit the Character nothing happens, where there should be an Alert
Sesa is offline   Reply With Quote
Old 02-06-2010, 08:02 AM   #35 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 50
Default

Quote:
Originally Posted by Sesa View Post
Its dosent work CGRectIntersectsRect: When the falling images hit the Character nothing happens, where there should be an Alert
Can you post some code, so I can take a look.
__________________
kdp8791 is offline   Reply With Quote
Old 02-06-2010, 11:15 AM   #36 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 90
Default

Yir, here

Code:
-(void)CheckCollision {
	
		if (CGRectIntersectsRect(flakeImage.frame, Elephant.frame)) {
			alert = [[UIAlertView alloc] initWithTitle:@"Enter Some Text" message:@"" delegate:self cancelButtonTitle:@"Done" otherButtonTitles:nil];
			
		}
	
}

Its says: request for member 'frame' in something not a structure or union

Last edited by Sesa; 02-06-2010 at 11:18 AM.
Sesa is offline   Reply With Quote
Old 02-06-2010, 11:23 AM   #37 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 50
Default

Quote:
Originally Posted by Sesa View Post
Yir, here

Code:
-(void)CheckCollision {
	
		if (CGRectIntersectsRect(flakeImage.frame, Elephant.frame)) {
			alert = [[UIAlertView alloc] initWithTitle:@"Enter Some Text" message:@"" delegate:self cancelButtonTitle:@"Done" otherButtonTitles:nil];
			
		}
	
}

Its says: request for member 'frame' in something not a structure or union
I am assuming that your code when you create the Image is similar to this:

Code:
		UIImage *meteorImage = [UIImage imageNamed:@"flake.png"];
		
		myRock= [[UIImageView alloc] initWithImage: meteorImage];
With obviously more to it. It looks as though you are actually using the UIImage, rather than the UIImageView. That is the cause of your error. Hopefully replacing that will get rid of the error. Try building and running that, when the two images collide and you still have problems. Just reply and let me know.
__________________
kdp8791 is offline   Reply With Quote
Old 02-06-2010, 11:32 AM   #38 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 90
Default

Ohh, okay,
i will try

EDIT: if i use UIImageView instead of UIImage, my app crashes


flakeImage = [UIImage imageNamed:@"STone.png"];

UIImageView* flakeView = [[UIImageView alloc] initWithImage:flakeImage];

Last edited by Sesa; 02-06-2010 at 11:43 AM.
Sesa is offline   Reply With Quote
Old 02-06-2010, 11:45 AM   #39 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 50
Default

Quote:
Originally Posted by Sesa View Post
Ohh, okay,
i will try

EDIT: if i use UIImageView instead of UIImage, my app crashes


flakeImage = [UIImage imageNamed:@"STone.png"];

UIImageView* flakeView = [[UIImageView alloc] initWithImage:flakeImage];
Does it crash when the two images collide?
__________________
kdp8791 is offline   Reply With Quote
Old 02-06-2010, 11:49 AM   #40 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 90
Default

Quote:
Originally Posted by kdp8791 View Post
Does it crash when the two images collide?
NO when the app starts
Sesa is offline   Reply With Quote
Old 02-08-2010, 07:28 AM   #41 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 90
Default

kdp8791, could you help me ?
Sesa is offline   Reply With Quote
Old 02-08-2010, 09:27 AM   #42 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 10
Exclamation PLEASE HELP ME!!!!!!

Quote:
Originally Posted by kdp8791 View Post
I am trying to setup a collision between the falling image and another image. I have setup the following way, but for some reason, instead of going up by 1. It's going up by different amounts like 5, 13, 4, 10, etc...for example If one image touches the other it may go 5 then it will go to 18 if touched again. But according to the code it should only go up by one.

Code:
-(void)checkcollision {
if(CGRectIntersectsRect(floatingball.frame, myRock.frame)) {
		
		flakeCounter = flakeCounter + 1;
		flakeInt.text = [NSString stringWithFormat:@"%d", flakeCounter];
		
	}
}
Hey Im, Kristers and i need to make images fall from top to bottom in iPhone SDK and also collide! I saw your code but i cant understand it, can somebody please send me source code for this app that has got images falling from top to bottom and also collision working? PLEASE!

Thanks, Kristers
MrGreatTutorials is offline   Reply With Quote
Old 02-10-2010, 08:04 AM   #43 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 50
Default

Quote:
Originally Posted by Sesa View Post
kdp8791, could you help me ?
Yes, I will help. Sorry for the late response. I had been very busy. Were you able to figure out anything else.
__________________
kdp8791 is offline   Reply With Quote
Old 02-10-2010, 08:08 AM   #44 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 50
Default

Quote:
Originally Posted by MrGreatTutorials View Post
Hey Im, Kristers and i need to make images fall from top to bottom in iPhone SDK and also collide! I saw your code but i cant understand it, can somebody please send me source code for this app that has got images falling from top to bottom and also collision working? PLEASE!

Thanks, Kristers
Hi Kristers,
Actually the code you posted is for detecting collision not creating the images and making them fall. I would read through this 2 page read as it has all of the information you could need about making images randomly fall from top to bottom. The code that you replied to again is when the 2 images collide than flakeCounter which is and NSTimer goes up 1 point. The next line of code in that would be an NSString which is tied to a label, so the user can see the points go up.
__________________
kdp8791 is offline   Reply With Quote
Old 02-10-2010, 03:12 PM   #45 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 90
Default

Quote:
Originally Posted by kdp8791 View Post
Yes, I will help. Sorry for the late response. I had been very busy. Were you able to figure out anything else.

No, i dont know what is wrong, my app crashes when i start it
Sesa is offline   Reply With Quote
Old 02-10-2010, 03:57 PM   #46 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 50
Default

Quote:
Originally Posted by Sesa View Post
No, i dont know what is wrong, my app crashes when i start it
I just send you a P.M. please take a look at it.
__________________
kdp8791 is offline   Reply With Quote
Old 02-14-2010, 02:02 AM   #47 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 90
Default

double post sorry

Last edited by Sesa; 02-14-2010 at 06:48 AM.
Sesa is offline   Reply With Quote
Old 02-14-2010, 02:07 AM   #48 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 90
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
Old 02-15-2010, 02:11 PM   #49 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,070
Default

Quote:
Originally Posted by Sesa View Post
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.
You're using beginAnimations/commitAnimations to animation an image from the top of the screen to the bottom, right? And then you're checking for collisions once, right after you start the animation.

In my experience CGRectIntersectsRect and beginAnimations/commitAnimations do not mix well. flakeImage.frame will return the endpoint of the animation, not the real current position of the flake. I would pick a reasonable framerate like 15 or 20 FPS and update the positions yourself, without using beginAnimations/commitAnimations.

I also don't see an array of flakes. CheckCollision needs to loop through the array of flakes and check each one against the elephant.
__________________
~~
-- Available Now: Dead Panic, a strategic zombie shooter!(iPhone)
-- New Blog Post: A Simple Observer Pattern for iPhone / Cocoa Games
smasher is offline   Reply With Quote
Old 02-16-2010, 01:30 AM   #50 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 90
Default

Quote:
Originally Posted by smasher View Post
You're using beginAnimations/commitAnimations to animation an image from the top of the screen to the bottom, right? And then you're checking for collisions once, right after you start the animation.

In my experience CGRectIntersectsRect and beginAnimations/commitAnimations do not mix well. flakeImage.frame will return the endpoint of the animation, not the real current position of the flake. I would pick a reasonable framerate like 15 or 20 FPS and update the positions yourself, without using beginAnimations/commitAnimations.

I also don't see an array of flakes. CheckCollision needs to loop through the array of flakes and check each one against the elephant.

Code:
Ohh, Okay, This is my new code, but it dosent work either :( 

@synthesize myRock, meteorArray; 

-(void)viewDidLoad {
	
	
	[self createMeteors];
	
	[NSTimer scheduledTimerWithTimeInterval:(0.1) target:self selector:@selector(moveMeteors) userInfo:nil repeats:YES];
	
	[super viewDidLoad];
}


-(void)createMeteors{
	
    if (meteorArray==nil){
		meteorArray = [[NSMutableArray alloc] init];
    }
	
    UIImage *meteorImage = [UIImage imageNamed:@"STone.png"];
    UIImageView *newView;
	
    for (int i = 0; i< 3; i++){  //creates meteors
        newView= [[UIImageView alloc] initWithImage: meteorImage];
		
        //pick a position *above* the top of the screen
		int x = arc4random()%320; 
        int y = -arc4random()%480;
		double scale = 1 / round(random() % 100) + 1.0;
		double speed = 1 / round(random() % 100) + 1.0;
        newView.center = CGPointMake (x,y);
		
		
		
        [self.view addSubview: newView]; //adds meteors as subviews
        [meteorArray addObject: newView]; //add to array
        [newView release];
    }
	
}

-(void)moveMeteors{
	
	for (UIImageView* myRock in meteorArray ){
		
		//move new center down
		CGPoint newCenter = myRock.center;
		newCenter.y = newCenter.y +10;
		
		if (newCenter.y > 480){ // if off the bottom
			newCenter.y=0;   //pop to top
			newCenter.x = arc4random()%320;  //and pick new x position
		}
		
		myRock.center = newCenter;
	}
}

Last edited by Sesa; 02-17-2010 at 12:43 AM.
Sesa 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: 362
22 members and 340 guests
AdamSubach, anonymous@, benoitr007, bensj, Duncan C, gtyt38, Jeremy1026, lifeCoder45, maxus182, mox, Mr. Mojo Risin, Olesesy, Ovidius, Paul10, pofak, raheel, squidboy, ufbobbo, ultrayard077
Most users ever online was 965, 06-30-2010 at 04:26 AM.
» Stats
Members: 41,859
Threads: 49,768
Posts: 213,052
Top Poster: BrianSlick (3,138)
Welcome to our newest member, ultrayard077
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:49 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0