Chapter 2 - How to Add a sprite
Chapter 2 - How to Add a sprite
- 1. Add image resources
- 2. Add a sprite
- The source code of these tutorials is here: https://github.com/flyingpacer/Cocos2dxSimpleGame. You can follow the articles to finish the game by yourself step by step, or download the finally source, simply build and run it.
1. Add image resources
Here’re three images made by Ray Wenderlich ’s wife , which would be used in Cocos2dSimpleGame.
After Chapter 1 - Create a New cocos2d-x project with multi-platforms, you must have cocos2d-x/Cocos2dSimpleGame folder now. Please download these images and copy them to cocos2d-x/Cocos2dSimpleGame/Resources folder
Then return to the IDE of different platforms.
1.1 add resources on win32
Win32 executable files will find the resources from it’s relative path. So we had to copy the images from cocos2d-x/Cocos2dSimpleGame/Resources folder to cocos2d-x/Debug.win32 folder manually.
But lazy guys like me, can always find lazy ways.
Write this line into Post-Build Event -> Command Line
xcopy /Y /E .\Resources\*.* $(OutDir)Each time the building completed, VistualStudio will copy the resources to the executable path
2. Add a sprite
You can see how easy to port cocos2d games from objc to c++ now. Open HelloWorldScene.cpp, replace the init method with following.
1bool 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::create(
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::sharedDirector()->getWinSize().width - 20, 20));
28
29 // Create a menu with the "close" menu item, it's an auto release object.
30 CCMenu* pMenu = CCMenu::create(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::create("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}
Beyonds the close menu, we only add the section of “2. Add your codes below” actually. You can see how to translate cocos2d-iphone codes line by line simply to cocos2d-x multi-platform codes.
1// cpp with cocos2d-x
2bool HelloWorld::init()
3{
4 if ( CCLayer::init() )
5 {
6 CCSize winSize = CCDirector::sharedDirector()->getWinSize();
7 CCSprite *player = CCSprite::create("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}
TIPS 1
- Don’t use __super in C++ like used in objc. The keyworkd __super is only recognized by VC++, but can not be compiled by GCC. So you better to call the name of parent class, CCLayer::init()
- There’s no the concept of property as there is in objc. Use get/set methods instead. For example, if you want to fetch the contentSize property of CCSprite , you must call sprite->getContentSize() method. Make the first letter capital, then add “get” prefix to it.
- Use setter to set the value of properties. So “player.position = …” , translates to player->setPosition(…)
- But the access to members of structures isn’t following this rule. E.g. there’re no getter/setter wrapper(包装器) with the “width” & “height” in winSize structure.
- We have implemented some frequently used functions of CGGeometry(几何类), such as CGRectMake, CGPointMake, CGSizeMake, CGPointZero, CGSizeZero, CGRectZero. You can find them in cocos2dx/include/CCGeometry.h. Their features are same as iOS. For naming conflicts, classes with CG, NS, UI prefix, has been changed to CC prefix in cocos2d-x.
- All the gaming elements in cocos2d-x, such as sprite, layer, scene, label, action, are allocated in the heap(堆). So we must call their methods by “->”
- Use keyword “this” in cpp instead of “self” used in objc.
- The return type of init method is “bool” now. There’s no keyword “id” in cpp, so the methods returning “id” is translated to a object pointer or bool.
- For android, the title bar has ocupied some space, so you should set the player's position to ccp(player.contentSize.width/2 + 40, winSize.height/2).
Well, we can build and run the code. Now the ninja is dressed in black, hidden in the black background with red eyes. For the gameplay(游戏玩法,可玩性), we had to change the background to white. It’s so easy, modify HelloWorld to inherit from CCLayerColor instead of CCLayer.
At first, modify the declaration in HelloWorldScene.h
1// cpp with cocos2d-x
2class HelloWorld : public cocos2d::CCLayerColor
Then modify the very beginning of HelloWorld::init() from 1if ( !CCLayer::init() )
2{
3 return false;
4}
to 1if ( !CCLayerColor::initWithColor( ccc4(255,255,255,255) ) )
2{
3 return false;
4}
Here’s a bit different to RayWenderlich’s code, because prefer defensive programming(防御式编程). The normal code is if supoer init success, then do bla-bla…, I prefer if init failed, do the error handling first, then continue writing the correct flow. OK, back to the topic. Let’s compare the translation of objc to cpp again
1// cpp with cocos2d-x
2 if ( CCLayerColor::initWithColor( ccc4(255,255,255,255) ) )
TIP 2
- The default permission of inheritance in c++ is private. So the modifier “public” before CCLayerColor is required.
- RicardoQuesada, the chief author of cocos2d-iphone , suggest us to use namespace in cocos2d-x. It’s very important to check your invoking of cocos2d-x classes is in “cocos2d” namespace or in “CocosDenshion” namespace
Build and run, then you can see the hero standing lonely in the white background.
- Win32
Chapter 2 - How to Add a sprite的更多相关文章
- Chapter 3 - How to Move a sprite
http://www.eoeandroid.com/forum.php?mod=viewthread&tid=250529 http://www.cocos2d-x.org/boards/6/ ...
- Fedora 22中的用户和用户组管理
The control of users and groups is a core element of Fedora system administration. This chapter expl ...
- cocos2dx 实现flappybird
前两天在博客园看到网友实现的一个网页版的flappy bird,挂在360游戏平台,玩了一会儿得分超低,就很想自己做一个.刚好这两天炫舞的活都清了,就弄一下玩玩. 效果图 布局类GameScene.h ...
- The Engine Document of JustWeEngine
JustWeEngine - Android FrameWork An easy open source Android Native Game FrameWork. Github Game core ...
- 【ASP.NET Identity系列教程(二)】运用ASP.NET Identity
注:本文是[ASP.NET Identity系列教程]的第二篇.本系列教程详细.完整.深入地介绍了微软的ASP.NET Identity技术,描述了如何运用ASP.NET Identity实现应用程序 ...
- cocos2d-x 第二篇 HelloWorld的流程
这篇博客主要是带领大家一起了解整个游戏的执行过程,其中涉及的一些譬如导演,场景,层之类的概念将会在后面讲解. 看main函数的区别: #import <UIKit/UIKit.h> // ...
- [原创]cocos2d-x研习录-第二阶 基本框架
了解完Cocos2D-x的基本概念和概念类之后,是不是有一种蠢蠢欲动的冲动,想要探究Cocos2D-x是如何完成这一切的.接着我将通过对Cocos2D-x自代的HelloCpp项目进行分析,初步了解C ...
- cocos2d::Vector
C++中的vector使用范例 一.概述 vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector是一个容器,它能够存放各种类型的对象,简 ...
- 如何使用box2d做碰撞检测
cocos2dx3.0+vs2012编译通过. 主要是通过body->SetTransform来设置body的位置和角度,然后自己写个ContactListener来监听碰撞事件 源代码下载 # ...
随机推荐
- 第 5 章 工厂方法模式【Factory Method Pattern】
以下内容出自:<<24种设计模式介绍与6大设计原则>> 女娲补天的故事大家都听说过吧,今天不说这个,说女娲创造人的故事,可不是“造人”的工作,这个词被现代人滥用了.这个故事是说 ...
- OneAPM Cloud Test——系统性能监控神器
2015 年 8 月,OneAPM 推出了一款系统性能监控产品--Cloud Test,产品上线以来以「两低一高」的特点迅速成为市场增长率最快的一匹黑马.「两低一高」,即低使用成本.低学习成本以及高服 ...
- 移动应用产品开发-android开发 新闻模块开发 百度Frontia组件应用之分享
这两天主要做了新闻模块的开发,做了新闻列表,新闻详情,数据结构解析,以及百度 Frontia 组件的研究. 新闻模块用的是开源中国的android开源代码里的代码,主要是模仿它的源码架构,首先打开是资 ...
- Android 模拟登陆 保存密码(信息)到手机中 文件信息读取
package com.wuyou.login; import java.io.IOException; import java.util.Map; import android.app.Activi ...
- DHTMLX 前端框架 建立你的一个应用程序教程(二)--设置布局
Layout控件的演示 Dhtmlx有很多的组建来组织网页的建设, 这篇主要介绍dhtmlxLayout . 下面图片中 布局将各个组件(1.Menu 2.Toolbar 3.Grid 4.Form ...
- BZOJ_1565_[NOI2009]_植物大战僵尸_(Tarjan+最大流+最大权闭合图)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1565 n*m的矩阵,可以种植植物,僵尸从图的右边进入吃植物.前面的植物可以保护后面的植物,还有 ...
- (转载)C++中, 构造函数和析构函数能不能被显示调用?
(转载)http://blog.csdn.net/zhangxinrun/article/details/6056321 代码: view plaincopy to clipboardprint?#i ...
- 打开URL操作
/* * 打开 url 的两种方法 */ public class OpenUrl { public static void main(String[] args) { String url = &q ...
- Ubuntu12.04-64bits搭建FFmpeg环境
所有的环境搭建动作请参考FFMpeg官方网站:http://www.ffmpeg.org/index.html 1. 获取源代码: git clone git://source.ffmpeg.org/ ...
- (DT系列二)device tree的书写规范
devicetree的书写规范 下面从节点,属性,reg,ranges,中断控制器等几个方面叙述devicetree的书写规范. 1,dts的基本元素:节点 .dts(或者其include的.dtsi ...