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 02-16-2010, 04:50 PM   #51 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

That code looks OK to me. You have an image named "STone.png" in the project, right? How does it now work - the stones don't appear, or they don't move?

Put these three logs at the end of createMeteors:
Code:
NSLog(@"meteorImage  is: %@", meteorImage);
NSLog(@"meteorArray is: %@", meteorArray);
NSLog(@"self.view  is: %@", self.view);
If any of them print null in the console, you have a problem. If meteorImage is null then you have the filename wrong.

If you post more code, put [code] [ /code] tags around it.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 02-16-2010, 05:17 PM   #52 (permalink)
Obj-C Learner
 
Join Date: Apr 2009
Location: Manchester, UK
Posts: 1,030
Send a message via MSN to ZunePod Send a message via Yahoo to ZunePod
Default

Quote:
Originally Posted by Sesa View Post
Ohh, Okay, This is my new code, but it dosent work

Code:
@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; // This should be declared in the .h file
	
    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;
	}
}
I believe this line is the problem:

Code:
UIImageView *newView;
Because then you are trying to access it from a different method, and it's not declared globally.

Have a look at what I've bolded.

EDIT: Also is moveMeteors being called from an NSTimer? Just curious.
ZunePod is offline   Reply With Quote
Old 02-17-2010, 01:44 AM   #53 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 91
Default

Code:
-(void)CheckCollision {
	
		if (CGRectIntersectsRect(myRock.frame, Elephant.frame)) {
			alert = [[UIAlertView alloc] initWithTitle:@"Enter Some Text" message:@"" delegate:self cancelButtonTitle:@"Done" otherButtonTitles:nil];
			[alert show];
[alert release];
		}
	
}
My Collision dosent work?

Last edited by Sesa; 02-17-2010 at 09:12 AM.
Sesa is offline   Reply With Quote
Old 02-17-2010, 02:24 AM   #54 (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 Sesa View Post
Code:
-(void)CheckCollision {
		if (CGRectIntersectsRect(myRock.frame, Elephant.frame)) {
			alert = [[UIAlertView alloc] initWithTitle:@"Enter Some Text" message:@"" delegate:self cancelButtonTitle:@"Done" otherButtonTitles:nil];
			[alert show];
[alert realase];
		}
}
My Collision dosent work?
You have an array of rocks; you need to loop through the array and check each rock against the elephant. Something like the loop in this post:
http://www.iphonedevsdk.com/forum/ip...tml#post150018

ZunePod: newView doesn't need to be in the .h file, because it's just a temp variable we're using to create the new views. The array meteorArray should be in the .h file, though.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 02-17-2010, 05:41 AM   #55 (permalink)
Obj-C Learner
 
Join Date: Apr 2009
Location: Manchester, UK
Posts: 1,030
Send a message via MSN to ZunePod Send a message via Yahoo to ZunePod
Default

Sesa: you spell it 'release'
ZunePod is offline   Reply With Quote
Old 02-17-2010, 09:24 AM   #56 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 91
Default

Quote:
Originally Posted by smasher View Post
You have an array of rocks; you need to loop through the array and check each rock against the elephant. Something like the loop in this post:
http://www.iphonedevsdk.com/forum/ip...tml#post150018

ZunePod: newView doesn't need to be in the .h file, because it's just a temp variable we're using to create the new views. The array meteorArray should be in the .h file, though.

I dont know what is wrong, the console dosent say Collision, when the Fallingimage hit the Elephant?

Code:
-(void)CheckCollision {
	
	for (int i = 0; i< [objectsArray count]; i++){
		
		UIView* currentBomb = (UIView*)[objectsArray objectAtIndex:i];
		
		if(CGRectIntersectsRect(currentBomb.frame, Elephant.frame)){
			NSLog(@"Collision ");
		}
		
	}
	
}

Last edited by Sesa; 02-17-2010 at 01:07 PM.
Sesa is offline   Reply With Quote
Old 02-17-2010, 11:49 AM   #57 (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 Sesa View Post
I dont konw what is wrong, the console dosn't say Collision, when the Falling image hit the Elephant?
Are you using objectsArray, or meteorArray like you had in the other code? I would log the contents of the array at the start of this method to make sure that (1) the method is really getting called and (2) objectsArray has some items in it.

What does this output?

Code:
NSLog(@"objectsArray is:",objectsArray);
__________________

Free Games!
smasher is offline   Reply With Quote
Old 02-17-2010, 02:02 PM   #58 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 91
Default

It is working now, thank you very much for the help
Sesa is offline   Reply With Quote
Old 02-23-2010, 07:35 AM   #59 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 6
Default Help

Quote:
Originally Posted by bachonk! View Post
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

Hi,

I am a newbie programmer and i saw your coding and tried in my example but its showing touchPosition undeclared error is showing. Could you tell me why this error is showing in the example.

thanks in advance
gladiator430 is offline   Reply With Quote
Old 02-23-2010, 10:41 AM   #60 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 6
Default Help

Quote:
Originally Posted by bachonk! View Post
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
Hello,

Could you send the source code how you reacting CGRectContainsPoint(aView.frame,touchPosition) == YES) when i add this in my coding it shows CGRectContainsPoint does not have reference and symbols not found...

thanks in advance
gladiator430 is offline   Reply With Quote
Old 03-03-2010, 05:59 PM   #61 (permalink)
Registered Member
 
wheli's Avatar
 
Join Date: Jan 2010
Posts: 128
Default

Hi,

I am trying to do something in my game exactly like this and having some problems implementing it....hoping you guys can help out....

In my .h file I have the following code:

within "@interface":
Code:
NSMutableArray *fruitArray;
below that I have:
Code:
@property(nonatomic,retain) NSMutableArray *fruitArray;
within my .m file, in "viewDidLoad", I have the following code:
Code:
if (fruitArray == nil) { [[NSMutableArray alloc] init];}
	UIImage *fruitImage = [UIImage imageNamed:@"banana.png"];
	UIImageView *newView;
	
	for (int i=0; i < current_level; i++)
	{
		newView = [[UIImageView alloc] initWithImage:fruitImage];
		int x = arc4random()%320;
		int y = (arc4random()%480);
		newView.center = CGPointMake(x,y);
		[self.view addSubview:newView];
		[fruitArray addObject:newView];
		[newView release];
		
	}
then within my game loop I have:

Code:
// Code for falling fruit - NOT WORKING
		for (UIImageView* tempView in fruitArray )
		{
			NSLog(@"yay");
			//move new center down
			CGPoint newCenter = tempView.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
			}
			
			tempView.center = newCenter;
		}
Since I never see "yay" in the console, I assume that my array is empty. Any ideas why this is happening?

Thanks for the help!
wheli is offline   Reply With Quote
Old 03-03-2010, 06:01 PM   #62 (permalink)
Registered Member
 
wheli's Avatar
 
Join Date: Jan 2010
Posts: 128
Default

hah! I just figured it out after posting! When the array was "nil" i wasn't assigning the blank array to "fruitArray".

Thanks anyway.
wheli is offline   Reply With Quote
Old 03-03-2010, 06:03 PM   #63 (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 wheli View Post
Since I never see "yay" in the console, I assume that my array is empty. Any ideas why this is happening?
You init an array, but you never point the pointer fruitArray at it. Try this:

Code:
if (fruitArray == nil) { fruitArray = [[NSMutableArray alloc] init];}
__________________

Free Games!
smasher is offline   Reply With Quote
Old 03-06-2010, 06:48 AM   #64 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 7
Default

Would you know how to put an individual score on a "rock" so it takes e.g:5 taps on one 'rock" to remove the rock view?

Thanks
Exaviorn is offline   Reply With Quote
Old 03-06-2010, 03:06 PM   #65 (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 Exaviorn View Post
Would you know how to put an individual score on a "rock" so it takes e.g:5 taps on one 'rock" to remove the rock view?

Thanks
At that point you can no longer have an array of UIViews - you should probably make your own "Rock" class that inherits from NSObject and has a pointer to a UIImageView, along with a counter for the score or hit points or velocity or angle or whatever else you need.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 03-07-2010, 11:09 AM   #66 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 7
Default

Quote:
Originally Posted by smasher View Post
At that point you can no longer have an array of UIViews - you should probably make your own "Rock" class that inherits from NSObject and has a pointer to a UIImageView, along with a counter for the score or hit points or velocity or angle or whatever else you need.
Could you give me a few pointers on how I would that up, and use it on a large number scale and randomness that I could use with the Random Rock example?
Exaviorn is offline   Reply With Quote
Old 03-07-2010, 12:02 PM   #67 (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 Exaviorn View Post
Could you give me a few pointers on how I would that up, and use it on a large number scale and randomness that I could use with the Random Rock example?
For info on creating your own class, see the sections on "Designing a Class Interface" and "Class Implementation" on this page: Cocoa Dev Central: Learn Objective-C

Whatever book you're using to learn objective-C must have a chapter on that too.

Once you have your own Rock class with a "view" property and whatever variables and properties you need, you can init that class:

Code:
//
newRock= [[Rock alloc] init];
rock.view.center = //whatever
[self.view addSubview: rock.view]; //adds meteors as subviews
[meteorArray addObject: rock.view]; //add to array
The init method for the Rock class can init the UIImageView, set up your other variables, choose a random position, and whatever other setup you need.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 03-07-2010, 01:38 PM   #68 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 7
Default

Quote:
Originally Posted by smasher View Post
For info on creating your own class, see the sections on "Designing a Class Interface" and "Class Implementation" on this page: Cocoa Dev Central: Learn Objective-C

Whatever book you're using to learn objective-C must have a chapter on that too.

Once you have your own Rock class with a "view" property and whatever variables and properties you need, you can init that class:

Code:
//
newRock= [[Rock alloc] init];
rock.view.center = //whatever
[self.view addSubview: rock.view]; //adds meteors as subviews
[meteorArray addObject: rock.view]; //add to array
The init method for the Rock class can init the UIImageView, set up your other variables, choose a random position, and whatever other setup you need.
whats newRock? is that the name of the class?

I'm also unsure what rock is too, looking at the code I think it might be the:
Code:
UIImage *Image = [UIImage imageNamed:@"ball.png"];
*Image in there?

Last edited by Exaviorn; 03-07-2010 at 02:01 PM.
Exaviorn is offline   Reply With Quote
Old 03-07-2010, 01:42 PM   #69 (permalink)
Obj-C Learner
 
Join Date: Apr 2009
Location: Manchester, UK
Posts: 1,030
Send a message via MSN to ZunePod Send a message via Yahoo to ZunePod
Default

I find that this implementation makes the game quite choppy.

Maybe it just me but it seems that way. That's the reason I'm rewriting in OpenGL, makes the game much smoother, and it's a damnsite more rewarding.
__________________
Will code for food
ZunePod is offline   Reply With Quote
Old 03-07-2010, 06:20 PM   #70 (permalink)
iPhone SDK learner
 
Join Date: Feb 2010
Location: Illinois, USA
Posts: 417
Default

Quote:
Originally Posted by smasher View Post
For info on creating your own class, see the sections on "Designing a Class Interface" and "Class Implementation" on this page: Cocoa Dev Central: Learn Objective-C

Whatever book you're using to learn objective-C must have a chapter on that too.

Once you have your own Rock class with a "view" property and whatever variables and properties you need, you can init that class:

Code:
//
newRock= [[Rock alloc] init];
rock.view.center = //whatever
[self.view addSubview: rock.view]; //adds meteors as subviews
[meteorArray addObject: rock.view]; //add to array
The init method for the Rock class can init the UIImageView, set up your other variables, choose a random position, and whatever other setup you need.
i feel completely lost on this whole class thing. i read those parts of the website but it did not say how to access methods in that class.
Batman is offline   Reply With Quote
Old 03-07-2010, 11:25 PM   #71 (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 ZunePod View Post
I find that this implementation makes the game quite choppy.
Using UIViews you're not going to get more than 15 or 20 FPS. I wasn't able to get a reliable 30FPS.

I still think this is a good way to make a "first game" and learn about Objective-C and UIViews. It's not a replacement for OpenGL or Cocos2D, though.

Quote:
Originally Posted by Batman View Post
i feel completely lost on this whole class thing. i read those parts of the website but it did not say how to access methods in that class.
The same way you access methods on any class - if you wrote a "move" method for your Rock class, you'd say [myRock move] . Creating classes is in no way specific to this little tutorial - any book or web site on Objective-C will teach you how to create a class and call methods.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 03-10-2010, 06:19 PM   #72 (permalink)
iPhone SDK learner
 
Join Date: Feb 2010
Location: Illinois, USA
Posts: 417
Default

figure out a much easier way to do this, by using UIImageView.tag.
Batman is offline   Reply With Quote
Old 03-11-2010, 12:31 PM   #73 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 7
Default

Quote:
Originally Posted by Batman View Post
figure out a much easier way to do this, by using UIImageView.tag.
Could you give an example?

-EDIT- Found out myself, its working well I think so far!!

Last edited by Exaviorn; 03-11-2010 at 02:41 PM.
Exaviorn is offline   Reply With Quote
Old 03-18-2010, 04:37 PM   #74 (permalink)
iPhone SDK learner
 
Join Date: Feb 2010
Location: Illinois, USA
Posts: 417
Default

Quote:
Originally Posted by gladiator430 View Post
Hello, Download my new game FruityQuiz which teaches you names of fruits in 5 different languages. Its free for limited time. Enjoy and leave a good feedback.
put this in the shameless advertising, don't just randomly post it in a thread.
Batman is offline   Reply With Quote
Old 03-19-2010, 05:33 AM   #75 (permalink)
Registered Member
 
Join Date: Oct 2009
Location: India
Posts: 11
Default @Smasher Collision Problem

@SMASHER OR ANY1ELSE "Ok soo i did this code and i have those images falling but there's a bug!I have a label that turns to 1 from 0 when that falling image collides with an image below BUT there is a bug when i starts falling down it just automatically adds 1 to my label where as i want tht 1 to add only when it collides!!Please help"
Here is the code

Quote:
-(void) move {
banana.center = CGPointMake(x, banana.center.y + 1.7);
if (banana.center.y > 480) {
[self performSelector:@selector(change)];
}

banana2.center = CGPointMake(x1, banana2.center.y + 2.0);
if (banana2.center.y > 480) {
[self performSelector:@selector(change1)];
}



banana4.center = CGPointMake(x3, banana4.center.y + 0.3);
if (banana4.center.y > 480) {
[self performSelector:@selector(change3)];
}


if(CGRectIntersectsRect(cloud.frame, banana.frame)) {


[self performSelector:@selector(change)];

mainInt += 1;
label.text = [NSString stringWithFormat:@"%d" , mainInt];
[self performSelector:@selector(UpdateINT)];


}}
iSdkDev 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: 245
18 members and 227 guests
ADY, Alsahir, beleg_1998, Dani77, diyora, iDifferent, iph_s, JamesCahall, JasonR, mer10, Monstertaco, prchn4christ, Robiwan, Rudy, smithdale87, Speed, spiderguy84, timle8n1
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:16 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0