【问题标题】:Cocos2d: y-coordinate for touch event greater than that of the touch positionCocos2d:触摸事件的y坐标大于触摸位置的坐标
【发布时间】:2024-01-05 11:49:01
【问题描述】:

我正在按照 gamesfromscratch 教程学习如何使用 cocos2d-x。当我注意到这个定位问题时,我来到this part

这个基本的应用程序基本上会在您点击的位置在屏幕上绘制“You Touched Here”标签。不过,每当我点击时,标签就会出现在我点击的位置上方。

在上面的截图中,我点击了原点。在输出日志中可以看到touch point(具体为:touch->getLocation(), unconverted)被记录为(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) 应该是?

【问题讨论】:

    标签: c++ position cocos2d-x frame


    【解决方案1】:

    您的 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));
    }
    

    【讨论】:

      最近更新 更多