android - Use of Classes in Cocos2d-x to create an "Enemy" to extend Layer -
pretty new cocos2dx , c++ objective-c background. i'm making game can have between 1-10 enemies on screen @ time. each enemy has 1 sprite body , 2 stats (health & damage).
in past objective-c i've been able define class.h/class.mm file , fill many variables want (health, speed, height, weight, color, etc), i'm having trouble figuring out how in cocos2dx.
here simplified project isolates problem i'm having. i'm sure it's straight forward i'm having trouble looking solution
//-----enemy.h #include "cocos2d.h" using_ns_cc; class enemy : public cocos2d::layer{ public: virtual bool init(); create_func(enemy); void sethealth(int val); private: int myhealth; sprite *body; sprite *weapon; }; //-----enemy.cpp #include "enemy.h" using_ns_cc; bool enemy::init() { if ( !layer::init() ) return false; return true; } void sethealth(int newhealth) { //myhealth = newhealth; } //-----helloworldscene.h #include "cocos2d.h" using_ns_cc; class helloworld : public cocos2d::layer { public: static cocos2d::scene* createscene(); virtual bool init(); create_func(helloworld); }; //-----helloworldscene.cpp #include "helloworldscene.h" #include <iomanip> #include "enemy.h" using_ns_cc; scene* helloworld::createscene() { auto scene = scene::create(); auto layer = helloworld::create(); scene->addchild(layer); return scene; } // on "init" need initialize instance bool helloworld::init() { if ( !layer::init() ) return false; enemy *newenemy = enemy::create(); //newenemy->sethealth(100); this->addchild(newenemy); return true; }
this compiles fine except 2 commented lines, cause errors.
please help, in advance!
without knowing error message exactly, looks missing class name in sethealth
method in enemy.cpp
change method declaration in enemy.h
from:
void sethealth(int val);
to:
void sethealth(int newhealth);
then in enemy.cpp file change following.
from:
void sethealth(int newhealth)
to:
void enemy::sethealth(int newhealth)
Comments
Post a Comment