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

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

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 11-22-2009, 11:20 AM   #1 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 81
Default Random Rocks

Hello i was wondering if any of you guys would know how to make random meteorites fall from the top of my screen to the bottom. Thanks
therealnosserman is offline   Reply With Quote
Old 11-22-2009, 12:21 PM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Thumbs up

Quote:
Originally Posted by therealnosserman View Post
Hello i was wondering if any of you guys would know how to make random meteorites fall from the top of my screen to the bottom. Thanks
Here's a partial example. You'll want to set up an NSTimer and an NSMutableArray - the NSTimer will call a method that moves all of the meteors on the screen.

When you create the meteors, you'll create them off the top of the screen - if you created them exactly at the top of the screen, then they'd all appear in one straight line.

Code:
-(void)createMeteors{

    if (meteorArray==nil){
    meteorArray = [[NSMutableArray alloc] init];
    }

    UIImage *meteorImage = [UIImage imageNamed:@"meteor.png"];
    UIImageView *newView;

    for (int i = 0; i< 10; i++){  //creates 10 meteors
        newView= [[UIImageView alloc] initWithImage: meteorImage];

        //pick a position *above* the top of the screen
        int x = arc4random()%320; 
        int y = -arc4random()%480; //negative y = off the top
        newView.center = CGPointMake (x,y);

        [self.view addSubview: newView]; //adds meteors as subviews
        [meteorArray addObject: newView]; //add to array
        [newView release];
    }

}


-(void)moveMeteors{ //TODO: NSTimer should call this method

    //TODO: loop through the array and move each meteor down 1 pixel. 

    //TODO: if y>480, move the meteor back to the top

}
Obvious improvements would be to give each meteor a random size, and to create a "Meteor" class that could have other attributes like "Speed" - then each meteor could have its own speed too.

Not compiled or tested!
__________________

Free Games!
smasher is offline   Reply With Quote
Old 11-22-2009, 01:36 PM   #3 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 81
Default What?

Well i cant really comprehend what you were saying. Could you maybe make a project that shows the uses of the code?
therealnosserman is offline   Reply With Quote
Old 11-22-2009, 02:54 PM   #4 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

Quote:
Originally Posted by therealnosserman View Post
Well i cant really comprehend what you were saying. Could you maybe make a project that shows the uses of the code?
You need to look up some things first to understand this code- NSMutableArray, NSTimer, and UIImageView would be good starters. The code I gave you would go in a view controller of some kind.

Feel free to ask questions, but I won't create the whole project. Maybe you should start with something simpler and work up to this?
__________________

Free Games!
smasher is offline   Reply With Quote
Old 12-07-2009, 11:01 PM   #5 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 126
Default

i am using this
-(void)createMeteors{

if (meteorArray==nil){
meteorArray = [[NSMutableArray alloc] init];
}

UIImage *meteorImage = [UIImage imageNamed:@"meteor.png"];
UIImageView *newView;

for (int i = 0; i< 10; i++){ //creates 10 meteors
newView= [[UIImageView alloc] initWithImage: meteorImage];

//pick a position *above* the top of the screen
int x = arc4random()%320;
int y = -arc4random()%480; //negative y = off the top
newView.center = CGPointMake (x,y);

[self.view addSubview: newView]; //adds meteors as subviews
[meteorArray addObject: newView]; //add to array
[newView release];
}

}


-(void)moveMeteors{ //TODO: NSTimer should call this method

//TODO: loop through the array and move each meteor down 1 pixel.

//TODO: if y>480, move the meteor back to the top
// HOw to make each object fall one by one i mean at a time there will be only 1 image on screen and when it goes>480 then next will appear

}
rahul7star is offline   Reply With Quote
Old 12-07-2009, 11:27 PM   #6 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

Quote:
Originally Posted by rahul7star View Post

How to make each object fall one by one - I mean at a time there will be only one image on screen and when it goes>480 then next will appear
In that case you only need one object - when it reaches the bottom of the screen you can simply set its y coordinate back to 0 (the top of the screen.) Something like this:

Code:
-(void)moveMeteors{

     //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;
}
__________________

Free Games!
smasher is offline   Reply With Quote
Old 12-07-2009, 11:50 PM   #7 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 126
Default

thanks and how to respond them on touch .. like if i am able to press those they will disappear and another will fall from top

and for collision do i need to put them in array or what
like this??
for (int count=0; count < [objectsArray count]; count++){

UIImageView *anItem = [objectsArray objectAtIndex:count];

if(CGRectIntersectsRect(anItem.frame, myCharacter.frame)){
anItem.hidden = YES;
}
}

Last edited by rahul7star; 12-08-2009 at 12:40 AM.
rahul7star is offline   Reply With Quote
Old 12-08-2009, 12:37 AM   #8 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

Quote:
Originally Posted by rahul7star View Post
thanks and how to respond them on touch .. like if i am able to press those they will disappear and another will fall from top

Your view controller needs a touchesBegan method - like so

Code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
 NSLog(@"Touch detected");

   UITouch *touch = [touches anyObject];
   CGPoint touchPosition = [touch locationInView:self]; 

   //Todo:
   //now you can check the distance from the touch to your imageView,
   //and react if it's less than a certain amount
   // or use CGRectContainsPoint(rect, point)
}
__________________

Free Games!

Last edited by smasher; 12-08-2009 at 01:54 AM. Reason: CGRectContainsPoint
smasher is offline   Reply With Quote
Old 12-08-2009, 12:46 AM   #9 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 126
Default

see i am using this for making 100 images fall form top
now i whenever user touch any one of them they will disappear

numberTaps +=1;

if (numberTaps < 100) {


UIImageView *bomb = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"smokeball.png" ]];
bomb.frame = CGRectMake(0, 0, 32, 32);// tp make small
[self addSubview: bomb];
[bomb release];


CGPoint position;
//do the stuff above to create the position
//pick a position anywhere on screen
//position.x = (arc4random() % 2) * 320; //stick to left or right side

PositionX = arc4random() % 360;
position.x = PositionX;
position.y = -30;

bomb.center = position;



[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:2.1];
[UIView setAnimationBeginsFromCurrentState:YES];
bomb.frame = CGRectMake(bomb.frame.origin.x, (bomb.frame.origin.y + 560.00), bomb.frame.size.width/2, bomb.frame.size.height/2);//w, h as original
[UIView commitAnimations];

// NOT WORKING
if (CGRectIntersectsRect(bomb.frame,wood.frame)) {
//collosion detected
NSLog(@"BOOM!");
}


in touchMove()
do i need to make an array or what for collisions
rahul7star is offline   Reply With Quote
Old 12-08-2009, 01:56 AM   #10 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

Quote:
Originally Posted by rahul7star View Post
in touchMove()
do i need to make an array or what for collisions
Yes, you'll need to loop through all of the meteors (or are they bombs?) and check each one to see if it intersects the target.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 12-08-2009, 02:02 AM   #11 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 126
Default

i did this ( i tried on both end in timer() and touchMOve()
objectsArray = [[NSMutableArray alloc] init];
[objectsArray addObject:newItem];
[self addSubview: newItem];

for (int count3=1; count3 < 70; count3++){
if(CGRectIntersectsRect(newItem.frame, wood.frame)){// touchlocation -->both not working
NSLog(@"Collision ");

newItem.hidden = YES;
}
rahul7star is offline   Reply With Quote
Old 12-08-2009, 12:45 PM   #12 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

You're changing the value of count3, but you're not using it anywhere. You need to loop through the whole array, get the object for each index, and do a collision check with that object.

Code:
for (int i = 0; i< [objectsArray count]; i++){

   UIView* currentBomb = (UIView*)[objectsArray objectAtIndex:i];

   if(CGRectIntersectsRect(currentBomb.frame, wood.frame)){
       NSLog(@"Collision ");       
   }

}
__________________

Free Games!
smasher is offline   Reply With Quote
Old 12-08-2009, 10:27 PM   #13 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 126
Default

Hi
for (int i = 0; i< [objectsArray count]; i++){

UIView* currentBomb = (UIView*)[objectsArray objectAtIndex:i];

if(CGRectIntersectsRect(currentBomb.frame, wood.frame)){
NSLog(@"Collision ");
}

}
what will be the value of my currentBomb as i am just using bomb
here is the code for 100 images falling form top defined in onTimer()

numberTaps +=1;

if (numberTaps < 100) {


bomb = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"smokeball.png" ]];
//bomb.frame = CGRectMake(0, 0, 32, 32);// tp make small




[self addSubview: bomb];
[bomb release];


CGPoint position;
//do the stuff above to create the position
//pick a position anywhere on screen
//position.x = (arc4random() % 2) * 320; //stick to left or right side

PositionX = arc4random() % 360;
position.x = PositionX;
position.y = -30;

bomb.center = position;

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:2.1];
[UIView setAnimationBeginsFromCurrentState:YES];
bomb.frame = CGRectMake(bomb.frame.origin.x, (bomb.frame.origin.y + 560.00), bomb.frame.size.width/2, bomb.frame.size.height/2);//w, h as original
[UIView commitAnimations];
rahul7star is offline   Reply With Quote
Old 12-08-2009, 10:47 PM   #14 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 126
Default

Hi
Thanks for that
for (int i = 0; i< [objectsArray1 count]; i++){

UIView* currentBomb = (UIView*)[objectsArray1 objectAtIndex:i];

if(CGRectIntersectsRect(currentBomb.frame, fireBall1.frame)){
NSLog(@"Collision ");
}

}



now my collision is working but its not when they are falling
collision is occurring at bottom (when falling image sits at bottom)
rahul7star is offline   Reply With Quote
Old 12-09-2009, 01:05 AM   #15 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 126
Default

can we calculate the value form center of image rather then origin??
int dx = player.frame.origin.x - wood.frame.origin.x;
int dy = player.frame.origin.y - wood.frame.origin.y;
rahul7star is offline   Reply With Quote
Old 12-30-2009, 04:11 PM   #16 (permalink)
Coincidental Coder
 
bachonk!'s Avatar
 
Join Date: Dec 2009
Location: Lakeville, MA
Posts: 104
Default Touches moved won't repond with timer

Quote:
Originally Posted by smasher View Post
Your view controller needs a touchesBegan method - like so

Code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
 NSLog(@"Touch detected");

   UITouch *touch = [touches anyObject];
   CGPoint touchPosition = [touch locationInView:self]; 

   //Todo:
   //now you can check the distance from the touch to your imageView,
   //and react if it's less than a certain amount
   // or use CGRectContainsPoint(rect, point)
}
I've gotten all of the above to work fine so that each view is created an moved across the screen.

however, I can't get the touchesBegan to track the motion of each view as it moves.

here's my current touchesBegan:
Code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{	
	UITouch *touch = [touches anyObject];
	
	for(UIView *aView in [self.view subviews])
	{
		CGPoint touchPosition = [touch locationInView:aView];
		if (CGRectContainsPoint(aView.frame, touchPosition) == YES)
		{
			[aView removeFromSuperview];
		}
	}
}

it loads everything but touching the screen doesn't do anything. Any help?
bachonk! is offline   Reply With Quote
Old 12-30-2009, 04:44 PM   #17 (permalink)
Coincidental Coder
 
bachonk!'s Avatar
 
Join Date: Dec 2009
Location: Lakeville, MA
Posts: 104
Default Nevermind, fixed!

Used this code and it works great:

Code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{	
	UITouch *touch = [touches anyObject];
	CGPoint touchPosition = [touch locationInView:self.view];
	for(UIView *aView in [self.view subviews])
	{
		//CGPoint touchPosition = [touch locationInView:aView];
		if (CGRectContainsPoint(aView.frame, touchPosition) == YES)
		{
			[aView removeFromSuperview];
		}
	}
}
thanks for the help above though
bachonk! is offline   Reply With Quote
Old 01-06-2010, 12:01 PM   #18 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 57
Default Problem

For some reason the image isn't falling down from the top, it just appears on the screen in random places. It doesn't move or fall down from the top. Please help. The codes I used are posted below. Any kind of help is much appreciated.

Imageshack - screenshot20100106at115

I added the following to my application in the .m file.

Code:
-(void)viewDidLoad {
[self createMeteors];
}

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

-(void)moveMeteors{
	
	//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;
}
And I added the following to my .h file

Code:
UIImageView *myRock
NSMutableArray *meteorArray;

@property(nonatomic, retain) UIImageView *myRock;
@property(nonatomic, retain) NSMutableArray *meteorArray;
__________________
kdp8791 is offline   Reply With Quote
Old 01-06-2010, 12:21 PM   #19 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 57
Default

I have also added an NSTimer as well.

Code:
[NSTimer scheduledTimerWithTimeInterval:(4) target:self selector:@selector(moveMeteors) userInfo:nil repeats:YES];
__________________
kdp8791 is offline   Reply With Quote
Old 01-06-2010, 03:31 PM   #20 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

Your moveMeteors method needs to loop through all of the meteors in the meteorArray. The post I responded to only wanted one meteor at a time.

Code:
-(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;
	}
}
__________________

Free Games!
smasher is offline   Reply With Quote
Old 01-06-2010, 03:53 PM   #21 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 57
Default

Thats what I am trying to do as well. I only want one image falling from the top at a time and when that image rolls off the screen another falls. But for some reason, when the view loads the images just appear on the screen as if they were placed there from Interface Builder in one solid location. No movement nothing. I have setup the NSTimer as well, so I am not sure what is wrong and why it's not moving.

Quote:
Originally Posted by smasher View Post
Your moveMeteors method needs to loop through all of the meteors in the meteorArray. The post I responded to only wanted one meteor at a time.

Code:
-(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;
	}
}
__________________
kdp8791 is offline   Reply With Quote
Old 01-06-2010, 04:02 PM   #22 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

Quote:
Originally Posted by kdp8791 View Post
Thats what I am trying to do as well. I only want one image falling from the top at a time and when that image rolls off the screen another falls. But for some reason, when the view loads the images just appear on the screen as if they were placed there from Interface Builder in one solid location. No movement nothing. I have setup the NSTimer as well, so I am not sure what is wrong and why it's not moving.
If you only want one meteor, you should not create the array of meteors; you should just create one meteor "myRock". I also changed it to "y = 0" - if you're only making one rock it makes sense to put it at the top of the screen, not far off the top like the other code.

Code:
-(void)createMeteors{
	
	UIImage *meteorImage = [UIImage imageNamed:@"flake.png"];
	
	myRock= [[UIImageView alloc] initWithImage: meteorImage];
	
	//pick a position *above* the top of the screen
	int x = arc4random()%320; 
	int y = 0; // place at top of the screen
	myRock.center = CGPointMake (x,y);
	
	[self.view addSubview: myRock]; //adds meteors as subviews
	[myRockrelease];	
}
__________________

Free Games!
smasher is offline   Reply With Quote
Old 01-06-2010, 04:31 PM   #23 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 57
Default

Sweet! It's working! Thank you SO MUCH. If I setup a new NSTimer is it possible to use it to delay image's from falling rather than it just fall immediately after the other disappears.

Quote:
Originally Posted by smasher View Post
If you only want one meteor, you should not create the array of meteors; you should just create one meteor "myRock". I also changed it to "y = 0" - if you're only making one rock it makes sense to put it at the top of the screen, not far off the top like the other code.

Code:
-(void)createMeteors{
	
	UIImage *meteorImage = [UIImage imageNamed:@"flake.png"];
	
	myRock= [[UIImageView alloc] initWithImage: meteorImage];
	
	//pick a position *above* the top of the screen
	int x = arc4random()%320; 
	int y = 0; // place at top of the screen
	myRock.center = CGPointMake (x,y);
	
	[self.view addSubview: myRock]; //adds meteors as subviews
	[myRockrelease];	
}
__________________
kdp8791 is offline   Reply With Quote
Old 01-06-2010, 04:47 PM   #24 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

Check the "//pop to top" line in moveMeters - if you put the new position off the top of the screen a little (like -50 or -100) there will be a delay before it appears.

You don't need more than one NSTimer in a game; you can have just one that triggers on every frame, and start other actions from that one.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 01-06-2010, 08:21 PM   #25 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 57
Default

Quote:
Originally Posted by smasher View Post
Check the "//pop to top" line in moveMeters - if you put the new position off the top of the screen a little (like -50 or -100) there will be a delay before it appears.

You don't need more than one NSTimer in a game; you can have just one that triggers on every frame, and start other actions from that one.
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];
		
	}
}
__________________
kdp8791 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: 251
23 members and 228 guests
@sandris, ADY, Dani77, diyora, FAED, fredidf, F_Bryant, HDshot, iDifferent, JamesCahall, JasonR, mer10, Oral B, prchn4christ, Rudy, smithdale87, Speed, spiderguy84, stekki, tgjorgoski, Touchmint, twerner, vigu360
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,880
Threads: 89,228
Posts: 380,755
Top Poster: BrianSlick (7,129)
Welcome to our newest member, @sandris
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:08 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0