Incorrect Y coordinate of mouse location on mouse event

Incorrect Y coordinate of mouse location on mouse event
0

#1

Hi there, I’m newbie in Cocos as well as in C++ :slightly_smiling_face:

I’ve faced the problem with simple clicks on my Scene.
When I’m running next code within my scene:

auto mouseClickListener = EventListenerMouse::create();

mouseClickListener->onMouseUp = [this](cocos2d::Event* event){
    EventMouse* mouseEvent = dynamic_cast<EventMouse*>(event);
    // debug mouseEvent data here
};

Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(mouseClickListener, this);

and click on my window frame I received data:

  1. topmost pixel:
  • mouseEvent->getLocation().y returns me value of window frame height (which I set in AppDelegate.cpp ~ e.g. 600) instead of 0 as I understand from guides;
  • mouseEvent->getLocationInView().y returns me 0 (according to my sizes: instead of 600?).
  1. when I click on lowermost pixel:
  • mouseEvent->getLocation().y returns me x2 value of window frame height (e.g. 1200);
  • mouseEvent->getLocationInView().y returns me a negative value of window frame height (e.g. -600);

Issue is related only to Y coordinate. X coordinate works as I expect (most left pixel X is 0, most right is equal to my frame width). As you may understand it does not allow me to click on my sprites within the scene. Could you please advice me what I’am doing wrong? Thanks!

configuration:
* macOS Mojave 10.14.2
* cocos2d-x-3.17
* macOS desktop app


#2

try listening on this nodes parent


#3

@Musab, thanks for your reply. Could you please provide me with example of code? As I understand dispatcher listens Scene in my example. What parent node I should use instead of this in such case?


#4

sorry i don’t use c++ or cocos2d-x :smiley: i was bored and i thought i knew the problem but when you do this with js and just use this it doesn’t work so i thought it would work for you to listen to its parent but i don’t know how you would do that :frowning: good luck


#5

Why are you choosing 600? Show me your AppDelegate please


#6

600 was chosen just for example. Initially I got a problem with a full-sized window.
@slackmoehrle, here is my AppDelegate.cpp:

#include "AppDelegate.h"
#include "HelloWorldScene.h"

USING_NS_CC;

AppDelegate::AppDelegate() {}

AppDelegate::~AppDelegate(){}

bool AppDelegate::applicationDidFinishLaunching() {
    auto director = Director::getInstance();
    auto glview = director->getOpenGLView();

    if (!glview) {
        glview = GLViewImpl::create("My Title");
        glview->setFrameSize(800, 600);
        director->setOpenGLView(glview);
    }

    auto helloWorldScene = HelloWorld::createScene();
    director->runWithScene(helloWorldScene);

    return true ;
}

void AppDelegate::applicationDidEnterBackground() {}
void AppDelegate::applicationWillEnterForeground() {}

#7

Why have you modified the code so much? I’d go back to the original template.


#8

I just followed guides on gamefromscratch.
Anyway, I will try the original AppDelegate.
Thanks for advice!


#9

Following your advice I have compared my AppDelegate.cpp with original test file on cocos2d-x git and I have found out that the problem was caused by 1 missing row:

glview->setDesignResolutionSize(800, 600, ResolutionPolicy::NO_BORDER);

I’ve added it and now everything seems ok. Thanks for help!


#10

You are very welcome. Please let me know if I can help in the future.