Search code examples
cocos2d-iphonecocos2d-android

Cocos2D - Large Image


Is it possible to use a large image in Cocos2D, and allow, via swiping or pinching, for the user to zoom in and out?

I see from this post, that the max res for a Cocos2D image is 2048x2048. That is obviously larger than a device viewport, so I want the user to be able to move around the image.

I'm not creating a game, I'm making a sort of interactive biological cell, that will allow the user to tap arbitrary organelles, and see a popup of information about them.

Here is an idea of what the image will be, and obviously cramming the whole thing into a device viewport is not possible:

Cell

So really, before I delve too deep into this project, I'm just curious as to whether it is possible to use a large image, that allows the user the ability to arbitrarily move it around, and, if I can detect organelle touches, perhaps via CCSprites?


Solution

  • I recommend subclassing CCSprite and using your large image as the class's image. CCSprites certainly can detect touches by simply adding the basic CCTouchDispatcher delegate to the sprite's class:

    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:-1 swallowsTouches:YES];

    Then also add this method to your CCSprite subclass:

    -(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event

    You can do anything you want with the touches at this point, scroll or whatever suits your needs.

    You could break up your image into many multiple sprites and use a CCLayer to manage touches instead, it just depends on whether you really need your image to be that large, or if the limitations for a single image are enough for you to work with, considering they are pretty large too. My method here is a lot less complicated than that.