我正在按照gamefromscratch教程学习如何使用cocos2d-x。我注意到{@ 3}}的定位问题:
这个基本的应用程序基本上是在您单击的位置在屏幕上绘制标签“ You Touched Here”。不过,无论何时单击,标签都会出现在我单击的位置上方。
在上面的屏幕截图中,我单击了原点。在输出日志中,您可以看到接触点(特别是:touch-> getLocation(),未转换)记录为(0,166),应为(0,0)。
我尝试使用其他位置函数,以及将触摸坐标转换为其他坐标类型,但是问题仍然存在。
以下是此简单应用程序的代码:
AppDelegate.h
#pragma once
#include "cocos2d.h"
class AppDelegate : private cocos2d::Application {
public:
AppDelegate();
virtual ~AppDelegate();
virtual bool applicationDidFinishLaunching();
virtual void applicationDidEnterBackground();
virtual void applicationWillEnterForeground();
};
AppDelegate.cpp
#include "AppDelegate.h"
// These header files are not used currently
//#include "HelloWorldScene.h"
//#include "GraphicsScene.h"
//#include "TouchScene.h"
#include "TouchScene2.h"
USING_NS_CC;
AppDelegate::AppDelegate() {
}
AppDelegate::~AppDelegate() {
}
bool AppDelegate::applicationDidFinishLaunching() {
auto director = Director::getInstance();
auto glView = director->getOpenGLView();
if (!glView) {
glView = GLViewImpl::create("Hello World");
glView->setFrameSize(640, 480);
director->setOpenGLView(glView);
}
auto scene = TouchScene2::createScene();
director->runWithScene(scene);
return true;
}
void AppDelegate::applicationDidEnterBackground() {
}
void AppDelegate::applicationWillEnterForeground() {
}
TouchScene2.h
#pragma once
#include "cocos2d.h"
class TouchScene2 : public cocos2d::Layer
{
public:
static cocos2d::Scene* createScene();
virtual bool init();
virtual bool onTouchBegan(cocos2d::Touch*, cocos2d::Event*);
virtual void onTouchEnded(cocos2d::Touch*, cocos2d::Event*);
virtual void onTouchMoved(cocos2d::Touch*, cocos2d::Event*);
virtual void onTouchCancelled(cocos2d::Touch*, cocos2d::Event*);
CREATE_FUNC(TouchScene2);
private:
cocos2d::Label* labelTouchInfo;
};
TouchScene2.cpp
#include "TouchScene2.h"
USING_NS_CC;
Scene* TouchScene2::createScene()
{
auto scene = Scene::create();
auto layer = TouchScene2::create();
scene->addChild(layer);
return scene;
}
bool TouchScene2::init()
{
if (!Layer::init())
{
return false;
}
labelTouchInfo = Label::createWithSystemFont("Touch or clicksomewhere to begin", "Arial", 30);
labelTouchInfo->setPosition(Vec2(
Director::getInstance()->getVisibleSize().width / 2,
Director::getInstance()->getVisibleSize().height / 2));
auto touchListener = EventListenerTouchOneByOne::create();
touchListener->onTouchBegan = CC_CALLBACK_2(TouchScene2::onTouchBegan, this);
touchListener->onTouchEnded = CC_CALLBACK_2(TouchScene2::onTouchEnded, this);
touchListener->onTouchMoved = CC_CALLBACK_2(TouchScene2::onTouchMoved, this);
touchListener->onTouchCancelled = CC_CALLBACK_2(TouchScene2::onTouchCancelled, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
this->addChild(labelTouchInfo);
return true;
}
bool TouchScene2::onTouchBegan(Touch* touch, Event* event)
{
std::stringstream output;
output << "Touch Pos: (" << touch->getLocation().x << ", " << touch- >getLocation().y << ")" << std::endl;
log(output.str().c_str());
labelTouchInfo->setPosition(touch->getLocation());
labelTouchInfo->setString("You Touched Here");
return true;
}
void TouchScene2::onTouchEnded(Touch* touch, Event* event)
{
cocos2d::log("touch ended");
}
void TouchScene2::onTouchMoved(Touch* touch, Event* event)
{
cocos2d::log("touch moved");
}
void TouchScene2::onTouchCancelled(Touch* touch, Event* event)
{
cocos2d::log("touch cancelled");
}
要指出的一件事是,我所遵循的教程已有几年历史(我相信写于2015年)。作者使用的是3.3版Beta,而我使用的是最新版3.17.1。这可能是问题的一部分吗?
而且,无论如何,如何解决此问题,以使原点应为(0,0)?
答案 0 :(得分:0)
您的TouchScene2.cpp和TouchScene2.hpp看起来不错 问题出在您设置
的AppDelegate.cpp中 glView->setFrameSize(640, 480);
director->setOpenGLView(glView);
请尝试以下代码,以代替这些代码。 您已固定FrameSize且未设置ContentScaleFactor。将其设置为Cocos2d-x示例项目中的
director->setOpenGLView(glview);
// Set the design resolution
glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::NO_BORDER);
Size frameSize = glview->getFrameSize();
vector<string> searchPath;
if (frameSize.height > mediumResource.size.height)
{
searchPath.push_back(largeResource.directory);
director->setContentScaleFactor(MIN(largeResource.size.height/designResolutionSize.height, largeResource.size.width/designResolutionSize.width));
}
// If the frame's height is larger than the height of small resource size, select medium resource.
else if (frameSize.height > smallResource.size.height)
{
searchPath.push_back(mediumResource.directory);
director->setContentScaleFactor(MIN(mediumResource.size.height/designResolutionSize.height, mediumResource.size.width/designResolutionSize.width));
}
// If the frame's height is smaller than the height of medium resource size, select small resource.
else
{
searchPath.push_back(smallResource.directory);
director->setContentScaleFactor(MIN(smallResource.size.height/designResolutionSize.height, smallResource.size.width/designResolutionSize.width));
}