Mouse Coordinate Problem

  • Ashish424
    Likes 0

    Problem Description

    I have strange problem using mouse with cocos2d-x.I am using the code from the cocos2d-x sample test but I get negative coordinates along Y-axis.(Coordinate along X-axis is corect)Same is true with code from the programmer's guide.Please help as the code included in the sample test of Cocos2d-x is working correct but i am new to cocos2d-x and don't now much about how the test are working and why mine is not.Any help on this is greatly appreciated.I checked the forums with people having the same issue and adding offsets to get it work for themselves.Any please help about  the INPUT test code which comes with cocos2d-x will be great.

    Here is the code i am using→

    Header file->

     

    #include "cocos2d.h"
    
    
    class myMouseEventTest : public cocos2d::Scene
    {
    public:
        CREATE_FUNC(myMouseEventTest);
        myMouseEventTest();
        ~myMouseEventTest();
    
        void onMouseDown(cocos2d::Event* event);
        void onMouseUp(cocos2d::Event* event);
        void onMouseMove(cocos2d::Event* event);
        void onMouseScroll(cocos2d::Event* event);
    
    
    private:
        cocos2d::Label*   _labelAction;
        cocos2d::Label*   _labelPosition;
        cocos2d::EventListenerMouse* _mouseListener;
    };

    Implementation file→

    #include "myMousetest.h"
    USING_NS_CC;
    
    template <typename T> std::string tostr(const T& t) { std::ostringstream os; os<<t; return os.str(); }
    
    
    //------------------------------------------------------------------
    //
    // MouseEventTest
    //
    //------------------------------------------------------------------
    myMouseEventTest::myMouseEventTest()
    {
        auto s = Director::getInstance()->getWinSize();
    
        //Create a label to display the mouse action
        _labelAction = Label::createWithTTF("Click mouse button and see this change", "fonts/arial.ttf", 22);
        _labelAction->setPosition(Vec2(s.width/2, s.height*2/3));
        addChild(_labelAction, 0);
    
        //Create a label to display the mouse position
        _labelPosition = Label::createWithTTF("Mouse not supported on this device", "fonts/arial.ttf", 22);
        _labelPosition->setPosition(Vec2(s.width/2, s.height/3));
        addChild(_labelPosition);
    
    
        _mouseListener = EventListenerMouse::create();
        _mouseListener->onMouseMove = CC_CALLBACK_1(myMouseEventTest::onMouseMove, this);
        _mouseListener->onMouseUp = CC_CALLBACK_1(myMouseEventTest::onMouseUp, this);
        _mouseListener->onMouseDown = CC_CALLBACK_1(myMouseEventTest::onMouseDown, this);
        _mouseListener->onMouseScroll = CC_CALLBACK_1(myMouseEventTest::onMouseScroll, this);
    
        _eventDispatcher->addEventListenerWithSceneGraphPriority(_mouseListener, this);
    }
    
    myMouseEventTest::~myMouseEventTest()
    {
        _eventDispatcher->removeEventListener(_mouseListener);
    }
    
    void myMouseEventTest::onMouseDown(Event *event)
    {
        EventMouse* e = (EventMouse*)event;
        std::string str = "Mouse Down detected, Key: ";
        str += tostr(e->getMouseButton());
        _labelAction->setString(str.c_str());
    }
    
    void myMouseEventTest::onMouseUp(Event *event)
    {
        EventMouse* e = (EventMouse*)event;
        std::string str = "Mouse Up detected, Key: ";
        str += tostr(e->getMouseButton());
        _labelAction->setString(str.c_str());
    }
    
    void myMouseEventTest::onMouseMove(Event *event)
    {
        EventMouse* e = (EventMouse*)event;
        std::string str = "MousePosition X:";
        str = str + tostr(e->getCursorX()) + " Y:" + tostr(e->getCursorY());
        _labelPosition->setString(str.c_str());
    }
    
    void myMouseEventTest::onMouseScroll(Event *event)
    {
        EventMouse* e = (EventMouse*)event;
        std::string str = "Mouse Scroll detected, X: ";
        str = str + tostr(e->getScrollX()) + " Y: " + tostr(e->getScrollY());
        _labelAction->setString(str.c_str());
    }
    
    I am running the Direcotr With this scene and and i get negative coordniates(along Y) but corect coordinates along the X-axis.Tried both on mac and PC(windows 8.1).
  • Sonar Systems admin
    Likes 0

    Try taking a look at this http://cocos.sonarlearning.co.uk/v1.0/docs/input

     

     

  • Ashish424
    Likes 0

    This does not ans the question.The link doesnt even have the code for  onMouseMove.Please reply with correct ans.

     

  • Sonar Systems admin
    Likes 0

    Do you always get negative or does it range from positive to negative?

  • Ashish424
    Likes 0

    Always.

  • Sonar Systems admin
    Likes 0

    OK, what is the range roughly for the values? Are they normalised (aka 0 – -1)

  • Ashish424
    Likes 0

    I am using a 640X480 resolution.The coordinates for Y value vary from -480 to 0.I know i can add 480 to get the result but I want to know why the sample given with cocos2d is able to run without the need of adding offset.

  • Sonar Systems admin
    Likes 0

    Did you copy the code from the samples?

  • Ashish424
    Likes 0

    Yes indeeed.Please read the question again.

  • Sonar Systems admin
    Likes 0

    You said sample test of Cocos2d-x is working correct, how was that tested and run?

  • Ashish424
    Likes 0

    I just go to cocos2d-x directory and build the cpp test project ,ran the test,got to mouse test and ran the test.Then i tried to make the test my self my copying the code in another project and when i played it reported negative coordinates and hence i posted this question to know what the cocos2d-x guys are doing in there test to report the correct coordinates.I hope this helps.


  • Sonar Systems admin
    Likes 0

    Did you compile the entire class from the test?

  • Ashish424
    Likes 0

    I compiled all the test with cocos2d-x test.

  • Sonar Systems admin
    Likes 0

    Sorry i meant copy the entire class to your project or just bits of it?

  • Ashish424
    Likes 0

     Just bits of it.I am tired of telling read the question.I pasted the code which i copied.You can run the code yourself and see.

  • Sonar Systems admin
    Likes 0

Login to reply