 |
|
 |
|
 |
01-06-2010, 07:41 PM
|
#26 (permalink)
|
|
Senior Member
iPhone Dev SDK Supporter
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,070
|
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.
|
|
|
01-06-2010, 07:50 PM
|
#27 (permalink)
|
|
Registered Member
Join Date: Sep 2009
Posts: 50
|
Quote:
Originally Posted by smasher
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.
|
|
|
01-06-2010, 08:10 PM
|
#28 (permalink)
|
|
Senior Member
iPhone Dev SDK Supporter
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,070
|
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.
|
|
|
01-06-2010, 08:59 PM
|
#29 (permalink)
|
|
Registered Member
Join Date: Sep 2009
Posts: 50
|
Quote:
Originally Posted by smasher
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
|
|
|
01-06-2010, 10:37 PM
|
#30 (permalink)
|
|
Senior Member
iPhone Dev SDK Supporter
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,070
|
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.
|
|
|
01-06-2010, 10:38 PM
|
#31 (permalink)
|
|
Registered Member
Join Date: Sep 2009
Posts: 50
|
Got it to work. You are amazing! Thank you so much! For all of the help!
|
|
|
02-06-2010, 05:20 AM
|
#32 (permalink)
|
|
Registered Member
Join Date: Nov 2009
Posts: 90
|
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
|
|
|
02-06-2010, 06:38 AM
|
#33 (permalink)
|
|
Registered Member
Join Date: Sep 2009
Posts: 50
|
Quote:
Originally Posted by Sesa
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.
|
|
|
02-06-2010, 08:00 AM
|
#34 (permalink)
|
|
Registered Member
Join Date: Nov 2009
Posts: 90
|
Its dosent work  CGRectIntersectsRect: When the falling images hit the Character nothing happens, where there should be an Alert
|
|
|
02-06-2010, 08:02 AM
|
#35 (permalink)
|
|
Registered Member
Join Date: Sep 2009
Posts: 50
|
Quote:
Originally Posted by Sesa
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.
|
|
|
02-06-2010, 11:15 AM
|
#36 (permalink)
|
|
Registered Member
Join Date: Nov 2009
Posts: 90
|
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.
|
|
|
02-06-2010, 11:23 AM
|
#37 (permalink)
|
|
Registered Member
Join Date: Sep 2009
Posts: 50
|
Quote:
Originally Posted by Sesa
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.
|
|
|
02-06-2010, 11:32 AM
|
#38 (permalink)
|
|
Registered Member
Join Date: Nov 2009
Posts: 90
|
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.
|
|
|
02-06-2010, 11:45 AM
|
#39 (permalink)
|
|
Registered Member
Join Date: Sep 2009
Posts: 50
|
Quote:
Originally Posted by Sesa
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?
|
|
|
02-06-2010, 11:49 AM
|
#40 (permalink)
|
|
Registered Member
Join Date: Nov 2009
Posts: 90
|
Quote:
Originally Posted by kdp8791
Does it crash when the two images collide?
|
NO when the app starts
|
|
|
02-08-2010, 07:28 AM
|
#41 (permalink)
|
|
Registered Member
Join Date: Nov 2009
Posts: 90
|
kdp8791, could you help me ?
|
|
|
02-08-2010, 09:27 AM
|
#42 (permalink)
|
|
Registered Member
Join Date: Aug 2009
Posts: 10
|
PLEASE HELP ME!!!!!!
Quote:
Originally Posted by kdp8791
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
|
|
|
02-10-2010, 08:04 AM
|
#43 (permalink)
|
|
Registered Member
Join Date: Sep 2009
Posts: 50
|
Quote:
Originally Posted by Sesa
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.
|
|
|
02-10-2010, 08:08 AM
|
#44 (permalink)
|
|
Registered Member
Join Date: Sep 2009
Posts: 50
|
Quote:
Originally Posted by MrGreatTutorials
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.
|
|
|
02-10-2010, 03:12 PM
|
#45 (permalink)
|
|
Registered Member
Join Date: Nov 2009
Posts: 90
|
Quote:
Originally Posted by kdp8791
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
|
|
|
02-10-2010, 03:57 PM
|
#46 (permalink)
|
|
Registered Member
Join Date: Sep 2009
Posts: 50
|
Quote:
Originally Posted by Sesa
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.
|
|
|
02-14-2010, 02:02 AM
|
#47 (permalink)
|
|
Registered Member
Join Date: Nov 2009
Posts: 90
|
double post sorry
Last edited by Sesa; 02-14-2010 at 06:48 AM.
|
|
|
02-14-2010, 02:07 AM
|
#48 (permalink)
|
|
Registered Member
Join Date: Nov 2009
Posts: 90
|
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.
|
|
|
02-15-2010, 02:11 PM
|
#49 (permalink)
|
|
Senior Member
iPhone Dev SDK Supporter
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,070
|
Quote:
Originally Posted by Sesa
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.
|
|
|
02-16-2010, 01:30 AM
|
#50 (permalink)
|
|
Registered Member
Join Date: Nov 2009
Posts: 90
|
Quote:
Originally Posted by smasher
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.
|
|
|
 |
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
» 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 |
|