zoukankan      html  css  js  c++  java
  • [置顶] 【cocos2d-x入门实战】微信飞机大战之四:飞机登场咯

    转载请表明地址:http://blog.csdn.net/jackystudio/article/details/11757175

    昨天收到了电子工业出版社寄过来的《cocos2d-x游戏开发之旅》这本书了,书还是不错的,那天rp爆发在微博上抽到的奖品。

    感觉自己这个系列写的好像有点慢,但是想说尽可能把每一个点介绍到,所以,嫌啰嗦的请见谅咯。。。

    我在这个游戏中对精灵和层的处理方式是一个层中只放一种精灵,把精灵的接口提供出去,然后通过层的叠加来实现整个游戏。


    1.飞机登场了

    飞机是Hero,所以它应该是一个单例类,cocos2d-x中有很多单例类,成员函数中有sharedxxxx的基本上就是。比如导演类,几个cache类等待。但是我这里偷个懒,并没有把它处理成单例类,因为整个游戏结构比较简单,但是在大的项目中就不能这么处理了。

    和导演类的shareDirector一样,我们在PlaneLayer中也添加PlaneLayer的一个静态指针sharedPlane。但是和导演类不一样的是,我们需要在创建PlaneLayer后才能使用这个sharedPlane,而且在PlaneLayer销毁后就不能使用它,这里需要我们自己控制好整个流程。

    //PlaneLayer.h
    class PlaneLayer :
    	public CCLayer
    {
    public:
    
    	PlaneLayer(void);
    	
    	~PlaneLayer(void);
    
    	static PlaneLayer* create();//实现create函数
    
    	virtual bool init();
    
    public:
    
    	static PlaneLayer* sharedPlane;//提供sharedPlane全局指针
    };
    
    //PlaneLayer.cpp
    PlaneLayer* PlaneLayer::sharedPlane=NULL;//静态变量要在cpp外初始化
    PlaneLayer* PlaneLayer::create() 
    { 
    	PlaneLayer *pRet = new PlaneLayer();
    	if (pRet && pRet->init())
    	{
    		pRet->autorelease();
    		sharedPlane=pRet;//获得静态指针sharedPlane的值
    		return pRet;
    	}
    	else
    	{
    		CC_SAFE_DELETE(pRet);
    		return NULL;
    	}
    }

    和示例不一样,这里的create我们要自己实现,而不是简单的使用LAYER_CREATE_FUNC宏了。


    而这个层的使用,只需要在上一篇中的GameLayer.cpp的init函数里调用就可以了。

    //加入planeLayer
    this->planeLayer=PlaneLayer::create();
    this->addChild(planeLayer);

    2.添加动画效果

    细心的玩家会发现,飞机在飞行过程中机尾是在喷火的,其实这就是帧动画。

    这里篇幅有限,就简单介绍下帧动画和这里用到的其他动画效果。

    帧动画的使用步骤:

    (1)create,创建CCAnimation类实例。

    (2)setDelayPerUnit,设置帧间间隔时间。

    (3)addSpriteFrame,添加帧图片。

    (4)create,创建CCAnimate类实例,传入(1)的CCAnimation实例,注意CCAnimation是动画过程是名词,CCAnimate才是动画动作。

    (5)精灵调用runAction即可使用。

    (6)注意CCAnimationCache这也是一个动画类全局缓冲池。如果大量重复动画可以放里面使用,加快游戏速度。

    bool PlaneLayer::init()
    {
    	bool bRet=false;
    	do 
    	{
    		CC_BREAK_IF(!CCLayer::init());
    		
    		CCSize winSize=CCDirector::sharedDirector()->getWinSize();
    
    		CCSprite* plane=CCSprite::create(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("hero1.png"));
    		plane->setPosition(ccp(winSize.width/2,plane->getContentSize().height/2));
    		this->addChild(plane,0,AIRPLANE);//添加精灵,AIRPLANE是tag
    
    		CCBlink *blink=CCBlink::create(1,3);//闪烁动画
    
    		CCAnimation* animation=CCAnimation::create();
    		animation->setDelayPerUnit(0.1f);
    		animation->addSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("hero1.png"));
    		animation->addSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("hero2.png"));
    		CCAnimate* animate=CCAnimate::create(animation);//帧动画
    		
    		CCSequence* sequence=CCSequence::create(blink,CCRepeatForever::create(animate));//重复帧动画和连续动画
    		plane->runAction(sequence);
    
    		bRet=true;
    	} while (0);
    	
    	return bRet;
    }

    那这些CCBlink,CCRepeaterForever和CCSequence又是什么?

    这就是cocos2d-x提供的一些动画效果,其实用起来都很简单,无非是设置duration持续时长,repeat重复次数,和一些位移的坐标点,角度等等,就可以直接运行。可以在cocos2d-x的test示例中找到。


    现在飞机出现了,在一开始闪烁3次以后,结合上篇的背景滚动,飞机就会喷气往前飞了。。。吼吼。。。


  • 相关阅读:
    [Algorithms] Insertion sort algorithm using TypeScript
    [Algorithms] Determine if a string is a palindrome
    [Algorithm] Determine if two strings are an anagram
    [Vue + TS] Watch for Changes in Vue Using the @Watch Decorator with TypeScript
    [Vue +TS] Use Two-Way Binding in Vue Using @Model Decorator with TypeScript
    uvc摄像头代码解析7
    并查集
    流程节点多场景多表单
    【图像识别】 图像处理和图像分析(leptonica)leptonica-1.68安装配置 (vs2008)
    Eclipse完美汉化教程
  • 原文地址:https://www.cnblogs.com/suncoolcat/p/3327716.html
Copyright © 2011-2022 走看看