一个cocos2dx 游戏解读(SceneStart)

初始化代码

SceneStart::SceneStart()
{
	CCScene::init();
	//取得屏幕大小
	CCSize size = CCDirector::sharedDirector()->getWinSize();
	CCSize designSize = CCSizeMake(800, 480);
	float scale = size.height/480;
	//背景图
	CCSprite* background = CCSprite::createWithSpriteFrameName("bg_start.png");
	background->setAnchorPoint(ccp(0, 0));
	this->addChild(background);
	//Logo图
	CCSprite* logo = CCSprite::createWithSpriteFrameName("logo.png");
	//设置到界面中间偏上
	logo->setPosition(ccp(size.width/2, size.height/2+120));
	this->addChild(logo);
    //两个按钮
	CCMenuItemSprite* btn_start = CCMenuItemSprite::create(
		CCSprite::createWithSpriteFrameName("btn_start1.png"), 
		CCSprite::createWithSpriteFrameName("btn_start2.png"), 
		this, menu_selector(SceneStart::click_start));
	CCMenuItemSprite* btn_setting = CCMenuItemSprite::create(
		CCSprite::createWithSpriteFrameName("btn_setting1.png"), 
		CCSprite::createWithSpriteFrameName("btn_setting2.png"), 
		this, menu_selector(SceneStart::click_setting));
	//MenuItem需要通过CCMenu组合
	CCMenu* menu = CCMenu::create(btn_start, btn_setting, NULL);
	//一个垂直间隔排列
	menu->alignItemsVerticallyWithPadding(10);
	//设置到界面中间偏下
	menu->setPosition(ccp(size.width/2, size.height/2-120*scale));
	this->addChild(menu);


//	/**********TEST**********/
	std::string id_buff[] =
		{"B1", "B2", "B3", "B4", "Hero02",
		"A1", "A2", "A3", "A4", "Hero11"};
	float leftmark = scale *64;
//	float rightmargin =  size -
	for (int i = 0; i < 2; i++) {
		for (int j = 0; j < 5; j++) {
			ActorBase* actor = new ActorBase(new ActorData(id_buff[i*5 + j]));
			actor->setActorDir((ActorDir)(i + 1));
			actor->setPosition(ccp(leftmark + i * leftmark, leftmark + j * leftmark));
			if (j % 2 == 1)
				actor->StateToRun();
			else
				actor->StateToAttack();
			this->addChild(actor);

			/****************/
			ActorBase* actor2 = new ActorBase(new ActorData(id_buff[i*5 + j]));
			actor2->setActorDir((ActorDir)(i + 1));
			actor2->setPosition(ccp(size.width-2*leftmark + i * leftmark, leftmark + j * leftmark));
			if (j % 2 == 0)
				actor2->StateToRun();
			else
				actor2->StateToAttack();
			this->addChild(actor2);
			/****************/
		}
	}
	/**********TEST**********/
}

CCSize designSize = CCSizeMake(800, 480); 设置800 *480是因为原始图片大小,我测试用的机器是1280*720,后续的基本我都加有缩放

CCSprite* background = CCSprite::createWithSpriteFrameName("bg_start.png");//这个就是从资源缓存里面读取数据,这个数据在AppDelegate调用GameRoot::InitializeResource();初始化

CCSprite *target = CCSprite::create("Target.png", CCRectMake(0,0,27,40) );//这句是直接读取资源图片,注意和例子中的对比


/**********TEST**********/
	std::string id_buff[] =
		{"B1", "B2", "B3", "B4", "Hero02",
		"A1", "A2", "A3", "A4", "Hero11"};

这个是图片的名字,在原帖里面写的很清楚

http://www.cnblogs.com/nowpaper/archive/2012/10/22/2733389.html

actor2是我自己加的 让右边也出现一些精灵,if(j%2==1) 这句判断是让精灵隔行显示动画不一样

void SceneStart::click_setting(CCObject* pSender)
{
CCDirector::sharedDirector()->end();
}

这个方法我让程序直接结束,也可以换成场景或者控制层,这样就和现在好多游戏的处理一样了

void SceneStart::click_start(CCObject* pSender)
{
CCScene* tmpScene = CCTransitionFade::create(0.5f, GameRoot::getSceneSelect());
CCDirector::sharedDirector()->pushScene(tmpScene);
}

注意这句开始的场景切换,他是把这个对象放在GameRoot里面,如果内存已经存在就直接显示。

这种设计和c/c++内存分配有关,java是动态分配内存和c/c++不一样

你可能感兴趣的:(cocos2dx,eclipse,cocos2dx,游戏,c++)