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!