没关系,我让它工作了。
对于任何想知道这一点的人,我创建了一个名为 CameraControls 的类来控制相机的基本功能。它还没有完成(我可能会在进行缩放功能等更改时更新代码)但是这个允许我完美地跟踪触摸输入。
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.types.CGPoint;
import org.cocos2d.types.CGSize;
public class CameraControls {
CGSize winSize = CCDirector.sharedDirector().displaySize();
CGPoint CameraPos = CGPoint.ccp(winSize.width, winSize.height);
CGPoint previousLocation;
double minX;
double maxX;
double minY;
double maxY;
public CameraControls(World world)
{
this.loadCamera(world);
}
public void setCameraLimit(float minX, float maxX, float minY, float maxY)
{
this.minX = minX;
this.maxX = maxX;
this.minY = minY;
this.maxY = maxY;
}
public void loadCamera(World world)
{
float[] x = new float[1];
float[] y = new float[1];
float[] z = new float[1];
world.getCamera().getCenter(x, y, z);
CameraPos.x = x[0];
CameraPos.y = y[0];
}
public void trackTouchMovement(CGPoint location, World world)
{
if(previousLocation == null)
{
previousLocation = location;
}
CGPoint movement = CGPoint.ccpSub(previousLocation, location);
previousLocation = location;
float[] x1 = new float[1];
float[] y1 = new float[1];
float[] z1 = new float[1];
world.getCamera().getEye(x1, y1, z1);
CameraPos.x = CameraPos.x + movement.x;
CameraPos.y = CameraPos.y + movement.y;
try
{
if(CameraPos.x >= maxX || CameraPos.x <= minX || CameraPos.y >= maxY || CameraPos.y <= minY)
{
CameraPos = CGPoint.ccpSub(CameraPos, movement);
}
}
catch (NullPointerException e)
{
System.out.println("Invalid values for camera Limits. No Limits applied.");
}
world.getCamera().setCenter(CameraPos.x, CameraPos.y, 0);
world.getCamera().setEye(CameraPos.x, CameraPos.y, z1[0]);
}
public void storePositionAsPrevious(CGPoint pos)
{
previousLocation = pos;
}
public void resetPrevious()
{
previousLocation = null;
}
}
现在我有了一个类,我只需在我的类中创建一个 CameraControls 实例,然后进行必要的配置。
CameraControls camera = new CameraControls(this);
在这种情况下,我希望我的相机可以查看的总面积是相机宽度的 3 倍,相机高度的 3 倍,所以我将相机的限制设置为相机的负宽度,宽度相机的高度,相机的负高度和相机的高度,因为相机从 (0, 0) 开始。
camera.setCameraLimit(-winSize.width, winSize.width, -winSize.height, winSize.height);
最后,我只是在 ccTouchesBegan、ccTouchesMoved 和 ccTouchesEnded 中添加必要的方法调用
@Override
public boolean ccTouchesMoved(MotionEvent event)
{
CGPoint location = CCDirector.sharedDirector().convertToGL(CGPoint.ccp(event.getX(), event.getY()));
camera.trackTouchMovement(location, this);
return true;
}
@Override
public boolean ccTouchesEnded(MotionEvent event)
{
camera.resetPrevious();
return true;
}
@Override
public boolean ccTouchesBegan(MotionEvent event)
{
CGPoint location = CCDirector.sharedDirector().convertToGL(CGPoint.ccp(event.getX(), event.getY()));
camera.storePositionAsPrevious(location);
return true;
}