cocos2d-X入门(win7+VS2012环境配置以及学习中遇到的问题)

现在我的电脑是win7 32位,因此我搜索到有关的安装方面的博客,下面可以参考下:

Sel - OSCHINA - 中文开源技术交流社区

Win7环境下VS2010配置Cocos2d-x-2.1.4最新版本的开发环境(亲测)_cocos2d-x win7 win10-CSDN博客

按照安装方法,遇到以下问题:

1)下载什么版本?

      一般的想法就是下载最新的版本,至少是最近更新的嘛。可是cocos2d这里做得不是很好,这方面有很多吐槽,可以自己一下。我试了不下与4个版本,最后才决定下载一个较为稳定的版本,我选的是2.2.1.  下载后一定要看,里面有没有tools文件夹,有几个版本下载是没有这个文件的,可能是作者上传时遗漏了,真的。而且,现在cocos2d已经不支持vs模板新建空文件,需要用Python编译。那么这里就需要考虑用什么版本的Python了,简而言之,用2.x就可以了,因为我用Python 3.3编译不了,没办法只能卸载了装2.7.原因到现在还没有找出来。(PS.  Python是个蛮好的脚本语言,可以学学)

      解决上面问题,安装是不会有问题的。我已经安装过两次了。就可以新建一个空的项目,然后运行得到久违的HelloWorld。

2)关于新建的空工程:

这里可以看到11个文件,这里Classes和Resource是公共文件夹,其他的是针对不同平台的文件夹。

现在打开proj.win32文件夹,以及打开helloworld.sln后可以看到:

                           

现在,就主要讲一下我们的这个空模板。它包含6个文件,3个.h和3个.cpp文件。下面主要介绍这几个文件的主要内容。

首先,main.h很简单,不用讲。

其次,main.cpp是整个程序的入口,也就是程序的第一行代码是从这里开始的。

int APIENTRY _tWinMain(HINSTANCE hInstance,
                       HINSTANCE hPrevInstance,
                       LPTSTR    lpCmdLine,
                       int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);
 
    AppDelegate app;
    CCEGLView* eglView = CCEGLView::sharedOpenGLView();
    eglView->setViewName("helloworld");
    eglView->setFrameSize(480, 320);
    return CCApplication::sharedApplication()->run();
}
       winmian函数的windows平台的main函数,前两行是window平台的句柄调用;第3行,是创建一个AppDelegate 应用;第4~6行,就是创建一个窗口,包括标题,大小等。第7行,就是运行整个程序。
      再次,就是AppDelegate.h和AppDelegate.cpp主要就是建立一个AppDelegate类。那么AppDelegate类干了些什么呢?

bool AppDelegate::applicationDidFinishLaunching() {
   
    CCDirector* pDirector = CCDirector::sharedDirector();
    CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();

    pDirector->setOpenGLView(pEGLView);	    
    pDirector->setDisplayStats(true);   
    pDirector->setAnimationInterval(1.0 / 60);    
    CCScene *pScene = HelloWorld::scene();    
    pDirector->runWithScene(pScene);

    return true;
}

AppDelegate类主要就是这个函数有作用,它的主要作用就是创建一个导演,然后通过导演设置了整个场景的一些属性,是否显示调试信息,帧率,启动场景等等。

然后,就是HelloWorld.h

class HelloWorld : public cocos2d::CCLayer
{
public:   
    virtual bool init();  
    
    static cocos2d::CCScene* scene();    
    
    void menuCloseCallback(CCObject* pSender);    
   
    CREATE_FUNC(HelloWorld);
};

就是创建了继承自CCLayer的类,该类有一个初始化函数,一个场景函数,一个回调函数,一个CREATE的宏函数(这个就不拓展了)。

最后,就是看HelloWorld.cpp。

CCScene* HelloWorld::scene()函数主要就是创建一个场景,将一个层加入到场景中。

bool HelloWorld::init()函数创建了一个关闭按钮,创建一个标签,以及创建一个背景图片。

这就是整个工程的文件。它的链接顺序就是,main函数运行,创建一个窗口;进入AppDelegate.cpp中,一些窗口的信息,以及启动HelloWorld场景;进入HelloWorld.cpp,场景创建一个层,包括层上显示的内容等。

这里还介绍一点cocos2d-x的一些结构原理:@@@@@@@@@@

3)现在完成一个非常小的程序,一次来解释cocos2d的基本原理的。

就是在新建的工程上,进行一点修改,即将新工程上创建一个Begin按钮,用于跳转到下一个场景中,下一个场景中有一个精灵可以动,背景也是可移动的,然后增加一个返回按钮,可以回到初始界面,效果图如下:

这里我将源代码贴出来,我也将所有资源打包上传到我的CSDN中:

整个main.h、main.cpp、AppDelegate.h、AppDelegate.cpp基本不变。

HelloWorld.h源代码为:

……

//@@这个头文件是在应用9妹的时候加的
#include "cocos-ext.h"
using namespace cocos2d::extension;

/*
@这里HelloWorld场景,主要是以原示例代码为主,只不过加了一个标题,然后一个begin按钮,
@点击按钮后可以切换到第二个场景中
*/

class HelloWorld : public cocos2d::CCLayer
{
public:
   
    virtual bool init();      
    static cocos2d::CCScene* scene();    
    void menuCloseCallback(CCObject* pSender);    
    // implement the "static node()" method manually @函数的create函数,宏函数
    CREATE_FUNC(HelloWorld);
	
private:
	//@@下面是对于手指按下的一个响应操作
	void touchDown(CCObject* pSender,CCControlEvent event);
};

……

HelloWorld.cpp的源代码为:

#include "SecondScene.h"

……
bool HelloWorld::init()
{
    if ( !CCLayer::init() )
    {
        return false;
    }
    
    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();//@获取屏幕大小
    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();//@获取原坐标(可以测试下原点在哪里,以及XY轴的方向)


        / **************************************************************************************** 
	// @@@@设置一个按钮,字符为begin,具体参数如下(9妹是一种可拉伸的图片,下面的图片是按照字符大小进行拉伸的)	
	CCScale9Sprite* btnNormal = CCScale9Sprite::create("animationbuttonnormal.png");//@正常显示的图片
	CCScale9Sprite* btnDown = CCScale9Sprite::create("animationbuttonpressed.png");//@按下时显示的图片
	CCLabelTTF* title = CCLabelTTF::create("Begin","Arial",30);//@创建一个标签

	CCControlButton* controlBtn = CCControlButton::create(title,btnNormal);//@@@创建按钮
	controlBtn->setBackgroundSpriteForState(btnDown,CCControlStateSelected);//设置按下状态下的图片
	controlBtn->setPosition(ccp(visibleSize.width-60,40));

	controlBtn->addTargetWithActionForControlEvents(this,
		cccontrol_selector(HelloWorld::touchDown),
		CCControlEventTouchDown);//@@@@@设置按钮响应事件
	this->addChild(controlBtn,1);//将按钮加入到场景中,位于第1层


	 / **************************************************************************************** 
	// @@@@设置一个标签,用于显示主题
    CCLabelTTF* pLabel = CCLabelTTF::create("My Game", "Arial", 30);    
    pLabel->setPosition(ccp(origin.x + visibleSize.width/2,origin.y + visibleSize.height - pLabel->getContentSize().height+12));
	this->addChild(pLabel, 1);


	 / **************************************************************************************** 
    // add "HelloWorld" splash screen"  @设置背景图片 (设置成精灵类型,方便可以对其添加动作)
    CCSprite* pSprite = CCSprite::create("HelloWorld.png");    
    pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));   
    this->addChild(pSprite, 0); //@0表示第0层,即最底层

    
    return true;
}

void HelloWorld::menuCloseCallback(CCObject* pSender)
{
   ……
}

void HelloWorld::touchDown(CCObject* pSender,CCControlEvent event)
{
	//@响应手指按下的操作,即切换到SecondScene场景,用时3秒
	CCDirector::sharedDirector()->replaceScene(CCTransitionJumpZoom::create(3.0f,SecondScene::scene()));
}


我特意对以上很多代码进行了详细的注释,就不再啰嗦了。

然后就是新建两个文件,Second.h和Second.cpp。就第二个场景。

Second.h的源代码为:

#ifndef _SECONDSCENE_H_
#define  _SECONDSCENE_H_

#include "cocos2d.h"
#include "cocos-ext.h"
using namespace cocos2d::extension;

/*
@这里SecondScene场景,主要是以HelloWorld代码为蓝本。加入背景图片,并且缓慢上移;
@一个女人精灵以三种动作运动(贝塞尔曲线运动,弹跳,旋转),最后停下;
@停下之后,弹出一个标签The end。然后设置一个按钮用于跳转到下一个场景,这里选为Helloworld;
@全程播放背景音乐,跳转的时候暂停
*/
class SecondScene:public cocos2d::CCLayer
{
public:
	virtual bool init();

	static cocos2d::CCScene* scene();

	CREATE_FUNC(SecondScene);

	void endCall(void);//女人精灵运动停止时的回调函数

private:
	void touchDown(CCObject* pSender,CCControlEvent event);//按钮按下时的响应事件
};

#endif


Second.cpp的源代码为:

#include "SecondScene.h"
#include "SimpleAudioEngine.h"
#include "HelloWorldScene.h"

USING_NS_CC;

CCScene* SecondScene::scene()
{
	CCScene* scene = CCScene::create();
	SecondScene* layer = SecondScene::create();
	scene->addChild(layer);

	return scene;
}

bool SecondScene::init()
{
	CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
	CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();

	 / **************************************************************************************** 
	//@播放背景音乐
	CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("10.mp3",true);
	

	 / **************************************************************************************** 
	//@创建背景图片
	CCSprite* pSprite = CCSprite::create("b11.png");    
	pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));   
	this->addChild(pSprite, 0);

	//@对背景图片添加移动的动作。MoveTo函数的参数就是在18s内,运动到指定位置
	CCMoveTo* moveTo = CCMoveTo::create(18.0f,ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y+50));
	pSprite->runAction(moveTo);


	 / **************************************************************************************** 
	//@创建女人精灵
	CCSprite* womanSprite = CCSprite::create("grossinis_sister1.png");
	womanSprite->setScale(0.7f);//缩放
	womanSprite->setPosition(ccp(origin.x+50,origin.y+70));
	this->addChild(womanSprite,1);

	//@设置贝塞尔曲线。设置贝塞尔曲线参数为,初始位置、第一,二固定点、终点四个点确定一条曲线
	ccBezierConfig bezier;
	bezier.controlPoint_1 = ccp(100,visibleSize.height);
	bezier.controlPoint_2 = ccp(200,250);
	bezier.endPosition = ccp(300,50);

	CCBezierTo* bezierTo = CCBezierTo::create(8.0f,bezier);//@贝塞尔动作,8s内沿着bezier生成曲线运动
	//womanSprite->runAction(bezierTo);//@测试bezierTo运动
	CCJumpBy* jumpBy = CCJumpBy::create(5.0f,ccp(-100,150),100,5);//@整个弹跳耗时5.0s,XY增加(400,150),每次跳跃高度100,总共弹5次
	//womanSprite->runAction(jumpBy);//@测试jumpBy运动
	CCRotateBy* rotateBy = CCRotateBy::create(5.0f,220,10);//@整个旋转耗时5.0s,X轴旋转220du,Y轴旋转10度。注意图像的处理
	//womanSprite->runAction(rotateBy);//@测试rotateBy运动
	//CCAction* actions = CCSequence::create(bezierTo,jumpBy,rotateBy,NULL);
	//womanSprite->runAction(actions);//@测试三种运动顺序连接在一起的运动。考虑CCSequence::create和CCpawn::create的区别
	                                                        //以及actions的类型用CCAction和CCSequence的区别

    //@定义运动完成后的响应函数
	CCCallFunc* callFunc = CCCallFunc::create(this,callfunc_selector(SecondScene::endCall));

	CCSequence* actions = CCSequence::create(bezierTo,jumpBy,rotateBy,callFunc,NULL);//@将上述所有运动连接起来播放

	womanSprite->runAction(actions);//@运行


	/ **************************************************************************************** 
	//@设置一个返回按钮,应用9妹图片
	CCScale9Sprite* btn = CCScale9Sprite::create("f1.png");
	CCLabelTTF* title = CCLabelTTF::create("       ","Arial",30);//注意这里空格是为了占位,即为了使9妹扩大到指定大小
	CCControlButton* controlBtn = CCControlButton::create(title,btn);//@@@创建按钮	
	controlBtn->setPosition(ccp(visibleSize.width-60,40));

	controlBtn->addTargetWithActionForControlEvents(this,cccontrol_selector(SecondScene::touchDown),
		CCControlEventTouchDown);//@@@@@设置按钮相应函数	
	this->addChild(controlBtn,1);

	return true;
}


void SecondScene::endCall()
{
	//@这里回调函数就是创建一个标签
	CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
	CCLabelTTF* pLabel = CCLabelTTF::create("The end!", "Arial", 50);    
	pLabel->setPosition(ccp( visibleSize.width/2, visibleSize.height - pLabel->getContentSize().height+12));
	this->addChild(pLabel);
}


void SecondScene::touchDown(CCObject* pSender,CCControlEvent event)
{
	CocosDenshion::SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();//关掉背景音乐
	CCDirector::sharedDirector()->pushScene(CCTransitionFadeUp::create(3.0f,HelloWorld::scene()));	//切换场景
	//@@这里可以区分一下:ReplaceScene、pushScene和popScene是怎样对新旧场景进行处理的
}


以上就是整个我学习cocos2d-x的第一个小示例http://download.csdn.net/detail/xunixianshi123/7707931,不难,很多都是新建时自动生成的,就这么简单。希望我的这个示例可以带领大家进入手机游戏编程的世界里。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值