在 cocos2d 中捏合时如何缩放精灵? [英] how to zoom sprites when pinching in cocos2d?
问题描述
我正在开发一个应用程序,我必须显示 50 到 70 个图像(精灵).我制作了一个滚动条,可以滚动所有这些图像,但我也想缩放这些图像,我一直在关注 "http://ganbarugames.com/2010/12/detecting-touch-events-in-cocos2d-iphone/" 这个教程.我想要完全相同的东西,但是那个教程只为单个精灵制作了缩放的东西,但我希望每个精灵都有什么我应该怎么做?请帮帮我?
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]convertToGL: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];
}
}这是我正在使用的代码,但它在代码中提到的箭头处警告我UITouch"可能无法响应 -allObjects'
} 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];
把他的代码放在你的init方法中,然后添加
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);
}
}
这篇关于在 cocos2d 中捏合时如何缩放精灵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!