gpt4 book ai didi

java - 使用触摸输入移动相机 Cocos2D android

转载 作者:行者123 更新时间:2023-11-29 09:13:38 26 4
gpt4 key购买 nike

我正在尝试为 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;
}

最佳答案

没关系,我还是让它工作了。

对于任何想了解这一点的人,我创建了一个名为 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;
}

关于java - 使用触摸输入移动相机 Cocos2D android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10869252/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com