Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

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:
image

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

Thank you.
Post edited by ujn2 on

Replies

  • smashersmasher Posts: 3,858Registered Users
    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.



    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.
    [IMG]http://a5.mzstatic.com/us/r1000/042/Purple/2d/83/7d/mzm.cimuqibw.75x75-65.jpg[/IMG] <a href="http://itunes.apple.com/us/app/tiny-nightclub/id418478743?mt=8" ta
  • KennyChongKennyChong Posts: 407Registered Users
    ujn2;399950 said:
    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:
    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!
  • ujn2ujn2 Posts: 5New Users
    KennyChong;401029 said:
    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.
Sign In or Register to comment.