我正在与cocos2D
游戏引擎一起制作游戏,并在其加载水平时加载所有sprites
,现在因为sprites
(障碍物)的某些高于320像素,因此似乎很难检查出它们。因此,为了方便起见,我想要应用ZOOM IN
和ZOOM out
效果,该效果可以一次最小化整个等级的所有精灵,而在缩小情况下,这些效果将驻留在旧的位置。应用变焦效果在cocos2D游戏环境中?
我可以做到这一点吗?
如果是,那么如何?
请介绍一下缩放也。
我正在与cocos2D
游戏引擎一起制作游戏,并在其加载水平时加载所有sprites
,现在因为sprites
(障碍物)的某些高于320像素,因此似乎很难检查出它们。因此,为了方便起见,我想要应用ZOOM IN
和ZOOM out
效果,该效果可以一次最小化整个等级的所有精灵,而在缩小情况下,这些效果将驻留在旧的位置。应用变焦效果在cocos2D游戏环境中?
我可以做到这一点吗?
如果是,那么如何?
请介绍一下缩放也。
缩放,非常简单,只需设置主游戏图层的缩放属性...但有一些捕获。
缩放图层时,它会移动图层的位置。它不会自动缩放到目前正在查看的中心位置。如果您的游戏中有任何类型的滚动,则需要对此进行说明。
为此,请将图层的anchorPoint
设置为ccp(0.0f, 0.0f)
,然后计算图层的移动量,并相应地重新定位图层。
- (void) scale:(CGFloat) newScale scaleCenter:(CGPoint) scaleCenter {
// scaleCenter is the point to zoom to..
// If you are doing a pinch zoom, this should be the center of your pinch.
// Get the original center point.
CGPoint oldCenterPoint = ccp(scaleCenter.x * yourLayer.scale, scaleCenter.y * yourLayer.scale);
// Set the scale.
yourLayer.scale = newScale;
// Get the new center point.
CGPoint newCenterPoint = ccp(scaleCenter.x * yourLayer.scale, scaleCenter.y * yourLayer.scale);
// Then calculate the delta.
CGPoint centerPointDelta = ccpSub(oldCenterPoint, newCenterPoint);
// Now adjust your layer by the delta.
yourLayer.position = ccpAdd(yourLayer.position, centerPointDelta);
}
捏缩放比较容易...只是检测touchesMoved,然后调用你的缩放例程。
- (void) ccTouchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
// Examine allTouches instead of just touches. Touches tracks only the touch that is currently moving...
// But stationary touches still trigger a multi-touch gesture.
NSArray* allTouches = [[event allTouches] allObjects];
if ([allTouches count] == 2) {
// Get two of the touches to handle the zoom
UITouch* touchOne = [allTouches objectAtIndex:0];
UITouch* touchTwo = [allTouches objectAtIndex:1];
// Get the touches and previous touches.
CGPoint touchLocationOne = [touchOne locationInView: [touchOne view]];
CGPoint touchLocationTwo = [touchTwo locationInView: [touchTwo view]];
CGPoint previousLocationOne = [touchOne previousLocationInView: [touchOne view]];
CGPoint previousLocationTwo = [touchTwo previousLocationInView: [touchTwo view]];
// Get the distance for the current and previous touches.
CGFloat currentDistance = sqrt(
pow(touchLocationOne.x - touchLocationTwo.x, 2.0f) +
pow(touchLocationOne.y - touchLocationTwo.y, 2.0f));
CGFloat previousDistance = sqrt(
pow(previousLocationOne.x - previousLocationTwo.x, 2.0f) +
pow(previousLocationOne.y - previousLocationTwo.y, 2.0f));
// Get the delta of the distances.
CGFloat distanceDelta = currentDistance - previousDistance;
// Next, position the camera to the middle of the pinch.
// Get the middle position of the pinch.
CGPoint pinchCenter = ccpMidpoint(touchLocationOne, touchLocationTwo);
// Then, convert the screen position to node space... use your game layer to do this.
pinchCenter = [yourLayer convertToNodeSpace:pinchCenter];
// Finally, call the scale method to scale by the distanceDelta, pass in the pinch center as well.
// Also, multiply the delta by PINCH_ZOOM_MULTIPLIER to slow down the scale speed.
// A PINCH_ZOOM_MULTIPLIER of 0.005f works for me, but experiment to find one that you like.
[self scale:yourlayer.scale - (distanceDelta * PINCH_ZOOM_MULTIPLIER)
scaleCenter:pinchCenter];
}
}
如果所有的精灵都有相同的父亲,你可以缩放他们的父母,他们将与它一起缩放,保持他们相对于父母的坐标。
啊,但触摸轨迹在这种情况下失去也就是说,如果缩小已经完成,然后触摸一个精灵,我需要在精灵之后的50到100像素后触摸。:( – rptwsthi 2011-05-18 07:19:53
这个代码规模我的层由2至特定位置
[layer setScale:2];
layer.position=ccp(240/2+40,160*1.5);
double dx=(touchLocation.x*2-240);
double dy=(touchLocation.y*2-160);
layer.position=ccp(inGamePlay.position.x-dx,inGamePlay.position.y-dy);
我的代码和它比其他的要好:
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSArray* allTouches = [[event allTouches] allObjects];
CCLayer *gameField = (CCLayer *)[self getChildByTag:TAG_GAMEFIELD];
if (allTouches.count == 2) {
UIView *v = [[CCDirector sharedDirector] view];
UITouch *tOne = [allTouches objectAtIndex:0];
UITouch *tTwo = [allTouches objectAtIndex:1];
CGPoint firstTouch = [tOne locationInView:v];
CGPoint secondTouch = [tTwo locationInView:v];
CGPoint oldFirstTouch = [tOne previousLocationInView:v];
CGPoint oldSecondTouch = [tTwo previousLocationInView:v];
float oldPinchDistance = ccpDistance(oldFirstTouch, oldSecondTouch);
float newPinchDistance = ccpDistance(firstTouch, secondTouch);
float distanceDelta = newPinchDistance - oldPinchDistance;
NSLog(@"%f", distanceDelta);
CGPoint pinchCenter = ccpMidpoint(firstTouch, secondTouch);
pinchCenter = [gameField convertToNodeSpace:pinchCenter];
gameField.scale = gameField.scale - distanceDelta/100;
if (gameField.scale < 0) {
gameField.scale = 0;
}
}
}
和我的代码有什么问题?在这个页面上没有可行和完整的代码,除了我的代码。 – user2083364 2013-07-01 07:25:31
奇妙的作品,非常感谢。我一直在苦苦挣扎一段时间! +1 – stenger96 2013-12-10 03:33:03
可能会听起来很愚蠢,但我不知道如何计算[glc_ getModifiedScale],请求详细阐述它。谢谢。 – rptwsthi 2011-05-18 07:16:49
@rptwsthi对不起,那是我的错。这是我的一些代码,我没有修复,以匹配其余的。它现在应该更有意义。 – 2011-05-19 14:47:00
你能否请进一步解释你如何“将屏幕位置转换为节点空间”?我尝试过使用'[self scale:(distanceDelta/PTM_RATIO)scaleCenter:pinchCenter];''还有'[self scale:distanceDelta scaleCenter:[self convertToNodeSpace:pinchCenter]];'但是他们两个(以及两个)每当我勉强捏住屏幕时都会狂暴。 – 2012-11-28 11:36:45