Then im trying to call that Update function in may main scene, but i dont know how to do it… this is what i got so far (almost nothing):
#include "HelloWorldScene.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "Ember.h"
USING_NS_CC;
using namespace cocostudio::timeline;
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}
Ember ember;
addChild(ember.make());
scheduleUpdate();
return true;
}
void HelloWorld::update(float dt) {
}
Your ember class creates a Sprite in it’s constructor but does not retain it. This means that if you didn’t addChild it would destruct and Ember::Sprite would become invalid.
make() does not make anything… this is a very misleading function.
Instead consider deriving your ember class from cocos2d::Sprite, and use scheduleUpdate() in it’s onEnter() override and unscheduleUpdate() in it’s onExit() override.
I quickly wrote this up as an example, haven’t tested if it’ll run, but it demonstrates the principals.
Ember.h
#pragma once
#include "cocos2d.h"
class Ember : public cocos2d::Sprite
{
public:
//declare destructor as virtual so we can derive from Ember safely
virtual ~Ember();
//A static create method that builds an auto released instance of Ember
//Auto released objects will destruct at the end of the current frame
static Ember create(const std::string& _fileName);
//synthesize variable using cocos2d-x macro
//this creates the variable and get/set functions for us
CC_SYNTHESIZE(cocos2d::Vec2, m_velocity, Velocity);
virtual void onEnter() override;
virtual void onExit() override;
virtual void update(float dt) override;
protected:
//make constructor protected to stop it being constructed directly
Ember();
//initialisation function that allows construction to fail via create()
bool init(const std::string& _fileName);
};
I used that code, just fix a couple of errors but it worked perfect, i dont know how to thank you…
i will learn more about that code and more about C++, once again thanks for the help!