开发者

Cocos2d grid design for drawing lines

Hello guys I have a small problem while designing a iphone game with a grid using cocos2d.

The game needs a 10x10 grid in the middle of the screen (it is not covering the entire screen). A line is drawn at runtime where the user touches two points in the grid.

Question: would tilemap be ideal for this problem? As i need to verify the co-ordinates do belong to the grid or开发者_C百科 not when the user touches a point would tilemap be useful?

Question: Is there any better way of solving this in cocos2d. Please help me out.

Thanks


I wouldn't recommend using tilemap for this. Personally I'd do it all with math.

Lets for arguments sake say your grid squares are 10px by 10px.

You now instantly know the positions of the rects for each square.

top right square would be (90, 0, 10, 10), this obviously doesn't include the positioning of your grid, but you can easily add that onto this by adding.. (90+gridPos.x, 0+gridPos.y, 10, 10).

Then you just check your touches intersect the rects of the grids.

Drawing a line is fairly simple, i imagine you'd draw it from the center of the 2 grid points.

So if the line started in the top right grid square it's initial point would start at (90+gridPos.x, 0+gridPos.y, 5, 5), or (90+gridPos.x, 0+gridPos.y, gridSquareHeight/2, gridSquareWidth/2)

Using cocos2d it's pretty easy to also make every square a touchable sprite, that can react when touched however you like, sending a message back to a delegate or even just doing a visual effect.

There are tonnes of possibilities for solving this problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜