2

我正在尝试为通过在触摸屏上拖动以将相机拖过来移动的 android 应用程序制作相机。我正在使用 Cocos2D 引擎进行开发。

问题是,每当您在屏幕上移动手指时,屏幕上的所有内容都会消失而不是移动。

我的代码在下面,我希望有人可以帮助我:) 感谢您的宝贵时间。

@Override
public boolean ccTouchesMoved(MotionEvent event)
        {
            CGPoint location = CCDirector.sharedDirector().convertToGL(CGPoint.ccp(event.getX(), event.getY()));
            CGPoint movement = CGPoint.ccpSub(location, previousLocation);
            previousLocation = location;
            //Update the camera
            float[] x = new float[1];
            float[] y = new float[1];
            float[] z = new float[1];
            this.getCamera().getCenter(x, y, z);
            CameraPos.x = x[0];
            CameraPos.y = y[0];
            this.getCamera().getEye(x, y, z);
            movement.x = 2 * movement.x * (1 + (z[0]/832));
            movement.y = 2 * movement.y * (1 + (z[0]/832));
            CameraPos.x = CameraPos.x - Math.round(movement.x);
            CameraPos.y = CameraPos.y - Math.round(movement.y);
            this.getCamera().setCenter(CameraPos.x, CameraPos.y, 0);
            this.getCamera().setEye(CameraPos.x, CameraPos.y, z[0]);
            return true;
        }
4

1 回答 1

0

没关系,我让它工作了。

对于任何想知道这一点的人,我创建了一个名为 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;
}
于 2012-06-05T09:30:01.640 回答