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

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

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

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.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 08-27-2011, 01:55 AM   #1 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 80
Chris1979 is on a distinguished road
Default COCOS2D: how to add random bricks on a grid from a texture atlas?

Hey guys!

I'm trying to develop a puzzle game, kinda 3-4 in a row type of game.

I'm using the books "learning cocos2d" and the "Cocos2d for iPhone 0.99 Beginners Guide" for help since this is my first project...
...but I got stuck!

The thing is that I'm trying to get random images (bricks) from a texture atlas inside a grid(6x6) but the game crashes almost every time and when it doesn't crash... well.. then the bricks don't appear as I want them to appear (they only appear as 1/4 of the image when I use the ipad sim, and not inside the frame of the grid, and they don't appear at all when I use the iphone sim)

the game is a universal game,so it should theoretically work both on iphone and on ipad.

this is how I tried to get the random images to appear on the grid.

bricks.m
Code:
-(void)placeInGrid:(CGPoint)place pt:(int)pt pl:(int)pl{

	CCLOG(@"placeInGrid");

	int bType = arc4random() % 4;

	if(bType == pt || bType == pl){
		[self placeInGrid:place pt:pt pl:pl];
		return;
	}
	else{

		CCSpriteBatchNode * b = (CCSpriteBatchNode *)[theGame getChildByTag:kSSheet];

		mySprite = [CCSprite spriteWithBatchNode:b rect:[self setBrickType:bType]];

		[b addChild:mySprite z:1];

		self.bricksType =bType;
		[self.mySprite setPosition:place];
	}
}

-(CGRect)setBrickType:(int)brickT{

	CCLOG(@"setBrickType");

	self.bricksType = brickT;
	CGRect type; 

	switch (self.bricksType){

		case 0:
			type = CGRectMake(100,2,50,50);
			break;
		case 1:
			type =CGRectMake(100,2,50,50);
			break;
		case 2:
			type = CGRectMake(100,2,50,50);
			break;
		case 3:
			type = CGRectMake(100,2,50,50);
			break;

	}
	return type;
}
gameplaylayer.h
Code:
Bricks *grid[6][6];
gameplaylayer.m
Code:
#define GRID_WIDTH 8
#define GRID_HEIGHT 7
#define GRID_OFFSET ccp(155,65)

-(id)init {
    self = [super init];
    if (self != nil) {            

        // enable touches
        self.isTouchEnabled = YES;                                

        CCSpriteBatchNode *SpriteBatchNode;

        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { 

			CCLOG(@"UIUserInterfaceIdiomPad");
            [[CCSpriteFrameCache sharedSpriteFrameCache]
             addSpriteFramesWithFile:@"bricksiPad.plist"];
            SpriteBatchNode =
			[CCSpriteBatchNode batchNodeWithFile:@"bricksiPad.png"];
        } else {
			CCLOG(@"UIUserInterfaceIdiomPhone");
            [[CCSpriteFrameCache sharedSpriteFrameCache]
             addSpriteFramesWithFile:@"bricksiPhone.plist"];
            SpriteBatchNode =
            [CCSpriteBatchNode batchNodeWithFile:@"bricksiPhone.png"];
        }

        [self addChild:SpriteBatchNode z:1 tag:kSSheet];                    

		for(int i =0; i< GRID_WIDTH ; i++) {
			for(int j =0; j< GRID_HEIGHT ; j++) {
				Bricks * b = [[Bricks alloc]initWithGame:self];
				grid[i][j] = b;
				[b release];
			}
		}
		[self placeBricks];
	}
    return self;                                                   

}

-(void)placeBricks{
	CCLOG(@"placeBricks");
	for(int i =0; i< GRID_WIDTH ; i++)
	{
		for(int j =0; j< GRID_HEIGHT ; j++)
		{
			Bricks * leftB = nil;
			Bricks *leftmostB= nil;
			Bricks * topB= nil;
			Bricks *topmostB= nil;
			int prohibitedLeft = -1, prohibitedTop = -1;
			if(i>=2)
			{
				leftB = (Bricks *)grid[i-1][j];
				leftmostB = (Bricks *)grid[i-2][j];
			}
			if(j>=2)
			{
				topB = (Bricks *)grid[i][j-1];
				topmostB = (Bricks *)grid[i][j-2];
			}
			if(leftB && leftmostB && leftB.bricksType == leftmostB.bricksType)
			{
				prohibitedLeft = leftB.bricksType;
			}
			if(topB && topmostB && topB.bricksType == topmostB.bricksType)
			{
				prohibitedTop = topB.bricksType;
			}
			[grid[i][j] placeInGrid:ccp(42*i + GRID_OFFSET.x,42*j + GRID_OFFSET.y) pt:prohibitedTop pl:prohibitedLeft];

		}
	}
}
can anyone help me to solve this?

I would appreciate it a lot!
Chris1979 is offline   Reply With Quote
Old 08-27-2011, 06:50 AM   #2 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 80
Chris1979 is on a distinguished road
Default

Quote:
Originally Posted by Chris1979 View Post
Hey guys!

I'm trying to develop a puzzle game, kinda 3-4 in a row type of game.

I'm using the books "learning cocos2d" and the "Cocos2d for iPhone 0.99 Beginners Guide" for help since this is my first project...
...but I got stuck!

The thing is that I'm trying to get random images (bricks) from a texture atlas inside a grid(6x6) but the game crashes almost every time and when it doesn't crash... well.. then the bricks don't appear as I want them to appear (they only appear as 1/4 of the image when I use the ipad sim, and not inside the frame of the grid, and they don't appear at all when I use the iphone sim)

the game is a universal game,so it should theoretically work both on iphone and on ipad.

this is how I tried to get the random images to appear on the grid.

bricks.m
Code:
-(void)placeInGrid:(CGPoint)place pt:(int)pt pl:(int)pl{

	CCLOG(@"placeInGrid");

	int bType = arc4random() % 4;

	if(bType == pt || bType == pl){
		[self placeInGrid:place pt:pt pl:pl];
		return;
	}
	else{

		CCSpriteBatchNode * b = (CCSpriteBatchNode *)[theGame getChildByTag:kSSheet];

		mySprite = [CCSprite spriteWithBatchNode:b rect:[self setBrickType:bType]];

		[b addChild:mySprite z:1];

		self.bricksType =bType;
		[self.mySprite setPosition:place];
	}
}

-(CGRect)setBrickType:(int)brickT{

	CCLOG(@"setBrickType");

	self.bricksType = brickT;
	CGRect type; 

	switch (self.bricksType){

		case 0:
			type = CGRectMake(100,2,50,50);
			break;
		case 1:
			type =CGRectMake(100,2,50,50);
			break;
		case 2:
			type = CGRectMake(100,2,50,50);
			break;
		case 3:
			type = CGRectMake(100,2,50,50);
			break;

	}
	return type;
}
gameplaylayer.h
Code:
Bricks *grid[6][6];
gameplaylayer.m
Code:
#define GRID_WIDTH 8
#define GRID_HEIGHT 7
#define GRID_OFFSET ccp(155,65)

-(id)init {
    self = [super init];
    if (self != nil) {            

        // enable touches
        self.isTouchEnabled = YES;                                

        CCSpriteBatchNode *SpriteBatchNode;

        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { 

			CCLOG(@"UIUserInterfaceIdiomPad");
            [[CCSpriteFrameCache sharedSpriteFrameCache]
             addSpriteFramesWithFile:@"bricksiPad.plist"];
            SpriteBatchNode =
			[CCSpriteBatchNode batchNodeWithFile:@"bricksiPad.png"];
        } else {
			CCLOG(@"UIUserInterfaceIdiomPhone");
            [[CCSpriteFrameCache sharedSpriteFrameCache]
             addSpriteFramesWithFile:@"bricksiPhone.plist"];
            SpriteBatchNode =
            [CCSpriteBatchNode batchNodeWithFile:@"bricksiPhone.png"];
        }

        [self addChild:SpriteBatchNode z:1 tag:kSSheet];                    

		for(int i =0; i< GRID_WIDTH ; i++) {
			for(int j =0; j< GRID_HEIGHT ; j++) {
				Bricks * b = [[Bricks alloc]initWithGame:self];
				grid[i][j] = b;
				[b release];
			}
		}
		[self placeBricks];
	}
    return self;                                                   

}

-(void)placeBricks{
	CCLOG(@"placeBricks");
	for(int i =0; i< GRID_WIDTH ; i++)
	{
		for(int j =0; j< GRID_HEIGHT ; j++)
		{
			Bricks * leftB = nil;
			Bricks *leftmostB= nil;
			Bricks * topB= nil;
			Bricks *topmostB= nil;
			int prohibitedLeft = -1, prohibitedTop = -1;
			if(i>=2)
			{
				leftB = (Bricks *)grid[i-1][j];
				leftmostB = (Bricks *)grid[i-2][j];
			}
			if(j>=2)
			{
				topB = (Bricks *)grid[i][j-1];
				topmostB = (Bricks *)grid[i][j-2];
			}
			if(leftB && leftmostB && leftB.bricksType == leftmostB.bricksType)
			{
				prohibitedLeft = leftB.bricksType;
			}
			if(topB && topmostB && topB.bricksType == topmostB.bricksType)
			{
				prohibitedTop = topB.bricksType;
			}
			[grid[i][j] placeInGrid:ccp(42*i + GRID_OFFSET.x,42*j + GRID_OFFSET.y) pt:prohibitedTop pl:prohibitedLeft];

		}
	}
}
can anyone help me to solve this?

I would appreciate it a lot!

Never mind!

I solved it!

It was my texture atlas that was the cause of the problem

Thanks anyway!
Chris1979 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: 395
14 members and 381 guests
7twenty7, apatsufas, AppsBlogger, comicool, Creativ, Dalia, dansparrow, Duncan C, HemiMG, heshiming, LunarMoon, Murphy, pbart, Tomsky
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,127
Posts: 402,915
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

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