开发者

Is there a gridview in cocos2d of a battleship game?

开发者 https://www.devze.com 2023-04-12 01:05 出处:网络
I\'ve using cocos2d for a while and I want to make a battleship game. The thing is I can probably do a battleship with UiKit(UIButtons and UIImageView) easier and faster than in cocos2d but I want to

I've using cocos2d for a while and I want to make a battleship game. The thing is I can probably do a battleship with UiKit(UIButtons and UIImageView) easier and faster than in cocos2d but I want to take full advantage of cocos2d because I think it's better for games. The problem is that I need a grid for the battleship or something to separate the touches in quadrants. Is there something like a gridview in cocos2d? I开发者_开发问答f not I think I would have to create my own quadrants by programming? What do you think is the best method? Thanks a lot Carlos Vargas


There's not a base class in Cocos2d to do that, but you could easily make a Class specifically designed to handle touches, and mapping them to the correct quadrants.

So if you have a 480x320 screen, and quadrant size is 32, you can get the correct quadrant for a touch like:

With a configuration like this you would have 480/32 = 15 , 320/32 = 10, 10*15 = 150; a 150 quadrants grid.

e.g: To get the quadrant for a touch

// Defined the Quadrant size for your grid
CGPoint quadrantSize = CGPointMake(32.0, 32.0)

// Obtain the quadrant X, Y coordinates for a user touch (assume touchPoint is CGPoint)
int quadrant_x = (int)ceilf(touchPoint.x/quadrantSize.x);
int quadrant_y = (int)ceilf(touchPoint.y/quadrantSize.y);

// Access a Quadrant
quadrantArray[quadrant_x][quadrant_y].touched = YES;
0

精彩评论

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