I have a game developed using Cocos2d-iphone and want to convert it to Cocos2d-X.
How I can convert the following code to cocos2d-x:
CCArray spiders = [[CCArray alloc] initWithCapacity:numSpiders];
I have a game developed using Cocos2d-iphone and want to convert it to Cocos2d-X.
How I can convert the following code to cocos2d-x:
CCArray spiders = [[CCArray alloc] initWithCapacity:numSpiders];
CCArray spiders = CCArray::arrayWithCapacity(num Spiders); //old version Cocos2d-x
CCArray spiders = CCArray::create(num Spiders); //new version of Cocos2d-x
Thanks for help ‘Alex Zhd’
It’s possible to describe how to convert this code
return [[[self alloc] initWithTargetScene:targetScene] autorelease];
targetScene is an Enum
typedef enum
{
TargetSceneINVALID = 0,
TargetSceneHelloWorldScene,
TargetSceneGameScene,
TargetSceneMAX,
} TargetScenes;
TargetScenes targetScene_;
Can you show source where is you get this code?
That links can help you - http://gameit.ro/2011/08/iphone-cocos2d-users-guide-to-cocos2d-x-part1/ and http://gameit.ro/2011/08/iphone-cocos2d-user’s-guide-to-cocos2d-x-part-2/
Yes, I get this source code from book
Learn cocos2d Game Development with iOS 5 (Learn Apress) by Steffen Itterheim and Andreas Löw
this is full source of the class that I try to convert
LoadingScene.h
#import
#import "cocos2d.h"
typedef enum
{
TargetSceneINVALID = 0,
TargetSceneFirstScene,
TargetSceneOtherScene,
TargetSceneMAX,
} TargetScenes;
// LoadingScene is derived directly from Scene. We don't need a CCLayer for this scene.
@interface LoadingScene : CCScene
{
TargetScenes targetScene_;
}
+(id) sceneWithTargetScene:(TargetScenes)targetScene;
-(id) initWithTargetScene:(TargetScenes)targetScene;
@end
LoadingScene.m
#import "LoadingScene.h"
#import "FirstScene.h"
#import "OtherScene.h"
@interface LoadingScene (PrivateMethods)
-(void) update:(ccTime)delta;
@end
@implementation LoadingScene
+(id) sceneWithTargetScene:(TargetScenes)targetScene;
{
CCLOG(@"===========================================");
CCLOG(@"%@: %@", NSStringFromSelector(_cmd), self);
// This creates an autorelease object of self (the current class: LoadingScene)
return [[[self alloc] initWithTargetScene:targetScene] autorelease];
// Note: this does the exact same, it only replaced self with LoadingScene. The above is much more common.
//return [[[LoadingScene alloc] initWithTargetScene:targetScene] autorelease];
}
-(id) initWithTargetScene:(TargetScenes)targetScene
{
if ((self = [super init]))
{
targetScene_ = targetScene;
CCLabelTTF* label = [CCLabelTTF labelWithString:@"Loading ..." fontName:@"Marker Felt" fontSize:64];
CGSize size = [[CCDirector sharedDirector] winSize];
label.position = CGPointMake(size.width / 2, size.height / 2);
[self addChild:label];
// Must wait one frame before loading the target scene!
// Two reasons: first, it would crash if not. Second, the Loading label wouldn't be displayed.
[self scheduleUpdate];
}
return self;
}
-(void) update:(ccTime)delta
{
// It's not strictly necessary, as we're changing the scene anyway. But just to be safe.
[self unscheduleAllSelectors];
// Decide which scene to load based on the TargetScenes enum.
// You could also use TargetScene to load the same with using a variety of transitions.
switch (targetScene_)
{
case TargetSceneFirstScene:
[[CCDirector sharedDirector] replaceScene:[FirstScene scene]];
break;
case TargetSceneOtherScene:
[[CCDirector sharedDirector] replaceScene:[OtherScene scene]];
break;
default:
// Always warn if an unspecified enum value was used. It's a reminder for yourself to update the switch
// whenever you add more enum values.
NSAssert2(nil, @"%@: unsupported TargetScene %i", NSStringFromSelector(_cmd), targetScene_);
break;
}
// Tip: example usage of the INVALID and MAX enum values to iterate over all enum values
for (TargetScenes i = TargetSceneINVALID + 1; i < TargetSceneMAX; i++)
{
}
}
-(void) dealloc
{
CCLOG(@"%@: %@", NSStringFromSelector(_cmd), self);
// don't forget to call "super dealloc"
[super dealloc];
}
@end
I think it should look like this:
LoadScenes.h
#ifndef LOADINGSCENE_H
#define LOADINGSCENE_H
typedef enum
{
TargetSceneINVALID = 0,
TargetSceneFirstScene,
TargetSceneOtherScene,
TargetSceneMAX
} TargetScenes;
#include "cocos2d.h"
USING_NS_CC;
class LoadingScene: public CCScene {
public:
LoadingScene();
static LoadingScene* sceneWithTargetScene(TargetScenes targetScene);
private:
virtual void update(ccTime dt);
bool initWithTargetScene(TargetScenes targetScene);
TargetScenes targetScene_;
};
#endif // LOADINGSCENE_H
LoadingScene.cpp
#include "LoadingScene.h"
#include "FirstScene.h"
#include "OtherScene.h"
LoadingScene::LoadingScene() {
}
LoadingScene *LoadingScene::sceneWithTargetScene(TargetScenes targetScene) {
LoadingScene aLoadingScene = new LoadingScene();
aLoadingScene->initWithTargetScene(targetScene);
aLoadingScene->autorelease();
// Note: this does the exact same, it only replaced self with LoadingScene. The above is much more common.
//return [[[LoadingScene alloc] initWithTargetScene:targetScene] autorelease];
}
bool LoadingScene::initWithTargetScene(TargetScenes targetScene) {
if(CCScene::init()) {
targetScene_ = targetScene;
CCLabelTTF* label = CCLabelTTF::labelWithString("Loading ...", "fonts/Marker Felt.ttf", 64);
CCSize size = CCDirector::sharedDirector()->getWinSize();
label->setPosition(ccp(size.width / 2, size.height / 2));
this->addChild(label);
// Must wait one frame before loading the target scene!
// Two reasons: first, it would crash if not. Second, the Loading label wouldn't be displayed.
this->scheduleUpdate();
return true;
}
return false;
}
void LoadingScene::update(ccTime delta) {
// It's not strictly necessary, as we're changing the scene anyway. But just to be safe.
this->unscheduleAllSelectors();
// Decide which scene to load based on the TargetScenes enum.
// You could also use TargetScene to load the same with using a variety of transitions.
switch (targetScene_)
{
case TargetSceneFirstScene:
CCDirector::sharedDirector()->replaceScene(FirstScene::node()); //or FirstScene::scene()
break;
case TargetSceneOtherScene:
CCDirector::sharedDirector()->replaceScene(OtherScene::node()); //or OtherScene::scene()
break;
default:
// Always warn if an unspecified enum value was used. It's a reminder for yourself to update the switch
// whenever you add more enum values.
CCAssert(NULL, "unsupported TargetScene %d", targetScene_);
break;
}
// Tip: example usage of the INVALID and MAX enum values to iterate over all enum values
for (TargetScenes i = TargetSceneINVALID + 1; i < TargetSceneMAX; i++)
{
}
}
This is great, the code works for me, I simple make little adjustments.
First:
I change this
LoadingScene aLoadingScene = new LoadingScene();
for
LoadingScene *aLoadingScene = new LoadingScene();
Only add the * symbol.
Second:
I add the return method
return aLoadingScene;
The following codes not work
CCAssert(NULL, "unsupported TargetScene %d", targetScene_);
and
for (TargetScenes i = TargetSceneINVALID + 1; i < TargetSceneMAX; i++)
//{
//}
But is not a problem because is for merely check errors when add more enums.
When i wrote this code, i not compile it, sorry, I just overlooked.
Alex Zhd, you help is very appreciated.
I know you help me a lot, but I still have some part of the code that I can’t convert
[self unschedule:@selector(spidersUpdate:)];
[self schedule:@selector(spidersUpdate:) interval:0.7f];
and this line:
CCSequence* sequence = CCSequence::actions(easeHang, easeEnd, callDidDrop, NULL);
and finally this:
void accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
float deceleration = 0.4f;
float sensitivity = 6.0f;
float maxVelocity = 100;
playerVelocity.x = playerVelocity.x * deceleration + acceleration.x * sensitivity;
if (playerVelocity.x > maxVelocity)
{
playerVelocity.x = maxVelocity;
}
else if (playerVelocity.x < -maxVelocity)
{
playerVelocity.x = -maxVelocity;
}
}
Sorry for asking so many things, but I’m new to c++
Please, read this guide, it’s help you - http://gameit.ro/2011/08/iphone-cocos2d-users-guide-to-cocos2d-x-part1/ and http://gameit.ro/2011/08/iphone-cocos2d-user’s-guide-to-cocos2d-x-part-2/
And read Cocos2d-x Wiki.
OK, I read the guides and help me much.
Thank you for your help ‘Alex Zhd’, you help me a lot.