cocos2d-x学习之飞镖打妖怪

cocos2d-x学习之飞镖打妖怪,第1张

概述cocos2d-x引擎版本是2.2.0 这篇博客其实不是给给人看的,只是给我自己看的,以便自己熟悉里面的代码 总共四个文件: #ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__#include "cocos2d.h"class HelloWorld : public cocos2d::CCLayerColor{pu


cocos2d-x引擎版本是2.2.0

这篇博客其实不是给给人看的,只是给我自己看的,以便自己熟悉里面的代码

总共四个文件:

#ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__#include "cocos2d.h"class HelloWorld : public cocos2d::cclayercolor{public:	~ HelloWorld();    // Here's a difference. Method 'init' in cocos2d-x returns bool,instead of returning 'ID' in cocos2d-iphone    virtual bool init();      // there's no 'ID' in cpp,so we recommend returning the class instance pointer    static cocos2d::CCScene* scene();	//小孩对象	cocos2d::CCSprite *player;	//怪物对象	cocos2d::CCSprite *target;	//怪物集合	cocos2d::CCArray *_targets;	//飞镖集合	cocos2d::CCArray *_proJs;	//击中次数	int _Shottimes;    // a selector callback    voID menuCloseCallback(CCObject* pSender);	//按钮的响应	voID responseFunc(CCObject* obj);        // implement the "static node()" method manually    CREATE_FUNC(HelloWorld);	voID mydefine(CCNode *who);	voID CreateTarget();	voID gameLogic(float dt);	voID cctouchesEnded(cocos2d::CCSet *ptouches,cocos2d::CCEvent *pEvent);	voID update(float delta);};#endif // __HELLOWORLD_SCENE_H__
#include "HelloWorldScene.h"#include "SimpleAudioEngine.h"#include "GameOverLayer.h"USING_NS_CC;CCScene* HelloWorld::scene(){    // 'scene' is an autorelease object    CCScene *scene = CCScene::create();        // 'layer' is an autorelease object    HelloWorld *layer = HelloWorld::create();    // add layer as a child to scene    scene->addChild(layer);    // return the scene    return scene;}voID HelloWorld::menuCloseCallback(CCObject* pSender){#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)	CcmessageBox("You pressed the close button. windows Store Apps do not implement a close button.","Alert");#else	CCDirector::sharedDirector()->end();#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)	exit(0);#endif#endif}// on "init" you need to initialize your instancebool HelloWorld::init(){    //////////////////////////////    // 1. super init first    if ( !cclayercolor::initWithcolor(ccc4(0,255,255)) )    {        return false;    }    //     CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();//     CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();// //     ///////////////////////////////     // 2. add a menu item with "X" image,which is clicked to quit the program//     //    you may modify it.// //     // add a "close" icon to exit the progress. it's an autorelease object//     CcmenuItemImage *pCloseItem = CcmenuItemImage::create(//                                         "Closenormal.png",//                                         "CloseSelected.png",//                                         this,//                                         menu_selector(HelloWorld::menuCloseCallback));//     // 	pCloseItem->setposition(ccp(origin.x + visibleSize.wIDth - pCloseItem->getContentSize().wIDth/2,//                                 origin.y + pCloseItem->getContentSize().height/2));// //     // create menu,it's an autorelease object//     Ccmenu* pMenu = Ccmenu::create(pCloseItem,NulL);//     pMenu->setposition(CCPointZero);//     this->addChild(pMenu,1);// //     ///////////////////////////////     // 3. add your codes below...// //     // add a label shows "Hello World"//     // create and initialize a label//     //     cclabelTTF* pLabel = cclabelTTF::create("Hello World","Arial",24);//     //     // position the label on the center of the screen//     pLabel->setposition(ccp(origin.x + visibleSize.wIDth/2,//                             origin.y + visibleSize.height - pLabel->getContentSize().height));// //     // add the label as a child to this layer//     this->addChild(pLabel,1);// //     // add "HelloWorld" splash screen"//     CCSprite* pSprite = CCSprite::create("HelloWorld.png");// //     // position the sprite on the center of the screen//     pSprite->setposition(ccp(visibleSize.wIDth/2 + origin.x,visibleSize.height/2 + origin.y));// //     // add the sprite as a child to this layer//     this->addChild(pSprite,0);	//屏幕大小	CCSize screenSize = CCDirector::sharedDirector()->getVisibleSize();	//添加小孩	player = CCSprite::create("Player.png");	player->setposition(ccp(0+20,screenSize.height/2));	this->addChild(player);// 	//菜单的使用// 	CcmenuItemImage* item = CcmenuItemImage::create("button2.png",// 		"button2.png",// 		this,// 		menu_selector(HelloWorld::responseFunc));// 	item->setposition(ccp(30,30));// 	Ccmenu *menu = Ccmenu::create(item,NulL);// 	this->addChild(menu);// // 	target = CCSprite::create("Target.png");// 	target->setposition(ccp(screenSize.wIDth -2,40));// 	this->addChild(target);	//定时生成怪物	this->schedule(schedule_selector(HelloWorld::gameLogic),1);	//检测碰撞	this->schedule(schedule_selector(HelloWorld::update));	//允许触屏	this->settouchEnabled(true);	_targets = new cocos2d::CCArray;	_proJs = new cocos2d::CCArray;	//播放背景音乐	CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("background-music-aac.wav",true);	//成员初始化	_Shottimes = 0;    return true;}//屏幕触摸---发射飞镖voID HelloWorld::cctouchesEnded(CCSet *ptouches,CCEvent *pEvent){	//获得屏幕大小	CCSize screenSize = CCDirector::sharedDirector()->getVisibleSize();	//获得触摸点	CCtouch * touch = (CCtouch *)ptouches->anyObject();	CCPoint LocinVIEw = touch->getLocationInVIEw();	CCPoint loc = CCDirector::sharedDirector()->convertToGL(LocinVIEw);	if (loc.x <= 20)	{		return;	}	//加载飞镖	CCSprite *proj = CCSprite::create("Projectile.png");	proj->setposition(ccp(20,screenSize.height/2.0));	this->addChild(proj);	//播放飞镖音乐	CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("pew-pew-lei.wav");	//加入到飞镖数组	_proJs->addobject(proj);	proj->setTag(2);	double dx = loc.x -20;	double dy = loc.y -screenSize.height / 2.0;	double d = sqrt(dx * dx + dy * dy);	//发射长度	double D = sqrt(screenSize.wIDth * screenSize.wIDth + screenSize.height * screenSize.height);	double ratio = D / d;	double endx = ratio * dx +20;	double endy = ratio * dy + screenSize.height / 2.0;	CCMoveto *move = CCMoveto::create(D / 320,ccp(endx,endy));	CCCallFuncN * moveFinish = CCCallFuncN::create(this,callfuncN_selector(HelloWorld::mydefine));	CCSequence *actions = CCSequence::create(move,moveFinish,NulL);	proj->runAction(actions);}//定时器回调函数voID HelloWorld::gameLogic(float dt){	CreateTarget();}//创建会运行的怪物voID HelloWorld::CreateTarget(){	//获取屏幕宽度	CCSize screenSize = CCDirector::sharedDirector()->getVisibleSize();	int y = rand() % (int)(screenSize.height);	//怪物精灵	CCSprite *target = CCSprite::create("Target.png");	target->setposition(ccp(screenSize.wIDth -2,y));	this->addChild(target);	//加入到怪物数组	_targets->addobject(target);	target->setTag(1);	CCMoveto *move = CCMoveto::create(3,ccp(0,y));	CCCallFuncN *disapear = CCCallFuncN::create(this,disapear,NulL);	target->runAction(actions);}//按钮响应voID HelloWorld::responseFunc(CCObject* obj){	//cclOG("button is pressed");	CCMoveto *move = CCMoveto::create(3,40));	CCCallFuncN *disapear = CCCallFuncN::create(this,NulL);	target->runAction(actions);}//让怪物和飞镖消失voID HelloWorld::mydefine(CCNode *who){	who->removeFromParentAndCleanup(true);	//销毁精灵	int tag = who->getTag();	if (tag == 1)//怪物消失	{		_targets->removeObject(who);		//切换到游戏结束场景		CCScene *gameOverScene = GameOverLayer::scene();		GameOverLayer *layer = (GameOverLayer*)gameOverScene->getChildByTag(100);		layer->_lable->setString("Sorry,You Failed~!");		CCDirector::sharedDirector()->replaceScene(gameOverScene);	}	else if (tag == 2)//飞镖消失	{		_proJs->removeObject(who);	}}//析构函数HelloWorld::~HelloWorld(){	if (_proJs != NulL)	{		_proJs->release();	}	if (_targets != NulL)	{		_targets->release();	}}//不停地检测碰撞事件voID HelloWorld::update(float delta){	//准备删除的临时集合	CCArray *toDelTarget = new CCArray;	CCArray *toDelProj = new CCArray;	CCObject *itarget,*iproj;	//遍历所有怪物	CCARRAY_FOREACH(_targets,itarget)	{		CCSprite * target = (CCSprite*)itarget;		CCRect targetZone = CCRectMake(target->getpositionX(),target->getpositionY(),target->getContentSize().wIDth,target->getContentSize().height);		//遍历所有飞镖		CCARRAY_FOREACH(_proJs,iproj)		{			CCSprite * proj = (CCSprite*)iproj;			CCRect projZone = CCRectMake(proj->getpositionX(),proj->getpositionY(),proj->getContentSize().wIDth,proj->getContentSize().height);			//检查重合			if (projZone.intersectsRect(targetZone))			{				toDelTarget->addobject(target);				toDelProj->addobject(proj);				//检测游戏结束				if (++_Shottimes == 5)				{					//切换到游戏结束场景					CCScene *gameOverScene = GameOverLayer::scene();					GameOverLayer *layer = (GameOverLayer*)gameOverScene->getChildByTag(100);					layer->_lable->setString("Congratulations,You Succeed~!");					CCDirector::sharedDirector()->replaceScene(gameOverScene);				}			}		}	}	//删除碰撞精灵	CCARRAY_FOREACH(toDelProj,iproj)	{		_proJs->removeObject(iproj);		CCSprite * proj = (CCSprite*)iproj;		proj->removeFromParentAndCleanup(true);	}	CCARRAY_FOREACH(toDelTarget,itarget)	{		_targets->removeObject(itarget);		CCSprite * target = (CCSprite*)itarget;		target->removeFromParentAndCleanup(true);	}}

#pragma once#include "cocos2d.h"class GameOverLayer : public cocos2d::cclayercolor{public:	GameOverLayer(voID);	~GameOverLayer(voID);public:	bool init();	CREATE_FUNC(GameOverLayer);	static cocos2d::CCScene* scene();	voID ReturnToGame(cocos2d::CCNode *node);public:	cocos2d::cclabelTTF * _lable;};

#include "GameOverLayer.h"#include "HelloWorldScene.h"USING_NS_CC;GameOverLayer::GameOverLayer(voID){}GameOverLayer::~GameOverLayer(voID){	if (_lable)	{		_lable->release();	}}bool GameOverLayer::init(){	if (cclayercolor::initWithcolor(ccc4(0,255)))	{		_lable = cclabelTTF::create("word","Artial",40);		CCSize size = CCDirector::sharedDirector()->getVisibleSize();		_lable->setposition(ccp(size.wIDth/2,size.height/2));		this->addChild(_lable);		_lable->retain();		//再次切换到游戏		CCDelayTime *delay = CCDelayTime::create(5);		CCCallFuncN * returnToGame = CCCallFuncN::create(this,callfuncN_selector(GameOverLayer::ReturnToGame));		this->runAction(CCSequence::create(delay,returnToGame,NulL));		return true;	}	return false;}//切换回游戏voID GameOverLayer::ReturnToGame(CCNode *node){	CCDirector::sharedDirector()->replaceScene(HelloWorld::scene());}//创建场景cocos2d::CCScene* GameOverLayer::scene(){	CCScene * scene = CCScene::create();	GameOverLayer *layer = GameOverLayer::create();	layer->setTag(100);	scene->addChild(layer);	return scene;}
总结

以上是内存溢出为你收集整理的cocos2d-x学习飞镖妖怪全部内容,希望文章能够帮你解决cocos2d-x学习之飞镖打妖怪所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1066100.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存