Quantcast
Channel: cocos2d-x - Cocos Forums
Viewing all articles
Browse latest Browse all 2748

Key held and update pointer problem

$
0
0

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

2 posts - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 2748

Latest Images

Trending Articles



Latest Images