简体   繁体   English

缩放和平移2D cocos2d iphone游戏的最佳方式

[英]Best way to zoom and pan a 2D cocos2d iphone game

What's the best way to implement a pan-able zoomable game map with Cocos2d for iPhone. 使用Cocos2d for iPhone实现可扩展的可缩放游戏地图的最佳方法是什么。 It would be like Mobile Safari except with a game map that doesn't fit completely on screen. 它会像Mobile Safari一样,但游戏地图不完全适合屏幕。 Or like how it's done with Angry Birds, Cut the rope, etc. 或者喜欢它如何用愤怒的小鸟,剪绳等。

This would not be a side scrolling game, just a fixed game area that you can zoom in/out of. 这不是一个侧滚动游戏,只是一个可以放大/缩小的固定游戏区域。

I'm new so if there are tutorials for this that would be great. 我是新手,如果有这方面的教程会很棒。

Thanks 谢谢

You absolutely should not follow the advice by Joao Caxaria, who seems to have unnecessarily reimplemented the entire UIGestureRecognizer API himself! 你绝对应该遵循的Joao Caxaria,谁似乎已经重新实现不必要整个UIGestureRecognizer API自己的意见!

You can simply use UIKit's available gesture recognizers and attach them to the sharedDirector's openGLView, as specified in the chosen answer in the link below. 您可以简单地使用UIKit的可用手势识别器并将它们附加到sharedDirector的openGLView,如下面链接中所选答案所述。 These allow for pan, pinch, tap etc. detection. 这些允许平移,捏合,敲击等检测。 You can even use a UILongPressGestureRecognizer to detect dragging. 您甚至可以使用UILongPressGestureRecognizer来检测拖动。

cocos2d-iOS - Gesture recognisers cocos2d-iOS - 手势识别器

if you are not going to use Physics in your game, then its very simple you will find a lot of tutorials on zoom/pan , just set your world SCALE in the touchesMoved zoom/pan code. 如果你不打算在你的游戏中使用物理,那么它非常简单你会发现很多关于缩放/平移的教程,只需在touchesMoved缩放/平移代码中设置你的世界SCALE。

but if you are going for Angry Bird like stuff, then you can use the Box2d/Chipmunk physics engine(comes with cocos2d), but for building the Physics world like Angry birds you will either have to write your own Level Editor or you can use LevelSVG(by Cocos2d people) kindof thing to visually make the world in SVG editor and Parse it to BOX2d. 但是如果你想使用愤怒的小鸟,那么你可以使用Box2d / Chipmunk物理引擎(随附cocos2d),但是为了建立物理世界,比如愤怒的小鸟,你要么必须编写自己的关卡编辑器,要么你可以使用LevelSVG(由Cocos2d人)在SVG编辑器中可视化地创建世界并将其解析为BOX2d。

As for the Zoom/pan with Physics bodies in your world, it will work as long as your Physics world is in your LAYER which you are trying to SCALE. 至于你的世界中使用物理体的缩放/平移,只要你的物理世界在你试图进行SCALE的你的LAYER中,它就会起作用。

but i ll recommend Overwriting the VISIT function of your Layer you want to Scale, and handle the Scaling by either glOrthof or glScalef 但我建议覆盖你想要缩放的图层的VISIT功能,并通过glOrthof或glScalef处理缩放

but i ll recommend Overwriting the VISIT function of your Layer you want to Scale, and handle the Scaling by either glOrthof or glScalef 但我建议覆盖你想要缩放的图层的VISIT功能,并通过glOrthof或glScalef处理缩放

I wouldn't overwrite the visit function... just add your layer to the touch dispatcher: 我不会覆盖访问功能...只需将您的图层添加到触摸调度程序:

[[CCTouchDispatcher sharedDispatcher] addStandardDelegate:self priority:0];

And implement the delegate functions: 并实现委托功能:

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;

inside this functions if you notice that the user is panning/zooming, just update the scene position / scale properties. 在此功能内部,如果您注意到用户正在平移/缩放,只需更新场景位置/比例属性即可。

To find if the user is panning or zooming, check this InputController helper class here: https://github.com/caxaria/LoopingMenu 要查找用户是否正在平移或缩放,请在此处检查此InputController帮助程序类: https//github.com/caxaria/LoopingMenu

Hope it helps, 希望能帮助到你,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM