我有一个我根本无法弄清楚的问题;可能是因为我没有正确的知识。
我有一张用 Tiled 制作的 TMX map 。 map 大于屏幕尺寸(图块为 32x32 像素,有 100x100 个图块)。
我想要做的是能够通过滑动屏幕来移动 map 。
我在网上查看了各种教程并检查了 paddle.m 示例,但仍然无法让它工作。
我遇到的所有教程都专注于在 map 周围移动夹紧的居中 Sprite ......
同样,我想要做的是能够通过滑动/滑动屏幕来移动 map ;就像滚动 iPod 或四处移动图片一样。
任何人都可以帮忙吗?
这是我的 ccTouchMoved 代码
-(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint touchPointMap = [touch locationInView: [touch view]];
touchPointMap = [[CCDirector sharedDirector] convertToGL: touchPointMap];
touchPointMap = [self convertToNodeSpace: touchPointMap];
CCLOG(@"Touch Point Map %lf, %lf", touchPointMap.x, touchPointMap.y);
self.position = CGPointMake(touchPointMap.x, touchPointMap.y);
}
为了说明我在使用上面的代码滑动屏幕时在屏幕上看到的问题:
似乎如果我触摸屏幕的中心, map 的左下角会跳到那个被触摸的坐标,并且会随着我的触摸移动,直到我的触摸被解除。
map 的左下角将始终移动到我开始触摸的位置。
此外,在移动 map 时,它会像疯了一样闪烁,如果移动过度,则会完全消失。
再次感谢所有,非常感谢。
最好和亲切的问候,
弘
最佳答案
我找到了问题的解决方案。
Cocos2D 社区中有一个非常聪明的人,他编写了一个 Controller ,不仅可以有机地平移,还可以放大和缩小。
Link to Controller, example and preview movie
您不需要编写 touchBegan、Moved 和 End 方法;这个 Controller 为你做这一切。
我的初始化
self.theMap = [CCTMXTiledMap tiledMapWithTMXFile: @"city_map.tmx"];
self.bgLayer = [theMap layerNamed:@"bg"];
// boundingRect is the area you wish to pan around
CGRect boundingRect = CGRectMake(0, 0, 32*50, 16*50);
theMap.anchorPoint = ccp(0,0);
[self addChild: theMap z: -1];
// _controller is declared in the @interface as an object of CCPanZoomController
_controller = [[CCPanZoomController controllerWithNode:self] retain];
_controller.boundingRect = boundingRect;
_controller.zoomOutLimit = _controller.optimalZoomOutLimit;
_controller.zoomInLimit = 2.0f;
[_controller enableWithTouchPriority:0 swallowsTouches:YES];
关于map - 在 Cocos2D 中滚动平铺 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7700538/