Custom class for Sprite , Doc Root Var in Cocosbuilder and Cocos2dx - cocos2d-x

I have CCB file with CCNode like this (since i cannot post images),
CCNode - Custom class (PanoController)
-DrawerLayer - its only holder, of type CCLayer
--rawImage - it is set as Doc root var with name : rawImage
I want to make rawImage to be instance of mySprite , not CCSprite as it is default.
So , The loader is like this for mySprite,
#include "mySprite.h"
class mySpriteLoader : public CCSpriteLoader{
public:
CCB_STATIC_NEW_AUTORELEASE_OBJECT_METHOD(mySpriteLoader, loader);
protected:
CCB_VIRTUAL_NEW_AUTORELEASE_CREATECCNODE_METHOD(mySprite);
};
also the mySprite.h is this ,
#include <iostream>
#include "cocos2d.h"
#include "cocos-ext.h"
using namespace cocos2d;
using namespace extension;
class mySprite : public CCSprite, public CCBMemberVariableAssigner {
public:
// CCB_STATIC_NEW_AUTORELEASE_OBJECT_METHOD(mySprite, create);
CCB_STATIC_NEW_AUTORELEASE_OBJECT_WITH_INIT_METHOD(mySprite, create);
bool onAssignCCBMemberVariable(CCObject* pTarget, const char* pMemberVariableName, CCNode* pNode){ return false; };
mySprite(){CCLog("init mySprite");};
};
also i registered the mySprite loader like this,
m_loaderLibrary = CCNodeLoaderLibrary::sharedCCNodeLoaderLibrary();
m_loaderLibrary->registerCCNodeLoader("mySprite", mySpriteLoader::loader());
and in the method from the parent CCLayer set Member Assigner
bool PanoController::onAssignCCBMemberVariable(CCObject* pTarget, const char* pMemberVariableName, CCNode* pNode){
CCLOG("%s panoController Assign",pMemberVariableName);
CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "rawImage", mySprite*, m_rawImage);
}
Still it throws error
Assertion failed: (m_rawImage), function onAssignCCBMemberVariable,
Any ideas, please help. Its working fine if its CCSprite , but mySprite not.

The assertion come from :
MEMBERVARIABLE = dynamic_cast<MEMBERVARIABLETYPE>(pNode);
CC_ASSERT(MEMBERVARIABLE);
The class node of node is CCSprite instead of MySprite.
Which mean that CocosBuilder is fail to setup custom class MySprite.
You may register the node at AppDelegate.cpp as below :
const char *pClassName = "MySprite"
cocos2d::extension::CCNodeLoader *pCCNodeLoader = MySpriteLoader::loader();
cocos2d::extension::CCNodeLoaderLibrary * pCCNodeLoaderLibrary = cocos2d::extension::CCNodeLoaderLibrary::sharedCCNodeLoaderLibrary();
pCCNodeLoaderLibrary->registerCCNodeLoader(pClassName, pCCNodeLoader);

Related

AdMob Interstitial Cocos2d-x WP8

Can enyone tell me how to call AdMob Interstitial between scenes in my cocos2d-x game?
I have tried this http://robwirving.com/2014/07/21/calling-c-methods-c-winrt-components/ guide, but i don't know how to run it from cocos classes.
Is there any another ways, or some guides?
I've recently made it. You have to do few things. First of all create helper class, which will help you calling native function (I use this for all 3 platforms, but here's just windows phone):
NativeHelper.h:
#ifndef __NATIVE_HELPER_H_
#define __NATIVE_HELPER_H_
#include <string>
#include <functional>
#include "cocos2d.h"
using namespace std;
USING_NS_CC;
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
namespace cocos2d
{
public delegate void CSharpShowInterstitialDelegate();
public ref class WP8NativeEventHelper sealed
{
public:
void WP8NativeEventHelper::SetCSharpShowInterstitialDelegate(CSharpShowInterstitialDelegate^ delegate){
m_CSharpShowInterstitialDelegate = delegate;
}
void CallShowInterstitial();
private:
property static CSharpShowInterstitialDelegate^ m_CSharpShowInterstitialDelegate;
};
}
#endif
class NativeHelper
{
public:
static void showInterstitial(string adSdk);
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
WP8NativeEventHelper^ wp8helper;
#endif
//instance required only for setting callback
static NativeHelper* getInstance();
~NativeHelper()
{
instanceFlag = false;
}
private:
static bool instanceFlag;
static NativeHelper* instance;
NativeHelper() {};
};
#endif // __NATIVE_HELPER_H_
So. We have special C++/CX class Wp8NativeEventHelper, which can "talk" with C#. Here we store a delegate.
How it works:
C# is calling SetCSharpShowInterstitialDelegate and passes a delegate to it, which will be remembered in static property.
Then C++\CX can call it using CallShowInterstitial.
Now NativeHelperWP.cpp:
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
#ifndef __NATIVE_HELPER_WP_H_
#define __NATIVE_HELPER_WP_H_
#include "NativeHelper.h"
void WP8NativeEventHelper::CallShowInterstitial(){
if (m_CSharpShowInterstitialDelegate)
{
m_CSharpShowInterstitialDelegate->Invoke();
}
}
bool NativeHelper::instanceFlag = false;
NativeHelper* NativeHelper::instance = NULL;
NativeHelper* NativeHelper::getInstance()
{
if(!instanceFlag){
instance = new NativeHelper();
instanceFlag = true;
instance->wp8helper = ref new WP8NativeEventHelper();
}
return instance;
}
void NativeHelper::showInterstitial(){
NativeHelper::getInstance()->wp8helper->CallShowInterstitial();
}
#endif
#endif
Here is just an implementation of CallShowInterstitial. Also in NativeHelper::showInterstitial we're calling C++/CX, which later calls c#.
Now c# code (MainPage.xaml.cs):
outside namespace:
using GoogleAds;
inside class:
private InterstitialAd interstitialAd;
in constructor:
WP8NativeEventHelper helper = new WP8NativeEventHelper();
helper.SetCSharpShowInterstitialDelegate(showInterstitial);
and also create showInterstitial function:
public void showInterstitial() //we recreate interstitial each time, because otherwise it'll show only once, only new requests won't work
{
interstitialAd = new InterstitialAd("MY_AD_UNIT_ID");
AdRequest adRequest = new AdRequest();
#if DEBUG
// Enable test ads.
adRequest.ForceTesting = true;
#endif
interstitialAd.ReceivedAd += OnAdReceived;
interstitialAd.LoadAd(adRequest);
}
and finally OnAdReceived:
private void OnAdReceived(object sender, AdEventArgs e)
{
System.Diagnostics.Debug.WriteLine("Ad received successfully");
interstitialAd.ShowAd();
}
Follow this guide to setup admob: https://developers.google.com/mobile-ads-sdk/docs/admob/wp/quick-start
Now let's use this.
In HelloWorldScene.h add:
#include "NativeHelper.h"
In HelloWorldScene.cpp:
NativeHelper::showInterstitial();
The same way you can show/hide/change position of admob banner for example (however it's buggy so I'm using ad mediation).

How to access Physics World in Layer

I need to create joint between two bodies in layer. Joints are to be added in physics world. How can i access physics world in layer?
My JavaScript implementation. Hope it will help.
var space;
function initPhysics() {
space = new cp.Space();
space.gravity = cp.v(0, -800);
space.iterations = 30;
space.sleepTimeThreshold = Infinity;
space.collisionSlop = Infinity;
}
function addJoints() {
chainTableJoint1 = new cp.PivotJoint(chainEnd1, tableBackBody, cp.v.add(cp.v(chainEnd1.getPos().x, tableBackNode.getPosition().y + (tableBackNode.getContentSize().height * 0.5)), boxOffset));
chainTableJoint2 = new cp.PivotJoint(chainEnd2, tableBackBody, cp.v.add(cp.v(chainEnd2.getPos().x, tableBackNode.getPosition().y + (tableBackNode.getContentSize().height * 0.5)), boxOffset));
space.addConstraint(chainTableJoint1);
space.addConstraint(chainTableJoint2);
}
you have four options:
1)- override Layer::onEnter() or onEnterTransitionDidFinish ()
methods then access from theme like this:
Layer::onEnter(){
Layer::onEnter();
auto world = _director->getRunningScene()->getPhysicsWorld();
}
2)- create a custom Layer::createScene() method then access from Layer::init() like this:
Scene* GameScene::createScene()
{
auto layer = new (std::nothrow) GameScene;
if (layer)
{
auto scene = Scene::createWithPhysics();
layer->autorelease();
scene->addChild(layer);
if (layer->init()) return scene;
}
CC_SAFE_DELETE(layer);
return nullptr;
}
// then in Layer::init() you do this:
Layer::init(){
auto world = _director->getRunningScene()->getPhysicsWorld();
}
3)- add a PhysicsWorld getter method to the director (involves some library code customization's):
first go to CCDirector.h and then add a forward declaration of the scene class under NS_CC_BEGIN like this:
NS_CC_BEGIN
class cocos2d::Scene;
then go to the private section of the Director class and add these two lines like this:
private:
cocos2d::PhysicsWorld* _myPhysicsWorld;
friend class Scene;
then go to the public section of the Director class and add this getter method like this:
public:
cocos2d::PhysicsWorld* getWorld() const{
return _myPhysicsWorld;
};
this is an overview of the changes made in CCDirector.h file:
// CCDirector.h
NS_CC_BEGIN
// 1
class cocos2d::Scene;
class Director : public Ref{
public:
//2
cocos2d::PhysicsWorld* getWorld() const{
return _myPhysicsWorld;
};
private:
// 3
cocos2d::PhysicsWorld* _myPhysicsWorld;
friend class cocos2d::Scene;
};
NS_CC_END
so after that we go to CCScene.cpp into a method with this signature (bool Scene::initWithPhysics()) in it you will find a call to PhysicsWorld::construct(this) add just right after that method call this line:
_director->_myPhysicsWorld = _physicsWorld;
and this should be the overview of what we did in CCScene.cpp:
// CCScene.cpp
class Scene : public Node{
bool Scene::initWithPhysics(){
_physicsWorld = PhysicsWorld::construct(this);
// 4 since Director is a singleton class, we can access its instance from anywhere
_director->_myPhysicsWorld = _physicsWorld;
}
};
now as soon as Scene::createWithPhysics() gets called anywhere the director will get a copy of the PhysicsWorld pointer which can be accessed using our director getWorld() method anywhere anytime!! (because the director is a singleton as you know) and thats all without exposing _myPhysicsWorld to the user meaning he can only read _myPhysicsWorld from the outside!!
4)- make your own Custom PhysicsWorld class and since PhysicsWorld::construct() is protected which means it can be easily inherited.
// PhysicsManager.h
class PhysicsManager : public cocos2d::PhysicsWorld
{
public:
static PhysicsManager* createWorld(cocos2d::Scene* scene) {
_myPhysicsWorld = PhysicsWorld::construct(scene);
}
PhysicsWorld* getWorld() const { return _myPhysicsWorld; }
private:
static cocos2d::PhysicsWorld* _myPhysicsWorld;
};
//PhysicsManager.cpp
PhysicsManager::PhysicsWorld* _myPhysicsWorld;
now you can use it like this:
Layer::init(){
auto physicsManager = PhysicsManager::createWorld(this->getScene());
auto world = physicsManager->getWorld();
}
remember that you can even make it a singleton class if you want to!
Edit:
I forgot to mention that there is another good solution in here:
http://discuss.cocos2d-x.org/t/physics-joint-distance-problem/17926/2

pass argv[] from main to other class

Hi I have a question about passing argv[] value to the other class. In my code, I want to pass the argv[1] parameter from cmd to the class mainWindow to trigger spacific event. Here is the code. #include "MainWindow.h"
int main(int argc, char *argv[])
{
#if _DEBUG
// Do not print memory leaks at end of program (QtWebKit causes a large number of them, see Menu class)
_CrtSetDbgFlag(_crtDbgFlag &~ _CRTDBG_LEAK_CHECK_DF);
#endif
QApplication app(argc, argv);
app.setApplicationName("Example_Qt");
MainWindow window;
window.show();
return app.exec();
}
MainWindow.cpp
MainWindow::MainWindow() :
m_pCurrentTutorial(0),
m_pCurrentTutorialAREL(0)
{
setupUi(this);
quitTutorialButton->setVisible(false);
QObject::connect(quitTutorialButton, SIGNAL(clicked()), this, SLOT(onQuitTutorialButtonClicked()));
m_pMenu = new Menu(this, this);
// Init the main view for the scene using OpenGL
QGLWidget *glWidget = new QGLWidget(QGLFormat(QGL::SampleBuffers));
m_pGraphicsView = graphicsView;
m_pGraphicsView->setScene(m_pMenu);
m_pGraphicsView->setViewport(glWidget);
m_pGraphicsView->setFrameShape(QFrame::NoFrame);
// Do not show context menu in web view
m_pGraphicsView->setContextMenuPolicy(Qt::NoContextMenu);
}
MainWindow::~MainWindow()
{
delete m_pMenu;
m_pMenu = 0;
delete m_pGraphicsView;
m_pGraphicsView = 0;
}
Mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <vector>
#include <QVBoxLayout>
#include <QWidget>
#include <QWebView>
class TutorialBase;
class TutorialBaseAREL;
#include "Menu.h"
#include "ui_MainWindow.h"
QT_BEGIN_NAMESPACE
class QGraphicsView;
class QBoxLayout;
QT_END_NAMESPACE
class MainWindow : public QMainWindow, public Ui::MainWindow, public Menu::TutorialSelectionCallback
{
Q_OBJECT
public:
MainWindow();
virtual ~MainWindow();
QBoxLayout* getButtonBar();
protected slots:
void onQuitTutorialButtonClicked();
protected:
void keyPressEvent(QKeyEvent *event);
void quitTutorialIfAny();
void startTutorial(int tutorialNumber);
void startTutorialAREL(int tutorialNumber);
TutorialBase* m_pCurrentTutorial;
TutorialBaseAREL* m_pCurrentTutorialAREL;
QGraphicsView* m_pGraphicsView;
Menu* m_pMenu;
};
#endif
Could anyone help me to pass the parameter from the argv[] to mainwindow.cpp ?
Many thanks
Ying
You should be able to just provide another constructor (or replace the current one) which takes a single char * argument, such as:
MainWindow::MainWindow (char *arg) :
m_pCurrentTutorial(0),
m_pCurrentTutorialAREL(0)
{
// body here, including checking argument, such as:
if (strcmp (arg, "-help") == 0) {
provideHelp();
}
}
and then use that constructor when creating your object:
MainWindow window (argv[1]);
Be aware that this new constructor will need to be listed in the class (in Mainwindow.h), not just added as a function to Mainwindow.cpp:
public:
MainWindow();
MainWindow(char*);
virtual ~MainWindow();

CCBMemberVariableAssigner callbacks disfunctional for custom CCTableViewCell in CocosBuilder

my goal is to design a CCTableViewCell via CocosBuilder
and load it via CCBReader.
My steps so far:
I added a new non-fullscreen CCNode TestCell.ccb in cocosbuilder,
set the root's custom class to TestCell
and added a CCSprite as the roots child while setting
it's doc-root-var to bg.
My problem: after implementing a loader TestCellLoader
as well as the Cell TestCell the callback-function
TestCell::onAssignCCBMemberVariable is not called at all.
What I tried:
Using a CCLayerLoader instead of a CCNodeLoader worked for me so far,
this is the first time I'm using a CCNodeLoader so maybe I missed
a crucial point.
Thank you,
Ciao!
Ben
Here are the codes:
TestCellLoader.h
#include <cocos2d.h>
#include "cocos-ext.h"
#include "TestCell.h"
using namespace cocos2d;
using namespace cocos2d::extension;
class TestCellLoader : public CCNodeLoader
{
public:
CCB_STATIC_NEW_AUTORELEASE_OBJECT_METHOD(TestCellLoader, create);
protected:
CCB_VIRTUAL_NEW_AUTORELEASE_CREATECCNODE_METHOD(TestCell);
virtual CCNode* loadCCNode(CCNode *, CCBReader * pCCBReader);
};
TestCellLoader.cpp
#include "TestCellLoader.h"
CCNode * TestCellLoader::loadCCNode(CCNode * pParent, CCBReader * pCCBReader)
{
CCLOG("TestCell::loadCCNode");
CCNode * ccNode = this->createCCNode(pParent, pCCBReader);
return ccNode;
}
TestCell.h
class TestCell : public CCTableViewCell, public CCNodeLoaderListener, public CCBMemberVariableAssigner
{
public:
TestCell();
virtual ~TestCell();
static TestCell *create();
virtual bool init();
virtual bool initWithBG(CCSprite* bg);
static TestCell* cellWithBG(CCSprite* bg);
// ccbuilder callbacks
virtual bool onAssignCCBMemberVariable(cocos2d::CCObject * pTarget, const char * pMemberVariableName, cocos2d::CCNode * pNode);
virtual void onNodeLoaded(cocos2d::CCNode * pNode, cocos2d::extension::CCNodeLoader * pNodeLoader);
private:
CC_PROPERTY(CCSprite*, bg, Bg);
};
TestCell.m
#include "TestCell.h"
using namespace cocos2d;
using namespace cocos2d::extension;
TestCell::TestCell(){}
TestCell::~TestCell(){}
#pragma mark creation
TestCell* TestCell::create(){
TestCell *pRet = new TestCell();
pRet->init();
pRet->autorelease();
return pRet;
}
bool TestCell::init(){
return true;
}
bool TestCell::initWithBG(CCSprite* bg){
return true;
}
TestCell* TestCell::cellWithBG(CCSprite* bg){
return new TestCell;
}
#pragma mark - synthesize
void TestCell::setBg(cocos2d::CCSprite *sprite){
this->bg = sprite;
}
CCSprite* TestCell::getBg(){
return this->bg;
}
#pragma mark - ccbuilder callbacks
void TestCell::onNodeLoaded(cocos2d::CCNode * pNode, cocos2d::extension::CCNodeLoader * pNodeLoader)
{
CCLOG("TestCell::onNodeLoaded");
}
bool TestCell::onAssignCCBMemberVariable(CCObject* pTarget, const char* pMemberVariableName, CCNode* pNode)
{
CCLOG("TestCell::onAssignCCBMemberVariable %s", pMemberVariableName);
return false;
}
I guess you used a CCLayer as TestCell.ccb's Root object type. Since when you are creating a new ccb file, CCLayer is as default option.
And this is why you use a CCLayerLoader instead of a CCNodeLoader worked.
So change your TestCell.ccb's Root object type to CCNode, this maybe works.

Run code before class instanciation in ActionScript 3

I need to run code in a class declaration before its instanciation. This would be especially useful to automatically register classes in a factory. See:
// Main.as
public class Main extends Sprite
{
public function Main() : void
{
var o : Object = Factory.make(42);
}
}
// Factory.as
public class Factory
{
private static var _factory : Array = new Array();
public static function registerClass(id : uint, c : Class) : void
{
_factory[id] = function () : Object { return new c(); };
}
public static function make(id : uint) : Object
{
return _factory[id]();
}
}
// Foo.as
public class Foo
{
// Run this code before instanciating Foo!
Factory.registerClass(CLASS_ID, Foo);
public static const CLASS_ID : uint = 42;
}
AFAIK, the JIT machine for the ActionScript language won't let me do that since no reference to Foo is made in the Main method. The Foo class being generated, I can't (and don't want to) register the classes in Main: I'd like to register all the exported classes in a specific package (or library). Ideally, this would be done through package introspection, which doesn't exist in ActionScript 3.
Do you know any fix (or other solution) to my design issue?
I'm not 100% sure sure if this is what you're after, but have you tried using a Static Initializer?
public class Foo
{
// Static Initializer
{
Factory.registerClass(CLASS_ID, Foo);
}
public static const CLASS_ID : uint = 42;
}
http://life.neophi.com/danielr/2006/12/static_initializers_in_as3.html
You can use compiler options to include class byte code in the resulting SWF or SWC. But you have to compile with MXMLC (or COMPC for SWCs).