Cocos2d-x -- Changing a sprite's frame

Hello,

I’m trying to change a sprite’s frame after a swipe motion (using TexturePacker to create a .list). It works fine when i test it in the init() method by adding the following lines:

auto splayer = Spritesheet::createIdleSprite();
idle_air = SpriteFrameCache::getInstance()->getSpriteFrameByName(Spritesheet::idle_air);
splayer->setSpriteFrame(idle_air);

However if i try moving the last line to another method, update() for example, the game crashes whenever it executes that line. I tried checking if idle_air==nullptr but it isn’t.

I’d really appreciate any help, Thank you.

It should work. probably you are doing something else wrong. If you can show the stack trace of your crash we can help you figure this out.

I would consider that maybe you set idle_air locally? or maybe you free it yourself before accessing it?

Other then looking if it is NULL try to see in a debugger if the object seems valid.

Thank you for your response.

Haven’t set idle_air locally, i’m about to check on the debugger what’s going wrong.

Here’s the crash log i have no idea what to conclude of it though:

********** Crash dump: **********
Build fingerprint: 'alps/full_wtk6753_65u_m0/wtk6753_65u_m0:6.0/MRA58K/1482734689:user/dev-keys'
pid: 12183, tid: 12199, name: GLThread 411  >>> survive.test.b <<<
signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0
Stack frame 01-07 19:46:32.737 12240 12240 F AEE/DEBUG:     #00 pc 004b8394  /data/app/survive.test.b-1/lib/arm/libMyGame.so (_ZN4Play6updateEf+995): Routine Play::update(float) at C:\Users\Armand\Desktop\test3\proj.android/jni/../../Classes/PlayScene.cpp:121
Stack frame 01-07 19:46:32.737 12240 12240 F AEE/DEBUG:     #01 pc 0058b3fc  /data/app/survive.test.b-1/lib/arm/libMyGame.so (_ZZN7cocos2d9Scheduler14scheduleUpdateINS_4NodeEEEvPT_ibENKUlfE_clEf+56): Routine void cocos2d::Scheduler::scheduleUpdate<cocos2d::Node>(cocos2d::Node*, int, bool)::{lambda(float)#1}::operator()(float) const at C:/Users/Armand/Desktop/test3/cocos2d/cocos/./base/CCScheduler.h:282
Stack frame 01-07 19:46:32.737 12240 12240 F AEE/DEBUG:     #02 pc 0058cf14  /data/app/survive.test.b-1/lib/arm/libMyGame.so (_ZNSt17_Function_handlerIFvfEZN7cocos2d9Scheduler14scheduleUpdateINS1_4NodeEEEvPT_ibEUlfE_E9_M_invokeERKSt9_Any_dataf+60): Routine std::_Function_handler<void (float), void cocos2d::Scheduler::scheduleUpdate<cocos2d::Node>(cocos2d::Node*, int, bool)::{lambda(float)#1}>::_M_invoke(std::_Any_data const&, float) at C:/Android/android-ndk-r16b/build//../sources/cxx-stl/gnu-libstdc++/4.9/include/functional:2039 (discriminator 1)
Stack frame 01-07 19:46:32.737 12240 12240 F AEE/DEBUG:     #03 pc 006c95b0  /data/app/survive.test.b-1/lib/arm/libMyGame.so (_ZNKSt8functionIFvfEEclEf+88): Routine std::function<void (float)>::operator()(float) const at C:/Android/android-ndk-r16b/build//../sources/cxx-stl/gnu-libstdc++/4.9/include/functional:2439 (discriminator 1)
Stack frame 01-07 19:46:32.737 12240 12240 F AEE/DEBUG:     #04 pc 006c5e14  /data/app/survive.test.b-1/lib/arm/libMyGame.so (_ZN7cocos2d9Scheduler6updateEf+332): Routine cocos2d::Scheduler::update(float) at C:/Users/Armand/Desktop/test3/cocos2d/cocos/./base/CCScheduler.cpp:850
Stack frame 01-07 19:46:32.737 12240 12240 F AEE/DEBUG:     #05 pc 0067f138  /data/app/survive.test.b-1/lib/arm/libMyGame.so (_ZN7cocos2d8Director9drawSceneEv+160): Routine cocos2d::Director::drawScene() at C:/Users/Armand/Desktop/test3/cocos2d/cocos/./base/CCDirector.cpp:298
Stack frame 01-07 19:46:32.737 12240 12240 F AEE/DEBUG:     #06 pc 00683c28  /data/app/survive.test.b-1/lib/arm/libMyGame.so (_ZN7cocos2d8Director8mainLoopEv+124): Routine cocos2d::Director::mainLoop() at C:/Users/Armand/Desktop/test3/cocos2d/cocos/./base/CCDirector.cpp:1498
Stack frame 01-07 19:46:32.738 12240 12240 F AEE/DEBUG:     #07 pc 004c03e5  /data/app/survive.test.b-1/lib/arm/libMyGame.so (Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeRender+16): Routine Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeRender at C:/Users/Armand/Desktop/test3/cocos2d/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxRenderer.cpp:18 (discriminator 1)
Stack frame 01-07 19:46:32.738 12240 12240 F AEE/DEBUG:     #08 pc 000eac29  /system/lib/libart.so (art_quick_generic_jni_trampoline+40)
Stack frame 01-07 19:46:32.738 12240 12240 F AEE/DEBUG:     #09 pc 000e6531  /system/lib/libart.so (art_quick_invoke_stub_internal+64)
Stack frame 01-07 19:46:32.738 12240 12240 F AEE/DEBUG:     #10 pc 003eaed9  /system/lib/libart.so (art_quick_invoke_static_stub+172)
Stack frame 01-07 19:46:32.738 12240 12240 F AEE/DEBUG:     #11 pc 00101ff4  [stack:12199]

The first debug points out ‘splayer->setSpriteFrame(idle_air);’ in update() which is the expected cause of crash

Still looking for a solution, this doesn’t seem hard to solve but i just can’t get to the bottom of the problem. If it would help i could link the .cpp.

you don’t provide enough data so we can help you. I tried what you are doing here and it works just fine…

OP code works for me as well.

You define splayer as a local variable. It may be null in update().

Also, you may need to do something like idle_air->retain(), or whatever, due to the memory model used with cocod2-x.

OP is using Texture Packers API for this.

PlayScene.cpp (4.4 KB)
PlayScene.h (1010 Bytes)

Thanks for the replies, linked the code. App crashes whenever i swipe up which triggers the sprite frame change. @squashmode i’ll try what you suggested, didn’t define splayer as a local variable however.

your bug is that you define splayer in your class and local.

just change :

auto splayer = Sprite::createWithSpriteFrame(idle);
to
splayer = Sprite::createWithSpriteFrame(idle);
and you are good to go :slight_smile:

@Nuno147 That didn’t work unfortunately.

Make sure you did what I told you. because this is definitely a bug you had. If it doesn’t work please make sure it crashes in the same place it did before. because I doubt it honestly :slight_smile:

If it doesn’t work with same crash please remove the line :
splayer->setSpriteFrame(idle_air);
and let us know if it still crashes without it.

Thank you so much, redefining was what caused the bug. Turned out i was modifying a copy of my code removing splayer->setSpriteFrame(idle_air); pointed that out. Can’t believe i’ve spent hours trying to find what was going on. Case solved.

It was so very clear that this is your issue. that why I said I doubt this is not the case :slight_smile: