Key held and update pointer problem

I found this thread about how to make a sprite move by helding a key pressed (please let me know if there’s has been a new solution since that post from 2018).

I tried to reproduce that code adapting it to mine. Here is my code. The header file and the source file

#include "cocos2d.h"

class HelloWorld : public cocos2d::Scene
    static cocos2d::Scene* createScene();
    virtual bool init();
	void update(float delta) override;

	boolean keys[255] = {false};
	cocos2d::Sprite* sprite;

#endif // __HELLOWORLD_SCENE_H__


#include "HelloWorldScene.h"

Scene* HelloWorld::createScene()
    return HelloWorld::create();

bool HelloWorld::init()
    if ( !Scene::init() )
        return false;

    auto visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();

	// Store cache and create sprite from it
    auto spriteFrameCache = SpriteFrameCache::getInstance();
    auto sprite = Sprite::createWithSpriteFrameName("idle_1.png");

    //Create keyboard event listener to move sprite with WSAD
    auto eventListenerKeyboard = EventListenerKeyboard::create();
	eventListenerKeyboard->onKeyPressed = [&](EventKeyboard::KeyCode code, Event* event) { keys[(int)code] = true; };
	eventListenerKeyboard->onKeyReleased = [&](EventKeyboard::KeyCode code, Event* event) { keys[(int)code] = false; };
	this->_eventDispatcher->addEventListenerWithSceneGraphPriority(eventListenerKeyboard, sprite);


    return true;

void HelloWorld::update(float delta)
	if (keys[(int)EventKeyboard::KeyCode::KEY_A]) {
		sprite->setPosition(sprite->getPosition().x - 1  * delta, sprite->getPosition().y);

	if (keys[(int)EventKeyboard::KeyCode::KEY_D]) {
		sprite->setPosition(sprite->getPosition().x + 1 * delta, sprite->getPosition().y);

	if (keys[(int)EventKeyboard::KeyCode::KEY_S]) {
		sprite->setPosition(sprite->getPosition().x, sprite->getPosition().y - 1 * delta);

	if (keys[(int)EventKeyboard::KeyCode::KEY_W]) {
		sprite->setPosition(sprite->getPosition().x, sprite->getPosition().y + 1 * delta);


The problem with this is that I get this error: this->sprite was nullptr. I understand the error, but I am not sure why it is throwing a null pointer error since the sprite was intialized before the updat (auto sprite = Sprite::createWithSpriteFrameName(“idle_1.png”);), so why is it saying that it is null?


acutally, never mind, it was a silly mistake. I wrote

   auto sprite = Sprite::createWithSpriteFrameName("idle_1.png");

instead of

    this->sprite = Sprite::createWithSpriteFrameName("idle_1.png");

I got it working now, but since I already open that post. I am having a hard time creating that same example if the update of the keyboard event are coming from another class. For instance, say I have a separate Player class that contains that same code. How can I running from the HelloWorld class (which is what AppDelegate is running). Not sure if I am being clear.


okey, I simple create the class instance as a member of the other class. Please feel free to close or delete this post since it was some basic cpp issues, not related to cocos.

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