How to add a cocos2d-x genie

Source: Internet
Author: User

1.1 add resources on the iPhone

1.2 add resources on Android

1.3 add resources on win32

1.4 add resources on vphone

2. Add an genie

Key Aspect 1

Key Aspect 2

The source code in the tutorial can be downloaded here: https://github.com/flyingpacer/Cocos2dxSimpleGame

You can complete the game step by step based on the article, or directly download the complete code and compile and run it easily.

1. Add image resources

There are three pictures made by Ray Wenderlich's wife, which will be used in Cocos2dSimpleGame.

Chapter 1-after creating a new cocos2d-x project on multiple platforms, you should now have a cocos2d-x/Cocos2dxSimpleGame folder. Download these images and copy them to the cocos2d-x/Cocos2dxSimpleGame/Resources folder.

Then return to the IDE on various platforms.

1.1 add resources on the iPhone

Very simple. Open Xcode and click Add-> Existing Files from the context menu of Cocos2dxSimpleGame/Resources group to Add the above three images. Note that you should check Cocos2dxSimpleGame in the "Add To Targets" column.

1.2 add resources on Android

If you run build_native.sh to compile the file, copy the image to the Resources folder. Otherwise, copy the image file to the assets folder.

1.3 add resources on win32

The executable file of Win32 looks for resources in its related path. So we must manually copy the image file from the cocos2d-x/Cocos2dxSimpleGame/Resources folder to the cocos2d-x/Debug. win32 folder.

But a lazy like me can always find a way to be lazy.

Write this Line to Post-Build Event-> Command Line

Xcopy/Y/E. \ Resources \ *. * $ (OutDir)

Upon completion of each compilation, VistualStudio copies the resource to the executable path.

1.4 add resources on vphone

It is highly recommended that you use a zip package in a vphone game. Read How to use ZIP resource package on cocos2d-uphone if you're still not clear about it

2. Add an genie

Now you will find how easy it is to port cocos2d games from objc to c ++. Open HelloWorldScene. cpp and replace init as follows:

1 bool HelloWorld: init ()

2 {

3 bool bRet = false;

4 do

5 {

6 /////////////////////////////////////// ///////////////////////////////////

7 // super init first

8 /////////////////////////////////////// ///////////////////////////////////

9

10 CC_BREAK_IF (! CCLayer: init ());

11

12 /////////////////////////////////////// ///////////////////////////////////

13 // add your codes below...

14 /////////////////////////////////////// ///////////////////////////////////

15

16 // 1. Add a menu item with "X" image, which is clicked to quit the program.

17

18 // Create a "close" menu item with close icon, it's an auto release object.

19 CCMenuItemImage * pCloseItem = CCMenuItemImage: itemFromNormalImage (

20 "CloseNormal.png ",

21 "CloseSelected.png ",

22 this,

23 menu_selector (HelloWorld: menuCloseCallback ));

24 CC_BREAK_IF (! PCloseItem );

25

26 // Place the menu item bottom-right conner.

27 pCloseItem-> setPosition (ccp (CCDirector: shareddire()-> getWinSize (). width-20, 20 ));

28

29 // Create a menu with the "close" menu item, it's an auto release object.

30 CCMenu * pMenu = CCMenu: menuWithItems (pCloseItem, NULL );

31 pMenu-> setPosition (CCPointZero );

32 CC_BREAK_IF (! PMenu );

33

34 // Add the menu to HelloWorld layer as a child layer.

35 this-> addChild (pMenu, 1 );

36

37 /////////////////////////////

38 // 2. add your codes below...

39

40 CCSize winSize = CCDirector: sharedDirector ()-> getWinSize ();

41 CCSprite * player = CCSprite: spriteWithFile ("Player.png ",

42 CCRectMake (0, 0, 27, 40 ));

43 player-> setPosition (ccp (player-> getContentSize (). width/2, winSize. height/2 ));

44 this-> addChild (player );

45

46 bRet = true;

47} while (0 );

48

49 return bRet;

50}

In the above code, I actually only added the content in section 2.Add your codes below. You can see from the comparison below how to easily turn cocos2d-iphone code into cocos2d-x cross-platform code line by line.

1 // cpp with cocos2d-x

2 bool HelloWorld: init ()

3 {

4 if (CCLayer: init ())

5 {

6 CCSize winSize = CCDirector: sharedDirector ()-> getWinSize ();

7 CCSprite * player = CCSprite: spriteWithFile ("Player.png ",

8 CCRectMake (0, 0, 27, 40 ));

9 player-> setPosition (ccp (player-> getContentSize (). width/2,

10 winSize. height/2 ));

11 this-> addChild (player );

12}

13 return true;

14}

1 // objcwith cocos2d-iphone

2-(id) init

3 {

4 if (self = [super init])

5 {

6 CGSize winSize = [[CCDirector shareddire] winSize];

7 CCSprite * player = [CCSprite spriteWithFile: @ "Player.png"

8 rect: CGRectMake (0, 0, 27, 40)];

9 player. position = ccp (player. contentSize. width/2,

10 winSize. height/2 );

11 [self addChild: player];

12}

13 return self;

14}

Key Aspect 1

1. Do not use _ super in C ++ to replace the super in objc. Keyword _ super can only be identified in VC ++, but cannot be compiled by GCC. Therefore, you 'd better call the parent class name, CCLayer: init ()

2. There is no attribute concept here. Therefore, we use the get/set method to replace the attributes in objc. For example, if you want to obtain the contentSize attribute of CCSprite, you must call the sprite-> getContentSize () method. The first letter of contentSize should be an uppercase "C", followed by a "get" prefix.

3. Use setter to set the attribute value, "player. position = ...", The response is player-> setPosition (...)

4. However, the members accessing the struct do not follow this rule. For example, there is no getter/setter for "width" & "height" in the winSize struct.

5. You do not need to explain the purpose of each parameter like objc. For example, [CCSprite spriteWithFile ..., Rect…]; Directly convert to CCSprite: spriteWithFile (..., ....);

6. We have completed some common CGGeometry functions, such as CGRectMake, CGPointMake, CGSizeMake, CGPointZero, CGSizeZero, and CGRectZero. You can find them in cocos2dx/include/CCGemoetry. h. They are used in the same way as iOS. Only a little difference lies in their prefix, in order to avoid naming conflicts, in the cocos2d-x, prefix for CG, NS, UI classes are unified into the CC prefix.

7. All game elements in cocos2d-x, such as sprite, layer, scene, label, and action, are allocated with heap memory. Therefore, we must use the pointer "->" to call their methods.

8. In cpp, use the keyword "this" to replace "self" in objc"

9. Now the return value of the init method is of the "bool" type. There is no keyword "id" in cpp, so the methods that return the value of "id" either replace it with the object pointer or change it to bool.

10. for Android, the title bar occupies some space, so you need to set the player position (player. contenSize. width/2 + 40, winSize. height/2 ).

Now, we can compile and run the code. Now, as the ninja attacked the black suit, they were hiding in the dark with a red eye. To play games, we must change the background to white. This is very simple. Modify HelloWorld to inherit CCLayerColor instead of CCLayer.

First, modify the Declaration section of HelloWorldScene. h.

1 // cpp with cocos2d-x

2 class HelloWorld: public cocos2d: CCLayerColor

1 // objcwith cocos2d-iphone

2 @ interface HelloWorld: CCLayerColor

Then modify the initial part of HelloWorld: init ().

Set

1if (! CCLayer: init ())

2 {

3 return false;

4}

Change

1if (! CCLayerColor: initWithColor (ccc4 (255,255,255,255 )))

2 {

3 return false;

4}

Some of the code here is different from RayWenderlich, because I prefer the defensive code style. The normal code is that if super init is successful, then executes something, and I prefer if init fails, first handle errors, and then write the correct process. Now, let's go back to the topic. Let's continue to compare the conversions between c ++ and objc.

1 // cpp with cocos2d-x

2if (CCLayerColor: initWithColor (ccc4 (255,255,255,255 )))

1 // objcwith cocos2d-iphone

2if (self = [super initWithColor: ccc4 (255,255,255,255)])

Key Aspect 2

1. In c ++, inheritance is private by default. Therefore, you must add "public" before the CCLayerColor class declaration ".

2. RicardoQuesada, the principal author of the Cocos2d-iphone, recommends that we use namespaces in the cocos2d-x. It is important to check whether the cocos2d-x class you are calling is in the cocos2d namespace or in the CocosDenshion namespace.

Compile and run the program. Then you will see the little hero standing in the white background alone.

IPhone:

Android:

Vphone:

Win32:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.