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-29-2011, 01:49 PM   #1 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 80
Chris1979 is on a distinguished road
Default COCOS2D: how to get points in a grid upon touch

Hey guys!

I've some issues with detecting the X and Y coordinates in the grid upon touch

the grid is 6x7 and there are 41 bricks upon the grid

what am I forgetting?

bricks.m
Code:
- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *) event{
	
	CCLOG(@"ccTouchEnded");
	NSAssert(state == kStateGrabbed, @"Unexpected state!"); 

	CGPoint location = [touch locationInView: [touch view]]; 
	location = [[CCDirector sharedDirector] convertToGL: location];

	CCLOG(@"location.x %f", location.x);
	CCLOG(@"location.y %f", location.y);
	
	[theGame moveBrick:self Xpos: location.x Ypos: location.y];

	state = kStateUngrabbed;
}
@end
gameplaylayer.m
Code:
//-(void)moveBrick:(Bricks *)brick dir:(int)dir{
-(void)moveBrick:(Bricks *)brick Xpos: (float)xpos Ypos: (float) ypos{
	
	CCLOG(@"moveBrick");
	CCLOG(@"xpos %f", xpos);
	CCLOG(@"ypos %f", ypos);
	
	float CoordX = xpos+blankPosition.x*80;
	float CoordY = ypos+blankPosition.y*46;
	
	CCLOG(@"CoordX %f", CoordX);
	CCLOG(@"CoordY %f", CoordY);
	
	for(int i =0; i< GRID_WIDTH ; i++)
	{
		for(int j =0; j< GRID_HEIGHT ; j++)
		{
			if(grid[i][j] == brick)
			{

				
				switch ([self validMove:CoordX Yposition:CoordY]) {
					case 1:
						if(i<GRID_WIDTH-1)
						{
							CCLOG(@"rightBrick");
							grid[i][j] = grid[i+1][j];
							grid[i+1][j] = brick;
							[grid[i][j].mySprite setPosition:ccp(100*i + GRID_OFFSET.x,100*j + GRID_OFFSET.y)];
							[grid[i+1][j].mySprite setPosition:ccp(100*(i+1) + GRID_OFFSET.x,100*j + GRID_OFFSET.y)];
							//[self checkGroups:YES];
							return;
						}
						break;
					case 2:
						if(i>0)
						{
							CCLOG(@"leftBrick");
							grid[i][j] = grid[i-1][j];
							grid[i-1][j] = brick;
							[grid[i][j].mySprite setPosition:ccp(100*i + GRID_OFFSET.x,100*j + GRID_OFFSET.y)];
							[grid[i-1][j].mySprite setPosition:ccp(100*(i-1) + GRID_OFFSET.x,100*j + GRID_OFFSET.y)];
							//[self checkGroups:YES];
							return;
						}
						break;
					case 3:
						if(j<GRID_HEIGHT-1)
						{
							CCLOG(@"downBrick");
							grid[i][j] = grid[i][j+1];
							grid[i][j+1] = brick;
							[grid[i][j].mySprite setPosition:ccp(100*i + GRID_OFFSET.x,100*j + GRID_OFFSET.y)];
							[grid[i][j+1].mySprite setPosition:ccp(100*i + GRID_OFFSET.x,100*(j+1) + GRID_OFFSET.y)];
							//[self checkGroups:YES];
							return;
						}
						break;
					case 4:
						if(j>0)
						{
							CCLOG(@"upBrick");
							grid[i][j] = grid[i][j-1];
							grid[i][j-1] = brick;
							[grid[i][j].mySprite setPosition:ccp(100*i + GRID_OFFSET.x,100*j + GRID_OFFSET.y)];
							[grid[i][j-1].mySprite setPosition:ccp(100*i + GRID_OFFSET.x,100*(j-1) + GRID_OFFSET.y)];
							//[self checkGroups:YES];
							return;
						}
						break;
				}
			}
		}
	}
	
}


-(void)placeBricks{
	CCLOG(@"placeBricks");
	
	
	//empty spot in the bottom right corner of the grid
	blankPosition = CGPointMake(5, 0);
	
	for(int i =0; i< GRID_WIDTH ; i++)
	{
		for(int j =0; j< GRID_HEIGHT ; j++)
		{
			
			CGPoint orgPosition = CGPointMake(i,j);
			
			if( blankPosition.x == orgPosition.x && blankPosition.y == orgPosition.y ){
				continue; 
			}
			
			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(100*i + GRID_OFFSET.x,101*j + GRID_OFFSET.y) pt:prohibitedTop pl:prohibitedLeft];
		}
	}
}

//Valid moves: UP, DOWN, LEFT or RIGHT
-(Move) validMove:(float) brickX Yposition: (float) brickY{
	CCLOG(@"validMove");
	CCLOG(@"brickX %f", brickX);
	CCLOG(@"brickY %f", brickY);
	CCLOG(@"blankPosition.x %f", blankPosition.x);
	CCLOG(@"blankPosition.y %f", blankPosition.y);
	// blank spot above current brick 
	if( brickX == blankPosition.x && brickY == blankPosition.y+1 ){
		return UP; 
		CCLOG(@"Up");
	}
	
	// bank splot below current brick
	if( brickX == blankPosition.x && brickY == blankPosition.y-1 ){
		return DOWN; 
		CCLOG(@"Down");
	}
	
	// bank spot left of the current brick
	if( brickX == blankPosition.x+1 && brickY == blankPosition.y ){
		return LEFT;
		CCLOG(@"Left");
	}
	
	// bank spot right of the current brick
	if( brickX == blankPosition.x-1 && brickY == blankPosition.y ){
		return RIGHT; 
		CCLOG(@"Right");
	}
	
	return NONE;
}
Chris1979 is offline   Reply With Quote
Old 08-29-2011, 03:33 PM   #2 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 280
dapis is on a distinguished road
Default

You'd do well to ask this in a Cocos2d forum, rather than this one.
dapis is offline   Reply With Quote
Old 08-29-2011, 03:35 PM   #3 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 80
Chris1979 is on a distinguished road
Default

Quote:
Originally Posted by dapis View Post
You'd do well to ask this in a Cocos2d forum, rather than this one.
Already did... got no answer
Chris1979 is offline   Reply With Quote
Old 08-31-2011, 05:32 AM   #4 (permalink)
iPhone SDK fanatics!
 
Join Date: Aug 2009
Location: Malaysia
Posts: 405
KennyChong is on a distinguished road
Default

Why not make the 41 bricks are touchable objects that response to touch event? Will this work for you?
__________________
KennyChong
iPhone SDK Fanatic!
KennyChong 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: 391
14 members and 377 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