zoukankan      html  css  js  c++  java
  • cocos2dx3.0戳青蛙游戏(打地鼠)

    1戳青蛙项目描写叙述

    1.1功能描写叙述

    实现类似打地鼠游戏。青蛙随机出如今屏幕左边5*3的格子中,并会向屏幕右边移动,在青蛙逃离之前,手指点击实现戳灭青蛙的效果。随着分数添加,青蛙越来越多,当青蛙逃离5个后,游戏结束。

    青蛙分为大青蛙和小青蛙,大青蛙走的忙。要点击3下,小青蛙走的快,仅仅需点击两下。

    1.2所需技术

    Cocos2D-x精灵类,动作类,多点触摸。CocoStudioUI编辑器。Vector

    2戳青蛙执行流程


    3戳青蛙具体设计

    3.1实体基类

    class CEntity : public Node {
    public:
    	CEntity();
    	~CEntity();
    	void spriteBind(Sprite* sprite);
    	Sprite* spriteGet();
    private:
    	Sprite* m_SprBind;
    	CC_SYNTHESIZE(int, m_iHP, iHP);		//血量
    	CC_SYNTHESIZE(int, m_iSpeed, iSpeed);		//速度
    	CC_SYNTHESIZE(std::string, m_sName, sName);	//名字
    	CC_SYNTHESIZE(Point, m_pos, pos);	//坐标
    };

    3.2青蛙类

    青蛙类是基于实体类的。


    class CFrog :public CEntity
    {
    public:
    	CFrog();
    	~CFrog();
    	virtual bool init();
    	CREATE_FUNC(CFrog);
    	void createSpriteByName();
    	bool clickJudge(Point pos);//推断该青蛙有没被点击
    	void getHurt();//获得伤害
    	bool dieJudge();//是否死亡推断
    };

    createSpriteByName是依据m_sName,从精灵缓冲池中选取不同的图片(大小青蛙),而且绑定动画。青蛙具有血量,名字(类型)。速度的属性。

    3.3青蛙管理器类

    class CFrogMgr :public Node
    {
    public:
    	CFrogMgr();
    	~CFrogMgr();
    	bool initBy();
    	CREATE_FUNC(CFrogMgr);
    	static CFrogMgr* createBy();
    	void createFrog(float dt);
    	void frogsMove(float dt);
    	void touchDeal();
    	CFrog* findClickFrog(Point pos);
    	void frogPake(Point pos);
    	void notityForPause(Ref* pData);
    private:
    	Vector<CFrog*> m_listFrog;
    };

    青蛙管理器类createFrog方法每2秒产生青蛙,产生数量是跟分数成正比的。

    随机青蛙的产生的位置。青蛙的类型。每产生一个青蛙增加到青蛙的容器中。

    3.3.1青蛙移动

    frogsMove方法每0.1秒运行一遍,详细内容是遍历青蛙容器,把每一个青蛙个体的横坐标依据速度添加,当青蛙逃离了屏幕的最右端的时候,要把它从容器中删除。特别说明:容器的erase删除一个后会返回迭代器指向的下一个位置。

    void CFrogMgr::frogsMove(float dt)
    {
    	//使用迭代器进行删除处理
    	for (Vector<CFrog*>::iterator it = m_listFrog.begin(); it != m_listFrog.end();)
    	{
    		(*it)->setPositionX((*it)->getPositionX() + (*it)->getiSpeed());//横坐标添加
    		if ((*it)->getPositionX() > MAP_WIDE)//当逃离时
    		{
    			(*it)->removeFromParentAndCleanup(true);
    			it = m_listFrog.erase(it);
    			NOTIFY->postNotification(NOTIFY_HP, (Ref*)1);
    		}
    		else
    			++it;
    	}
    }

    3.3.2多点触摸响应

    遍历青蛙容器。通过找哪个青蛙被点击。

    使该青蛙掉血,直到其阵亡。

    void CFrogMgr::frogPake(Point pos)
    {
    	CFrog* frogDiv = findClickFrog(pos);
    	if (frogDiv == NULL)
    		return;
    	else
    	{
    		frogDiv->getHurt();
    		if (frogDiv->dieJudge() == true)
    		{
    			CocosDenshion::SimpleAudioEngine::getInstance()->playEffect(PATH_DIE_MUSIC);//循环播放背景音乐
    			frogDiv->removeFromParentAndCleanup(true);
    			m_listFrog.eraseObject(frogDiv);
    			NOTIFY->postNotification(NOTIFY_SCORE, (Ref*)1);
    		}
    	}
    }

    4戳青蛙执行结果



    源代码下载

  • 相关阅读:
    75. Sort Colors
    101. Symmetric Tree
    121. Best Time to Buy and Sell Stock
    136. Single Number
    104. Maximum Depth of Binary Tree
    70. Climbing Stairs
    64. Minimum Path Sum
    62. Unique Paths
    css知识点3
    css知识点2
  • 原文地址:https://www.cnblogs.com/jhcelue/p/7258194.html
Copyright © 2011-2022 走看看