Extending the sprite class in 3.0


#1

Looking for help. iOS/Xcode/Cocos2dx c++ alpha

I’ve been attempted to extend the sprite class with cocos alpha. I’m not having any luck. I’m VERY new to C++ with a long background in scripting languages. This is what I have:

Header

	#include <iostream>
	#include "cocos2d.h"
	#include "GameSelectionScene.h"
	
	using namespace cocos2d;
	
	class CoinTally : public cocos2d::Sprite {
	public:
	    virtual bool init();
	   
	    CoinTally();
	    virtual ~CoinTally();
	   
	};

	#include "CoinTally.h"
	#include "SimpleAudioEngine.h"
	#include "globals.h"
	#include "CCUserDefault.h"
	#include <sstream>
	
	USING_NS_CC;
	
	CoinTally::CoinTally(void) {
	 
	}
	
	CoinTally::~CoinTally(void) {
	    
	}
	
	
	bool init(){
	    if ( !Sprite::create() )
	    {
	        return false;
	    }
	
	    
	    return true;
	    
	}

#2

i think…

bool CoinTally::init()
{
    if ( !Sprite::init() )
    {
        return false;
    }


    return true;
}

#3

@Yehsam thanks for the quick reply, I had that, but also failed on instantiation


#4

Solution was a different approach using Director::getInstance()->getRunningScene();

I needed a class to run operations that many scenes needed. Using a simple class that did not extend any but accessed the current running scene proved a better solution. I’m not sure if this is the most optimal approach, but it let me abstract my code.

/// H FILE

#include <iostream>
#include "cocos2d.h"

using namespace cocos2d;

class CoinTally {
public:
    bool init();
  
};


/// CPP FILE


using namespace cocos2d;


bool init(){
   
    auto scene = Director::getInstance()->getRunningScene();
    auto coin = Sprite::createWithSpriteFrameName("coin-01.png");
    
    scene->addChild(coin);
    
    return true;
    
};