开发者

how to zoom sprites when pinching in cocos2d?

I am working on an application where i have to display 50 to 70 images(sprites).I have make a scroll which will scroll all these images but i also want to zoom these images as well I have been following "http://ganbarugames.com/2010/12/detecting-touch-events-in-cocos2d-iphone/" this tutorial.I want exactly the same thing but that tutorial has make that zoom thing for only a single sprite but i want that on every sprite what should i do? please help me out?

-(void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
  {
    CGPoint touchPoint = [touch locationInView:[touch view]];

    touchPoint = [[CCDirector sharedDirector]conv开发者_JAVA技巧ertToGL:touchPoint];
   self.position = ccp((-(currentScreen-1)*scrollWidth)+(touchPoint.x-startSwipe),0);

--> NSArray *touchArray = [touches allObjects];
if([touchArray count] > 1)
{
    UITouch *fingerOne = [touchArray objectAtIndex:0];
    UITouch *fingerTwo = [touchArray objectAtIndex:1];

    CGPoint pointOne = [fingerOne locationInView:[fingerOne view]];
    CGPoint pointTwo = [fingerTwo locationInView:[fingerTwo view]];

pointOne = [[CCDirector sharedDirector] convertToGL:pointOne];
    pointTwo = [[CCDirector sharedDirector] convertToGL:pointTwo];

    float distance = sqrt(pow(pointOne.x - pointTwo.x, 2.0) + pow(pointOne.y - pointTwo.y, 2.0));

    float scale = distance / [CCDirector sharedDirector].winSize.width * 5;
    [backgroundScroll setScale:scale];
}

} here's the code i am using but it gives me warning at the arrow mentioned in the code that 'UITouch' may not respond to -allObjects'

please help me out........


finaly I got the solution and here it goes

UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(makepinch:)];  

[[[CCDirector sharedDirector] openGLView] addGestureRecognizer:pinch];

put his code in your init method and then add

-(void)makepinch:(UIPinchGestureRecognizer*)pinch
{
    if(pinch.state == UIGestureRecognizerStateEnded)
    {

        currentScale = pinch.scale;
    }
    else if(pinch.state == UIGestureRecognizerStateBegan && currentScale != 0.0f)
    {

        pinch.scale = currentScale;
    }
    if(pinch.scale != NAN && pinch.scale != 0.0)
    {
        pinch.view.transform = CGAffineTransformMakeScale(pinch.scale, pinch.scale);
    }
}


To do zooming on the games I work on, I use a combination of a a class to capture the pinch input and a class to treat the screen as a viewport. The real "secret sauce" here is the viewport, though.

You need to treat the screen as if it is a "box in in a box" and apply a linear transformations to map between the pixel space of the screen and the "game space" of the objects in your game. I usually use Box2D under the game, so the game is all in "meters".

A viewport works like the diagram below:

how to zoom sprites when pinching in cocos2d?

The general method for mapping the world space limits (Wxmin, Wxmax) onto the screen coordinates (0,Sxmax) is done by a linear mapping with a y = mx + b formulation. Given the two known points for the transformation:

Wxmin (meters) maps onto (pixel) 0 and Wxmax (meters) maps onto (pixel) Sxmax Solving y0 = mx0 + b and y1 = mx1 + b1 yields:

m = Sxmax/(Wxmax – Wxmin) and b = -Wxmin*Sxmax/(Wxmax – Wxmin) (= -m * Wxmin) We replace (Wxmax – Wxmin) with scale*(Wxmax-Wxmin) for the x dimension and scale*(Wymax-Wymin)/aspectRatio in the y dimension.

The value (Wxmax – Wxmin) = scale*worldSizeMeters (xDimension)

The value Wxmin = viewport center – 1/2 the width of the viewport

etc.

In Code:

void Viewport::CalculateViewport()
{
   // Bottom Left and Top Right of the viewport
   _vSizeMeters.width = _vScale*_worldSizeMeters.width;
   _vSizeMeters.height = _vScale*_worldSizeMeters.height/_aspectRatio;

   _vBottomLeftMeters.x = _vCenterMeters.x - _vSizeMeters.width/2;
   _vBottomLeftMeters.y = _vCenterMeters.y - _vSizeMeters.height/2;
   _vTopRightMeters.x = _vCenterMeters.x + _vSizeMeters.width/2;
   _vTopRightMeters.y = _vCenterMeters.y + _vSizeMeters.height/2;

   // Scale from Pixels/Meters
   _vScalePixelToMeter.x = _screenSizePixels.width/(_vSizeMeters.width);
   _vScalePixelToMeter.y = _screenSizePixels.height/(_vSizeMeters.height);

   // Offset based on the screen center.
   _vOffsetPixels.x = -_vScalePixelToMeter.x * (_vCenterMeters.x - _vScale*_worldSizeMeters.width/2);
   _vOffsetPixels.y = -_vScalePixelToMeter.y * (_vCenterMeters.y - _vScale*_worldSizeMeters.height/2/_aspectRatio);

   _ptmRatio = _screenSizePixels.width/_vSizeMeters.width;

   Notifier::Instance().Notify(Notifier::NE_VIEWPORT_CHANGED);

}

The Viewport class itself provides methods for calculating points from one space to the other and also calculating the ptmRatio (ratio of pixels to meters), which is what maps the size of something in screen space (pixels) to something in the game space (meters). This allows you to dynamically change the size of your sprites as you zoom in/out.

There is a large article describing this here as well as code on github.

Was this helpful?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜