Cocos2d网格设计之画线
大家好 我在使用 cocos2d 设计带有网格的 iphone 游戏时遇到一个小问题。
游戏需要在屏幕中间有一个 10x10 的网格(它不覆盖整个屏幕)。 在运行时,用户触摸网格中的两个点,绘制一条线。
问题:tilemap 适合解决这个问题吗?因为我需要验证当用户触摸某个点时坐标是否属于网格,tilemap 会有用吗?
问题:在cocos2d中有没有更好的方法来解决这个问题?请帮帮我。
谢谢
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 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不建议为此使用tilemap。就我个人而言,我会用数学来完成这一切。
为了便于论证,我们假设您的网格方块大小为 10 像素 x 10 像素。
您现在立即知道每个正方形的矩形的位置。
右上角的方块将是 (90, 0, 10, 10),这显然不包括网格的位置,但您可以通过添加.. (90+gridPos.x, 0+gridPos.x, 0+gridPos.x) 轻松地将其添加到此上。 y, 10, 10)。
然后你只需检查你的触摸是否与网格的矩形相交。
绘制一条线相当简单,我想您会从两个网格点的中心绘制它。
因此,如果该线从右上角的网格正方形开始,则其初始点将从 (90+gridPos.x, 0+gridPos.y, 5, 5) 或 (90+gridPos.x, 0+gridPos.y, gridSquareHeight /2, gridSquareWidth/2)
使用 cocos2d 也可以很容易地使每个方块都成为可触摸的精灵,它可以在您喜欢的方式触摸时做出反应,将消息发送回委托人,甚至只是做一个视觉效果。
解决这个问题有很多可能性。
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.