I'm currently porting an ObjC cocos2d game to cocos2d-x, but I'm encountering some problems when trying to create a registerWithTouchDispatcher method, at the moment I'm doing
void GameLayer::registerWithTouchDispatcher()
{
CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this,0,true);
}
but this gives an error 'No member named sharedDispatcher' in cocos2d::CCTouchDispatcher'.
Is there another way that this must be done in cocos2d-x?
If you are using 2.0, they have been merged in to CCDirector.
please use
CCDirector::sharedDirector()->getTouchDispatcher()
use those code instead ccdirector. put the code to cclayer init function.
setTouchMode(kCCTouchesOneByOne);
registerWithTouchDispatcher();
In the cocos2d-x you can do like this.
CCDirector::sharedDirector()->getTouchDispatcher()->addStandardDelegate(this,0);
Related
I've been trying to get the acceleromter to work in cocos2d-x c++.
I tried to add this function to my scene which a lot of my Google search results said I should:
virtual void didAccelerate(Acceleration *acceleration);
That does however give me an error saying I'm overriding a final function.
Then I found how I could use the EventDispatcher thing for it.
auto accListener = EventListenerAcceleration::create(CC_CALLBACK_2(MainScene::accelerated, this));
getEventDispatcher()->addEventListenerWithSceneGraphPriority(accListener, this);
In both of these I had called this function earlier in my scene's init function:
setAccelerometerEnabled(true);
I'm all out of ideas and I need help. On the second "approach" it compiled, but my accelerated function was never called.
Thank you in advance!
I'm using Android so maybe I need to edit something in the AndroidManifest?
The guys at the cocos forums linked me to this:
Cocos docs
As you can see the correct way to go is to use the EventListener approach as described earlier. However you need to call
Device::setAccelerometerEnabled(true);
Calling
setAccelerometerEnabled(true);
From the scene's init doesn't work, you need to call the static method on 'Device'.
So here's how you do it.
Device::setAccelerometerEnabled(true);
auto accListener = EventListenerAcceleration::create(CC_CALLBACK_2(MainScene::accelerated, this));
getEventDispatcher()->addEventListenerWithSceneGraphPriority(accListener, this);
Where accelerated is takes these parameters:
void accelerated(Acceleration *acceleration, Event *event);
Of course this function can be called anything as the EventListener takes a function pointer.
Tritzium answered it right. Just wanted point out:
You wont get accelerator events in simulator, you need a device.
I've just used cocos2d-x for creating some games. When I read the HelloWorld.cpp, I saw this line
Scene* HelloWorld::createScene()
That's strange for me. How does it work? A method named creatScene that takes no parameters and returns a pointer to a Scene ?
In different libraries, there are different methods to initialize library or some part of that. So, in this case, it maybe create a new context inside library and return it without any argument. It maybe needs no arguments (use defaults) it this step of get them from somewhere else like configuration file. And note that this is convenient to use this type of initializing. Like this:
rc = redis.Redis() #uses default values for server address
It is really an easy question even it cannot be called as question when you check the source code.
In cocos2d-x, CCScene always create this way.
1. create a Layer, which coded by yourself with a lot of other widgets.
2. create a Scene
3. add the layer to the scene
4. return the scene you create.
I am programming with Cocos2d 3.0 now, In Cocos2d 2.0, we can use the following code to add accelerometer to app, but this example was based on class CCLayer which has deprecate in Cocos2d 3.0, and UIAccelerometer also replaced by CMMotionManager in IOS 5.0, so I am wondering how to do this in Cocos2d 3.0? I googled for a while, didn't find anything useful.
-(id) init
{
if ((self = [super init]))
{
// ...
self.isAccelerometerEnabled = YES;
// ...
}
}
-(void) accelerometer:(UIAccelerometer *)accelerometer
didAccelerate:(UIAcceleration *)acceleration
{
// ...
}
===
We've written a tutorial on exactly this: https://www.makegameswith.us/gamernews/371/accelerometer-with-cocos2d-30-and-ios-7
You need to use the CoreMotion framework.
Well, there are two problems in the tutorial example given above.
Single instance of CMMotionManager.
Acceleration data become +Ve or -Ve according to the orientation of the device. You also need to add Scene as observer of device orientation change notification.
If you don't want handle these overheads, you can use CCAccelerometer class. It solves both the problems.
HOW TO USE
Add CoreMotion Framework in your project from Build Phases.
Copy CCAccelerometer.h and CCAccelerometer.m files in your project.
Import CCAccelerometer.h file in the Prefix.pch.
Implement the <CCSharedAccelerometerDelegate> in the CCScene where you want to use the accelerometer.
Create shared instance in init method by simply calling [CCAcceleroMeter sharedAccelerometer];
Start accelerometer in -(void)onEnterTransitionDidFinish by calling [CCAcceleroMeter sharedAccelerometer]startUpdateForScene:self];
Define delegate method -(void)acceleroMeterDidAccelerate:(CMAccelerometerData*)accelerometerData in your scene.
Stop accelerometer in -(void)onExitTransitionDidStart by calling [CCAcceleroMeter sharedAccelerometer]stopUpdateForScene:self];
You can find out the example project in GitHub.
Here is example:
Device::setAccelerometerEnabled(true);
auto accelerometerListener = EventListenerAcceleration::create([this](Acceleration* acc, Event* event)
{
});
getEventDispatcher()->addEventListenerWithSceneGraphPriority(accelerometerListener, this);
Also video tutorial https://www.youtube.com/watch?v=Xk6lXK6trxU
I am translating a piece of code from cococs2D to cocos2D-X. I came across the following lines that i cannot fathom out how to translate
[spriteBg runAction:[CCSequence actions:sc,[CCCallFuncO actionWithTarget:basketTimer_ selector:NSSelectorFromString([selectors objectAtIndex:0]) object:sprite], nil]];
Can someone please help me translate this to Cocos2d in Cocos2d-X ?
Kind Regards,
try this..
spriteBg->runAction::create(CCSequence::create(sc,CCCallFunc::create(this, callfunc_selector(myMethod)),NULL));
In your code the myMethod is replaced by the selector at the index 0 of the "selectors" which I'm guessing is an array of the selectors or a dictionary or something like that.
and "sc" is a predefined action which is to be run on spriteBg.
For cocos2d-x v 2.2.1:
NSSelectorFromString([selectors objectAtIndex:0] <- in cocos2d-x you can't create selector from string, so you must know function you want call, or keep selectors in container(but I never do this)
CCCalFuncO *call = CCCallFuncO::create(basketTimer_, callfuncO_selector(BasketTimerClass::BasketTimerMethod), sprite)
spriteBg->runAction(CCSequence::create(sc, call, NULL));
Every class in cocos2d-x and cocos2d-iphone have the same name, so you can easly find it in documentation:
CCSequence CCCalFuncO
I connected C++ and QML via a mediator-class and have everything working in both directions but this one puzzles me.
This is how I connect the mediator-class:
// Initialize Mediator between QML and C++
QmlCppMediator m_qmlCppMediator;
QDeclarativeContext *context = viewer.rootContext();
context->setContextProperty("cppInterface", &m_qmlCppMediator);
How to fire off an ordinary Property-Animation from within C++ ?
Ok I can answer this myself already.
I went for an approach described here http://qt-project.org/doc/qt-4.8/qdeclarativeanimation.html
I bind the “state” of the object which I try to animate to a Q_PROPERTY in the C++ interface.
The different states are linked to transitions (in QML) which do the animate the object.
Rather an easy way would be to define a JavaScript function inside the QML file itself, lie this:
function startAnimation() {
animationID.running = true;
}
Now call this code from C++, simple!