cocos2d-x学习记录1——图片显示

这篇算是cocos2d-x入门篇,显示一张图片即可。

观察工程中HelloWorld的结构,包含AppDelegate和HelloWorldScene两个类文件,AppDelegate中包含基本的处理,并且创建最初要显示的Scene并运行之。

而HelloWorldScene中则做了相关的初始化工作,在这里,自己仿照HelloWorldScene写个更简单的Scene用于显示。

AppDelegate.cpp内容

复制代码
 1 #include "AppDelegate.h"
 2 //#include "HelloWorldScene.h"
 3 #include "MyScene.h"
 4 
 5 USING_NS_CC;
 6 
 7 AppDelegate::AppDelegate() {
 8 
 9 }
10 
11 AppDelegate::~AppDelegate() 
12 {
13 }
14 
15 bool AppDelegate::applicationDidFinishLaunching() {
16     // initialize director
17     CCDirector* pDirector = CCDirector::sharedDirector();
18     CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();
19 
20     pDirector->setOpenGLView(pEGLView);
21     
22     // turn on display FPS
23     pDirector->setDisplayStats(true);
24 
25     // set FPS. the default value is 1.0/60 if you don't call this
26     pDirector->setAnimationInterval(1.0 / 60);
27 
28     // create a scene. it's an autorelease object
29     //CCScene *pScene = HelloWorld::scene();
30     CCScene *pScene = MyScene::createScene();
31 
32     // run
33     pDirector->runWithScene(pScene);
34 
35     return true;
36 }
37 
38 // This function will be called when the app is inactive. When comes a phone call,it's be invoked too
39 void AppDelegate::applicationDidEnterBackground() {
40     CCDirector::sharedDirector()->stopAnimation();
41 
42     // if you use SimpleAudioEngine, it must be pause
43     // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
44 }
45 
46 // this function will be called when the app is active again
47 void AppDelegate::applicationWillEnterForeground() {
48     CCDirector::sharedDirector()->startAnimation();
49 
50     // if you use SimpleAudioEngine, it must resume here
51     // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
52 }
复制代码

 

MyScene.h内容

 

复制代码
 1 #ifndef MyScene_H_H
 2 #define MyScene_H_H
 3 
 4 #include "cocos2d.h"
 5 using namespace cocos2d;
 6 
 7 class MyScene : public CCLayer
 8 {
 9 public:
10     static CCScene* createScene();
11     virtual bool init();
12     CREATE_FUNC( MyScene );
13 
14 
15 private:
16 };
17 
18 #endif
复制代码

MyScene.cpp内容

复制代码
 1 #include "MyScene.h"
 2 
 3 CCScene* MyScene::createScene()
 4 {
 5     CCScene *scene = CCScene::create();
 6     MyScene *layer = MyScene::create();
 7     scene->addChild(layer);
 8     return scene;
 9 };
10 
11 
12 bool MyScene::init()
13 {
14     if( !CCLayer::init() ){
15         return false;
16     }
17     CCSize size = CCDirector::sharedDirector()->getWinSize();
18     CCSprite *sprite = CCSprite::create("pal4.png");
19     sprite->setAnchorPoint( ccp(0.5f, 0.5f) );
20     sprite->setPosition( ccp(size.width/2, size.height/2) );
21     sprite->setScale(0.5f);
22     addChild(sprite);
23 
24     return true;
25 }
复制代码

 

运行结果:

 

posted @   丛林小阁楼  阅读(301)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示