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

View Single Post
Old 01-30-2010, 12:07 PM   #11 (permalink)
smasher
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

Not bad. Since createFirstBlock and addOneBlock do essentially the same thing, so you only need one of them, not both. I'd remove the loop from createFirstBlock (you don't need to loop from 0 to 0) and keep that one. Or you could redeclare it as "createBlocksint)numBlocks" and use the loop to create the desired number of blocks.

Code:
-(void)createOneBlock{
		
    if (blockArray==nil){
		blockArray = [[NSMutableArray alloc] init]; }
	
    UIImage *blockImage = [UIImage imageNamed:@"block.png"];
	UIImageView *block;
	
	block = [[UIImageView alloc] initWithImage: blockImage];
	
        int x = arc4random()%320; 
        int y = -500;
        block.center = CGPointMake (x,y);
		
        [self.view addSubview: block];
        [blockArray addObject: block];
        [block release];
}
	
-(void)moveBlocks {
	
	if(gameState == kGameStateRunning) {
		
		for (int i=0; i< [blockArray count]; i++) {
			UIImageView *block = [blockArray objectAtIndex:i];
			
			//move new center down
			CGPoint newCenter = block.center;
			newCenter.y = newCenter.y +10;
			
			if (newCenter.y > 420) {
				
				if (stoppedBlocks==nil){
					stoppedBlocks = [[NSMutableArray alloc] init];
				}
				
				newCenter.y = newCenter.y -10;
				[stoppedBlocks addObject: block];
				[self createOneBlock];
				continue;
				
			}
				block.center = newCenter; 
		}
		[blockArray removeObjectsInArray: stoppedBlocks];
	}

	else {
		if (showPlay.hidden) {
			showPlay.hidden = NO;}
		if (showInstructions.hidden) {
			showInstructions.hidden = NO;}
		if (showOpenfeint.hidden) {
			showOpenfeint.hidden = NO;}
		}
}
I changed the type of the loop so that we could add items from the array while the loop is running - you can't make changes to the array while doing the "fast enumeration" loop we had before. I also moved the blockArray removeObjectsInArray: outside the loop; we really only need to do that once after the loop is over.

I think that will do the trick. Test it out; blocks should fall, stop at the bottom of the screen, and a new block should appear.
__________________

Free Games!

Last edited by smasher; 01-30-2010 at 12:12 PM.
smasher is offline   Reply With Quote
 

» Advertisements
» Online Users: 283
14 members and 269 guests
ADY, betterlee, BrianSlick, chits12345, dcool, JohnS., Joseph Nardone, leahov, marshusensei, NSeven, Paul10, Phi, Promo Dispenser, RoryHarvey
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,877
Threads: 89,219
Posts: 380,705
Top Poster: BrianSlick (7,129)
Welcome to our newest member, peterkessler45
Powered by vBadvanced CMPS v3.1.0

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