@paull51 wrote:
Hey,
I have been trying to learn cocos using VS and C++. Recently, I have encountered an issue while trying to run an animation when a key on the keyboard is pressed. The code:
Vector<SpriteFrame*> frames = getAnimation("player%d.png", 3); _player = Sprite::createWithSpriteFrame(frames.front()); this->addChild(_player); _player->setPosition(x, y); animation = Animation::createWithSpriteFrames(frames, 1.0f / 3); auto action = RepeatForever::create(Animate::create(animation)); auto eventListener = EventListenerKeyboard::create(); eventListener->onKeyPressed = [&](EventKeyboard::KeyCode keyCode, Event* event) { _player->runAction(action); };
After compiling this code and executing it, I get this error when I press a key:
Exception thrown: write access violation.
this was 0x10400A62.And VS leads me to this line in CCRef.cpp file
void Ref::retain() { CCASSERT(_referenceCount > 0, "reference count should be greater than 0"); ++_referenceCount; // <---- this line
Though, if I put the runAction function outside the onKeyPressed lambda, (in the init function for example) everything works fine and the animation executes.
It's probably something quite obvious, but since I am fairly new I hope to get some insight on what is exactly happening behind the scenes and why there is an error, how should I fix it.
Thanks.
Posts: 7
Participants: 3