cocos2d-x之新手学习留痕(一)——helloWorld+button+animation

没学过c++,因cocos2d-x能做游戏,所以,开始了悲剧的开始。。。。。。

新建一个cocos2d-x的工程,找到source包中的HelloWorldScene.cpp,

在bool HelloWorld::init()这个方法中,删除系统自动生成的代码,自己写了如下的代码

 1 bool HelloWorld::init()
 2 {
 3     bool bRet = false;
 4     do 
 5     {
 6         CC_BREAK_IF(! CCLayer::init());
 7         
 8         //加入文字
 9         CCLabelTTF* pLabel = CCLabelTTF::create("hello cjj","Arial",24);
10             //检查指针
11         CC_BREAK_IF(! pLabel);
12         CCSize size = CCDirector::sharedDirector()->getWinSize();
13         //设置位置
14         pLabel->setPosition(ccp(size.width/3,size.height/2));
15         this->addChild(pLabel,1);
16 
17         //加入退出按钮
18         CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
19                                                             "CloseNormal.png",//按钮正常情况
20                                                             "CloseSelected.png",//按钮按下之后
21                                                             this,menu_selector(HelloWorld::menuCloseCallback)//回调函数,退出程序
22                                                             );
23     
24         CC_BREAK_IF(!pCloseItem);
25         pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width-20,20));
26         //创建menu
27         CCMenu* pMenu = CCMenu::create(pCloseItem,NULL);
28         CC_BREAK_IF(!pMenu);
29         pMenu->setPosition(CCPointZero);
30         this->addChild(pMenu,1);
31 
32          //绘制背景图片
33         CCSprite* pSprite = CCSprite::create("7.jpg");
34         CC_BREAK_IF(! pSprite);
35         pSprite->setPosition(ccp(size.width/2,size.height/2));
36         this->addChild(pSprite,0);
37         
38 
39         //获得帧
40         CCTexture2D* pTexture = CCTextureCache::sharedTextureCache()->addImage("you.png");
41         CCSpriteFrame* pFrame0 = CCSpriteFrame::createWithTexture(pTexture,CCRectMake(0,94*0,81,94));
42         CCSpriteFrame* pFrame1 = CCSpriteFrame::createWithTexture(pTexture,CCRectMake(0,94*1,81,94));
43         CCSpriteFrame* pFrame2 = CCSpriteFrame::createWithTexture(pTexture,CCRectMake(0,94*2,81,94));
44         CCSpriteFrame* pFrame3 = CCSpriteFrame::createWithTexture(pTexture,CCRectMake(0,94*3,81,94));
45         CCSpriteFrame* pFrame4 = CCSpriteFrame::createWithTexture(pTexture,CCRectMake(0,94*4,81,94));
46         CCSpriteFrame* pFrame5 = CCSpriteFrame::createWithTexture(pTexture,CCRectMake(0,94*5,81,94));
47         CCArray  *pArray=CCArray::create();  
48         pArray->addObject(pFrame0);
49         pArray->addObject(pFrame1);
50         pArray->addObject(pFrame2);
51         pArray->addObject(pFrame3);
52         pArray->addObject(pFrame4);
53         pArray->addObject(pFrame5);
54         //创建动画
55         CCAnimation* pAnimation = CCAnimation::createWithSpriteFrames(pArray,0.5);
56         CC_BREAK_IF(! pAnimation);
57 
58         //创建精灵
59         CCSprite* pSprtieYou = CCSprite::createWithSpriteFrame(pFrame0);
60         CC_BREAK_IF(!pSprtieYou); 
61         pSprtieYou->setPosition(ccp(size.width/3,size.height/3));
62         this->addChild(pSprtieYou,2);
63         //播放动画
64         CCAnimate* pAnimate = CCAnimate::create(pAnimation);
65         pSprtieYou->runAction(CCRepeatForever::create(pAnimate));
66 
67         bRet = true;
68     } while (0);
69 
70     return bRet;
71 }

运行下程序,就有这种效果了 ,当然,前提是要有图片资源:

效果:

恩恩,就这样,至少第一篇不像随笔的随笔完成了,再接再悲剧吧。。。。。。

 

 

 

 

 

posted @ 2013-11-05 23:55  梦醒边缘花落  阅读(590)  评论(0编辑  收藏  举报