Centering the sprite and getting touch response on it

Hey all, I was trying to position a sprite into the center of the scene and getting a touch response on it. But in the output, the sprite is showing in a side of the scene and the area of touch response is also different. I draw a red square on output where I am getting the touch response instead of getting it on the sprite. (v4 & vs2019) .
Here is my code:

AppDelegate.cpp

#include "AppDelegate.h"
#include "TouchScene.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 = TouchScene::createScene();
    director->runWithScene(scene);

    return true;
}

void AppDelegate::applicationDidEnterBackground() {
}

void AppDelegate::applicationWillEnterForeground() {
}

TouchScene.cpp

#include "TouchScene.h"

USING_NS_CC;

Scene* TouchScene::createScene()
{
    auto scene = Scene::create();
    auto layer = TouchScene::create();
    scene->addChild(layer);

    return scene;
}

bool TouchScene::init()
{
    if (!Layer::init())
    {
        return false;
    }

    auto sprite = Sprite::create("Picture.png");
    sprite->setPosition(Vec2(Director::getInstance()->getVisibleSize().width / 2,
        Director::getInstance()->getVisibleSize().height / 2));
    
    // Add a "touch" event listener to sprite
    auto touchListener = EventListenerTouchOneByOne::create();
    touchListener->onTouchBegan = [](Touch* touch, Event* event) -> bool {

        auto bounds = event->getCurrentTarget()->getBoundingBox();

        if (bounds.containsPoint(touch->getLocation())) {
            std::stringstream touchDetails;
            touchDetails << "Touched at OpenGL coordinates: " <<
                touch->getLocation().x << "," << touch->getLocation().y << std::endl <<
                "Touched at UI coordinate: " <<
                touch->getLocationInView().x << "," << touch->getLocationInView().y << std::endl <<
                "Touched at local coordinate:" <<
                event->getCurrentTarget()->convertToNodeSpace(touch->getLocation()).x << "," <<
                event->getCurrentTarget()->convertToNodeSpace(touch->getLocation()).y << std::endl <<
                "Touch moved by:" << touch->getDelta().x << "," << touch->getDelta().y;

            MessageBoxA(0, touchDetails.str().c_str(), "Touched", MB_OK);
            
        }
        return true;
    };

    Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(touchListener, sprite);
    this->addChild(sprite, 0);

    return true;
}

And here is output:


(red box is not the part of output, i draw it by editing)

I would change anchor point on the sprite.

https://docs.cocos2d-x.org/cocos2d-x/v4/en/basic_concepts/sprites.html

Changing anchor point didn’t help. And it should display at the center while anchor point is (0.5,0.5). Touch response area problem also remain same, doesn’t matter what position or anchor point the sprite have.

Post your sprite please and I’ll drop this into a new project and test

Also 640x480 is quite small these days.

Picture
Thanks.

@rahul_d_v Your AppDelegate.cpp code is heavily modified, and it seems that you’ve deleted code that is still required. Best to not do that unless you know exactly what the purpose of that code is, otherwise you’ll end up with issues like this.

The missing lines have been added below:

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

    // the two missing lines
    glview->setDesignResolutionSize(640, 480, ResolutionPolicy::FIXED_WIDTH);
    director->setContentScaleFactor(1.0f);

    auto scene = TouchScene::createScene();
    director->runWithScene(scene);

    return true;
}
2 Likes

Thanks @r101 I was just replying to say cocos new with their Sprite works correctly.

The posted AppDelegate.cpp code looked way too different from what is generated by the cocos new command, so had a hunch it was there. That section of code in AppDelegate can be a bit confusing, but it’s one of the most important bits of code in the app, and if it’s wrong, strange things happen :scream:.

1 Like

Thanks @R101 @slackmoehrle. After adding these two lines its working fine. I am in leaning phase so I just copied the code from gamefromscratch tutorial to understand.

@rahul_d_v just remember cocos new ... and cpp-tests are your friends. Tutorials found online can be old or modified without explanation. Games From Scratch is pretty good IMHO.

@R101 :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.