我正在开发一个小游戏,其中一艘飞船穿过一个层(doh!),在某些情况下飞船靠近敌人,整个层在飞船上以缩放级别放大取决于船和敌人之间的距离。所有这一切都很好。
然而,主要的问题是,我如何保持变焦以飞船为中心?
目前我通过update方法控制GameLayer对象的缩放,代码如下:
-(void) prepareLayerZoomBetweenSpaceship{
CGPoint mainSpaceShipPosition = [mainSpaceShip position];
CGPoint enemySpaceShipPosition = [enemySpaceShip position];
float distance = powf(mainSpaceShipPosition.x - enemySpaceShipPosition.x, 2) + powf(mainSpaceShipPosition.y - enemySpaceShipPosition.y,2);
distance = sqrtf(distance);
/*
Distance > 250 --> no zoom
Distance < 100 --> maximum zoom
*/
float myZoomLevel = 0.5f;
if(distance < 100){ //maximum zoom in
myZoomLevel = 1.0f;
}else if(distance > 250){
myZoomLevel = 0.5f;
}else{
myZoomLevel = 1.0f - (distance-100)*0.0033f;
}
[self zoomTo:myZoomLevel];
}
-(void) zoomTo:(float)zoom {
if(zoom > 1){
zoom = 1;
}
// Set the scale.
if(self.scale != zoom){
self.scale = zoom;
}
}
基本上我的问题是:如何缩放图层并将其准确地居中在两艘船之间?我想这就像用两根手指捏缩放!