它很好,但我得到它的工作。
對於任何想知道這一點的人,我創建了一個名爲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攝像機的X上的寬度,和攝像機的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;
}
哦,我想我要補充一點,previousLocation是一個CGPoint,我存儲這樣我就可以制定出在改變觸摸位置。 – WingDev
Cocos2d-android不再支持。您必須使用cocos2d-x,跨平臺,使用JNI來使用C++代碼。更多信息[這裏](http://stackoverflow.com/q/10711047/1077364)。你問的問題是在cocos2d-x裏面的一個測試中解決的,看看。 – vgonisanz