Can't add sprite to another layer?

Hi everyone, I’m very confused as to why this doesn’t work, I’ve tried many solutions and all of them failed and I’m in need of help:

In the .h file:

class OverWorldView : public CCLayer {
    static ButtonController* dPadLayer;
    static CCLayer* playerLayer;
}

In the .cpp file:

#include "OverWorldView.h"

ButtonController* OverWorldView::dPadLayer = nullptr;
CCLayer* OverWorldView::playerLayer = nullptr;


CCScene* OverWorldView::scene() {
    // 'scene' is an autorelease object
    CCScene* scene = CCScene::create();

    // 'layer' is an autorelease object
    OverWorldView* layer = OverWorldView::create();
    dPadLayer = ButtonController::create();
    playerLayer = CCLayer::create();

    // add layer as a child to scene
    scene->addChild(layer);
    scene->addChild(dPadLayer);
    scene->addChild(playerLayer);

    // return the scene
    return scene;
}

Also in the .cpp file:

void OverWorldView::addPlayer(CCPoint location) {
    sharedSpriteFrameCache->addSpriteFramesWithFile("Player.plist", "Player.png");
    player = std::make_shared(currentMap, mapModel);
    player->setSprite( CCSprite::createWithSpriteFrameName("Player[3][2].png") );
    player->setPosition(location);
    playerLayer->addChild(player->sprite);<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Thread 1: EXC_BAD_ACCESS(code=2, address=0x0)
    std::cout << "Position: " << player->sprite->getPosition().x << " " << player->sprite->getPosition().y << std::endl;
}

Why can’t I add a sprite created from the main scene into one of it’s layers? What’s the correct way of doing it?

Does it work if you create the sprite separately and then add it to both the player and player layer?

CCSprite* playerSprite = CCSprite::createWithSpriteFrameName(“Player[3][2].png”)
player~~>setSprite
playerLayer~~>addChild(playerSprite)

The problem might be getting the sprite out of the player object.

Nope, same error.

I can add the sprite and have everything work correctly to the OverWorldView layer, this is very confusing.

do you mean,
this->addChild(playerSprite) works ok?

Can you add anything to playerLayer (like a normal CCSprite rather than using player = std::make_shared and then playerLayer->addChild(player->sprite))?

Nope, adding anything fails, but the ‘dPadLayer’ is rendered on screen and can detect all touch events, the player layer uses the exact same code to create an object, I can’t add a sprite as a child to either layers, which makes me incredibly confused.

The dPadLayer has a dozen sprites added to itself in it’s own class, that seems to work but trying to add a sprite to it from the main scene just fails and give the exact same error above.

This code in OverWorldView.cpp produces the same error:

    CCSprite* ps = CCSprite::createWithSpriteFrameName("Player[3][2].png");
    player->setSprite(ps);
    player->setPosition(location);
    dPadLayer->addChild(ps); // or playerLayer->addChild(ps);

But this code in ButtonController.cpp works:

bool ButtonController::init() {
    upButtonSprite = new CCSprite;
    upButtonSprite->initWithFile(upButtonImage);
    upButtonSprite->setPosition( ccp(73, 86) );
    addChild(upButtonSprite);
}

This code in OverWorldView.cpp also works:

void OverWorldView::addPlayer(CCPoint location) {
    sharedSpriteFrameCache->addSpriteFramesWithFile("Player.plist", "Player.png");
    player = std::make_shared(currentMap, mapModel);
    player->setSprite( CCSprite::createWithSpriteFrameName("Player[3][2].png") );
    player->setPosition(location);
    this->addChild(player->sprite);
    std::cout << "Position: " << player->sprite->getPosition().x << " " << player->sprite->getPosition().y << std::endl;
}

I’ve tried using:
->retain();
static NPC* player;
declaring a raw pointer in dPadLsyer, then attempt to new it in OverWorldView

Nothing works, can anyone give me any advice?

I think at this point your best bet is to break at addChild(ps) and step in to see where and why it is crashing. If it is literally crashing on addChild then something seems to be mucked up with the dPadLayer pointer.

Seems that playerLayer is always NULL.