存档

2010年10月 的存档

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

2010年10月15日 没有评论

What’s the best way to implement a pan-able zoomable game map with Cocos2d for iPhone. It would be like Mobile Safari except with a game map that doesn’t fit completely on screen. 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

but i ll recommend Overwriting the VISIT function of your Layer you want to Scale, and handle the Scaling by either glOrthof or 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

Hope it helps,

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.

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.

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.

but i ll recommend Overwriting the VISIT function of your Layer you want to Scale, and handle the Scaling by either glOrthof or glScalef

You absolutely should not follow the advice by Joao Caxaria, who seems to have unnecessarily reimplemented the entire UIGestureRecognizer API himself!

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. These allow for pan, pinch, tap etc. detection. You can even use a UILongPressGestureRecognizer to detect dragging.

cocos2d-iOS – Gesture recognisers

分类: stackoverflow精选, unity3d 标签:

Cocos2d From iPad to iPhone

2010年10月1日 没有评论

I currently have an iPad application built entirely on Cocos2D and Chipmunk. I want to port my app to the iPhone now.

All I can find are tips on how to rebuilt your app for the iPad.
Is there a simple solution, like for say, scale down your cocos2D scene?

Does anyone have some best practice tips on this?

Sheesh…This is not really an “answer” – but I will say that I had to do this type of conversion once – and one thing that helped me was discovering that you could “change” a XIB file from an “iPhone” to “iPad” one back and forth by editing the XML. I think it is a matter of changing:

archive type="com.apple.InterfaceBuilder3.CocoaTouch.iPad.XIB"

to

<archive type="com.apple.InterfaceBuilder3.CocoaTouch.XIB"

(But I don’t remember exactly).

Well, CCNode does have a Scale property so you should be able to do something like:

if(![MyApp isIPad]){
    [myScene Scale:iPadToiPhoneScale];
}

but you shouldn’t really do stuff like this (not even sure if it would work).

You should, in your AppDelegate, check if you’re running on an iPad or an iPhone and load the correct resources (eg, previously scaled down sprites).

Then, when creating your scenes, instead of positioning your CCNodes in absolute positioning:

[back setPosition:ccp(160, 240)];

place them in relative positioning:

[back setPosition:ccp(0.5*[MyApp deviceWidth], 0.5*[MyApp deviceHeight])];

where MyApp would have a bunch of static helpers that would return the device capabilities.

Even better, why not something like:

CGPoint convertedPosition = [MyApp convertForDevice:ccp(160, 240)];
[back setPosition:convertedPosition];

Hope this helps.

分类: cocos2d, stackoverflow精选 标签: