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 01-07-2012, 07:59 AM   #1 (permalink)
Registered Member
 
Join Date: Jan 2012
Posts: 2
ujn2 is on a distinguished road
Default Restrict movement to grid

Hey,

I'm all new at iOS dev. I have built some simple projects with Cocos2d, just to get up to date - but still having some troubles.

I would really like to see some code that shows me how to build a grid where I can move a blocks/squares around with multitouch within that grid. Movement should be restricted to the grid. So you should not be able to move freely, but only horizontal/vertical whenever your block is aligned with the grid system. Also it would be cool if there is some collision detection so you can't move through another block.

A grid size of 4x4 e.g. 400x400pt where the block/square you move around is 100x100pt.

Here is a example image:


Anybody who have some examples or would like to help out, it doesn't matter if it's not cocos2d.

Thank you.
ujn2 is offline   Reply With Quote
Old 01-07-2012, 12:10 PM   #2 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

I wouldn't worry about what's displayed on the screen first - I'd think about what our objects are, what actions they perform, and what data access you need. Then figure out how to write those; then figure out how to display it.

It sounds like you have a Grid and some Blocks. The Grid object needs to store all of the blocks; when it's time to get touch controls working you'll want to ask questions like "what block is in position 1,1" and "what moves are available from 1,1", so the Grid object needs a way to answer that.

A simple way to so that is with a two-dimensional C array of pointers. Each grid square would have a pointer to the block in that square, or nil if there is no block.

Code:
struct gridPoint{
     int x;
     int y;
}

@interface Grid : NSObject {
    Block* gridSquares[4][4];
}

//actions
-(void)addBlock:(Block*) block;
-(bool)moveBlock:(Block*) to:(gridPoint)point //use this to make the move, if it is valid

//data access
-(Block*)getBlockAt:(gridPoint) point; find the block at a point when touching
-(bool)isValidMove:(gridPoint) forBlock:(Block*)block  //use this to find out if the move is valid after a drag or tap
@end;
Once you have these building blocks ready then you can create a scene with sprites and have it make calls into this "data model". The rules of the game will be in the data model, not your view/sprite code.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 01-11-2012, 12:24 PM   #3 (permalink)
iPhone SDK fanatics!
 
Join Date: Aug 2009
Location: Malaysia
Posts: 405
KennyChong is on a distinguished road
Default

Quote:
Originally Posted by ujn2 View Post
Hey,

I'm all new at iOS dev. I have built some simple projects with Cocos2d, just to get up to date - but still having some troubles.

I would really like to see some code that shows me how to build a grid where I can move a blocks/squares around with multitouch within that grid. Movement should be restricted to the grid. So you should not be able to move freely, but only horizontal/vertical whenever your block is aligned with the grid system. Also it would be cool if there is some collision detection so you can't move through another block.

A grid size of 4x4 e.g. 400x400pt where the block/square you move around is 100x100pt.

Here is a example image:


Anybody who have some examples or would like to help out, it doesn't matter if it's not cocos2d.

Thank you.

Is tilemap what you are looking for?
__________________
KennyChong
iPhone SDK Fanatic!
KennyChong is offline   Reply With Quote
Old 01-18-2012, 02:08 PM   #4 (permalink)
Registered Member
 
Join Date: Jan 2012
Posts: 2
ujn2 is on a distinguished road
Default

Quote:
Originally Posted by KennyChong View Post
Is tilemap what you are looking for?
First of all, sorry for the long response delay.

It's actually more like what Smasher says.

I need to build a Grid. The grid could be any size, 3x3, 9x9 or 4x4 like the image. Then I need to be able to move some Blocks in the grid, and then last check where blocks are positioned. So I think that smashers method is the way to go.

I would like to end up with a grid and some blocks that are moveable via touch and allow two blocks to move simultaneously via multitouch - also the blocks should not be able to move through each other and the movement should be restricted to the grid.

I'm coming from a C# background and all this objective-c is all new to me. I should probably write this is in C# and then convert it.
ujn2 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: 387
8 members and 379 guests
apatsufas, comicool, dansparrow, husthlj, LunarMoon, mer10, Murphy, pbart
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,677
Threads: 94,127
Posts: 402,916
Top Poster: BrianSlick (7,990)
Welcome to our newest member, husthlj
Powered by vBadvanced CMPS v3.1.0

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