Hi peoples!
I’m satisfied to say I searched for an answer up and down google and the forum.
edit
>I’m using Xcode 4.6
>I’m using Cocos2dx 2.0.4 and cocosbuilder 2.1
>I’m developing for iOS for the iPhone 4 and 5
edit 2
>So clearly the problem I’ve found is that the code inside of “onResolveCCBCCMenuItemSelector” and “onResolveCCBCCControlSelector” is never executed. Nothing I can find says where these functions are supposed to be called. I understand what the CCString selector name parameter/argument, but not the CCObject one.
Any help would be greatly appreciated! Thank you!
So, I managed to get my ccbi file loaded and displayed (is contains only a CCLayer with a CCControlButton inside). CCLayer has a custom class HelloCocosBuilder. CCControlButton has selector: pressedNewGameButton and target’s owner. (no idea what these mean!)
I’ve been following:…
http://www.plungeinteractive.com/blog/using-cocosbuilder-on-cocos2d-x-games/
…to get my code hooked up to my button.
AppDelegate.cpp
bool AppDelegate::applicationDidFinishLaunching()
{
// initialize director
CCDirector * pDirector = CCDirector::sharedDirector();
pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());
// turn on display FPS
pDirector->setDisplayStats(true);
// set FPS. the default value is 1.0/60 if you don't call this
pDirector->setAnimationInterval(1.0 / 60);
// create and register node loader library
cocos2d::extension::CCNodeLoaderLibrary* nodeLoader;
nodeLoader = cocos2d::extension::CCNodeLoaderLibrary::newDefaultCCNodeLoaderLibrary();
nodeLoader->registerCCNodeLoader("HelloCocosBuilder", HelloCocosBuilderLoader::loader());
// create ccbreader with the default node loaderLibrary
cocos2d::extension::CCBReader * ccbReader = new cocos2d::extension::CCBReader(nodeLoader);
// load the main node from the ccbi file
CCNode * node = ccbReader->readNodeGraphFromFile("MainMenuCCB.ccbi");
// create a scene. it's an autorelease object
CCScene * pScene = HelloWorld::scene();
// add the ccbi node to the scene
if(node != NULL) {
pScene->addChild(node);
}
// release the CCBReader instance since we don't need it anymore
ccbReader->release();
// run the scene
pDirector->runWithScene(pScene);
return true;
}
HellowWorldScene.h
#ifndef __HELLO_WORLD_H__
#define __HELLO_WORLD_H__
// When you import this file, you import all the cocos2d classes
#include "cocos2d.h"
#include "Box2D.h"
class PhysicsSprite : public cocos2d::CCSprite
{
public:
PhysicsSprite();
void setPhysicsBody(b2Body * body);
virtual bool isDirty(void);
virtual cocos2d::CCAffineTransform nodeToParentTransform(void);
private:
b2Body* m_pBody; // strong ref
};
class HelloWorld : public cocos2d::CCLayer {
public:
~HelloWorld();
HelloWorld();
// returns a Scene that contains the HelloWorld as the only child
static cocos2d::CCScene* scene();
void initPhysics();
// adds a new sprite at a given coordinate
void addNewSpriteAtPosition(cocos2d::CCPoint p);
virtual void draw();
virtual void ccTouchesEnded(cocos2d::CCSet* touches, cocos2d::CCEvent* event);
void update(float dt);
private:
b2World* world;
cocos2d::CCTexture2D* m_pSpriteTexture; // weak ref
};
#endif // __HELLO_WORLD_H__
HelloWorldScene.cpp
CCScene* HelloWorld::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create();
// add layer as a child to scene
CCLayer* layer = new HelloWorld();
scene->addChild(layer);
layer->release();
return scene;
}
HelloCocosBuilder.h
#include
#include "CCLayer.h"
#include "CCObject.h"
#include "CCControlExtensions.h"
#include "CCBReader.h"
#include "CCLayerLoader.h"
#include "CCBSelectorResolver.h"
class HelloCocosBuilder : public cocos2d::CCLayer, public cocos2d::extension::CCBSelectorResolver {
//virtual bool init();
virtual cocos2d::SEL_MenuHandler onResolveCCBCCMenuItemSelector(CCObject * pTarget, cocos2d::CCString * pSelectorName);
virtual cocos2d::extension::SEL_CCControlHandler onResolveCCBCCControlSelector(cocos2d::CCObject * pTarget, cocos2d::CCString * pSelectorName);
void pressedNewGameButton(CCObject *pSender, cocos2d::extension::CCControlEvent pCCControlEvent);
//void pressedContinueButton(CCObject *pSender, cocos2d::extension::CCControlEvent pCCControlEvent);
public:
CREATE_FUNC(HelloCocosBuilder);
};
class HelloCocosBuilderLoader : public cocos2d::extension::CCLayerLoader {
public:
CCB_STATIC_NEW_AUTORELEASE_OBJECT_METHOD(HelloCocosBuilderLoader, loader);
protected:
CCB_VIRTUAL_NEW_AUTORELEASE_CREATECCNODE_METHOD(HelloCocosBuilder);
};
HelloCocosBuilder.cpp
SEL_MenuHandler HelloCocosBuilder::onResolveCCBCCMenuItemSelector(CCObject * pTarget, CCString * pSelectorName)
{
//CCB_SELECTORRESOLVER_CCMENUITEM_GLUE(this, "pressedContinueButton", HelloCocosBuilder::pressedContinueButton);
return NULL;
}
SEL_CCControlHandler HelloCocosBuilder::onResolveCCBCCControlSelector(cocos2d::CCObject *pTarget, cocos2d::CCString*pSelectorName) {
CCB_SELECTORRESOLVER_CCCONTROL_GLUE(this, "pressedNewGameButton", HelloCocosBuilder::pressedNewGameButton);
return NULL;
}
void HelloCocosBuilder::pressedNewGameButton(cocos2d::CCObject *pSender, cocos2d::extension::CCControlEvent pCCControlEvent) {
CCLOG("Pressed new game button.");
}
I create a scene in the appdelegate and add the node as a child there before running the scene.
I see “Cocos2d: Unexpected NULL target for selector.” in my log after successfully building. (again, I don’t no what selectors are, or selector resolvers)
I feel like I’m just missing a step somewhere.
So, are cccontrols treated differently from menuitems? Are they just not used anymore or something, uh, deprecated? I can click my button, but the CCLOG (“You pressed the button!”) isn’t executing.
Thank you very much for any help you can offer!