Key held and update pointer problem

Hello,
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).
https://discuss.cocos2d-x.org/t/code-sharing-detecting-key-being-held-down/44207

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

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"


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

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

#endif // __HELLOWORLD_SCENE_H__

and

#include "HelloWorldScene.h"
USING_NS_CC;


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();
    spriteFrameCache->addSpriteFramesWithFile("idle.plist");
    auto sprite = Sprite::createWithSpriteFrameName("idle_1.png");
    sprite->setPosition(visibleSize/2);
    this->addChild(sprite);

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

	scheduleUpdate();

    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?

Thanks

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.

thanks,
R

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.