使用cocos2d-x 写的蜘蛛闪避小游戏------从cocos2d移植过来的~有哪里缺陷的可以提噢。亲。

以下是附上的代码。 主要是一个类GameScene类。
#pragma once

#include "cocos2d.h"
USING_NS_CC;

class GameScene :public cocos2d::CCLayer
{
public:
	GameScene(void);
	~GameScene(void);

	//游戏开始初始化
	void initGame();

	//player 的重力感应更新位置
	virtual void update(float dt);
	void didAccelerate(CCAcceleration* pAccelerationValue);
	/** 初始化蜘蛛**/
	void initSpiders(); 

	/** 排列蜘蛛**/
	void resetSpiders();

	/** 蜘蛛的帧更新 动作**/
	void spiderUpdate(float dela);

	/** 蜘蛛的移动更新**/
	void runSpiderMoveSequence(CCSprite *sprite);
	/** 让蜘蛛在移动的过程中产生扭动效果,就是放大缩小**/
	void runSpiderWiggleSequence(CCSprite *sprite);
	/** 蜘蛛下降到平面底部的时候回调的方法**/
	void spiderBelowScreen(CCNode *node);

	/** 蜘蛛与player的碰撞检测 **/
	void checkSpiderCollision(float dela);
	/** 游戏结束**/
	void gameOver();

	/** 触屏事件回调,当手指离开屏幕,游戏重新开始**/
	void ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent);

	/** 重置游戏**/
	void resetGame();
private:
	CCSprite *player;
	CCPoint playerVelocity;

	CCArray *spiders;
	float spiderMoveDuration;
	int numSpiderMoved;

	CCLabelBMFont *font;

	//int totalTime;
	float totalTime;
	int score;

};
 
 
 
 
 
 
 
 
下面个是GameScene.cpp实现方法
 
 

#include "GameScene.h"
#include "SimpleAudioEngine.h"
using namespace CocosDenshion;

GameScene::GameScene(void):player(NULL)
{
	this->initGame();

	this->setTouchEnabled(true);
}


void GameScene::initGame()
{
	CCSize size = CCDirector::sharedDirector()->getWinSize();

	CCSprite *bg = CCSprite::create("bg.jpg");
	bg->setPosition(ccp(size.width*0.5f,size.height*0.5f));
	this->addChild(bg);

	player = CCSprite::create("player.png");
	player->setPosition(ccp(size.width/2,player->getContentSize().height/2));
	this->addChild(player);

	font = CCLabelBMFont::create("0","bitmapFontTest3.fnt");
	font->setPosition(ccp(size.width*0.5,size.height));
	font->setAnchorPoint(ccp(0.5f,1.0f));
	this->addChild(font,10);//addChild会自动retain

	//this->gameOver();
	CCLabelTTF *startTtf = CCLabelTTF::create("tap screen to play","Arial",20);
	startTtf->setPosition(ccp(size.width*0.5,size.height*0.25));
	this->addChild(startTtf,100,101);

	//动作:闪烁
	CCBlink *blink = CCBlink::create(10,20);
	CCRepeatForever *repeatBlink = CCRepeatForever::create(blink);
	startTtf->runAction(repeatBlink);
	initSpiders();

	SimpleAudioEngine::sharedEngine()->playBackgroundMusic("bg.mp3",true);
	//当有蜘蛛到最底部时候发出的声音,先加载到内存。
	//SimpleAudioEngine::sharedEngine()->preloadEffect("sipder.mp3");
}


GameScene::~GameScene(void)
{
	spiders->release();
	spiders = NULL;

}

void GameScene::didAccelerate( CCAcceleration* pAccelerationValue )
{
	//控制减速的速率    值越小,更容易改变方向
	float deceleration = 0.4f;
	//加速计的敏感值越大,主角对加速计的输入会越敏感
	float sensitivity = 6.0;
	//最大输入值
	float maxVelocity = 100.0f;

	playerVelocity.x = playerVelocity.x*deceleration + pAccelerationValue->x*sensitivity;
	if (playerVelocity.x>=maxVelocity)
	{
		playerVelocity.x = maxVelocity;
	}else if(playerVelocity.x <=-maxVelocity)
	{
		playerVelocity.x = -maxVelocity;
	}
}

void GameScene::update( float dt )
{
	CCPoint point = player->getPosition();
	point.x +=playerVelocity.x;

	CCSize size = CCDirector::sharedDirector()->getWinSize();
	//判断player是否移动到屏幕外面
	if(point.x>=(size.width-player->getContentSize().width*0.5))
	{
		point.x =size.width- player->getContentSize().width*0.5;
		playerVelocity = CCPointZero;
	}else if (point.x<=player->getContentSize().width*0.5)
	{
		point.x = player->getContentSize().width*0.5;
		playerVelocity = CCPointZero;
	}else{

	}
	player->setPosition(point);

	/*int totalTime =0;*/
	totalTime += dt;
	int currentTime = (int)totalTime;
	if (score < currentTime)
	{
		score = currentTime;
		CCString *s = CCString::stringWithFormat("%i",score);
		font->setString(s->getCString());
	}

	checkSpiderCollision(dt);

}

void GameScene::initSpiders()
{
	CCSize size = CCDirector::sharedDirector()->getWinSize();
	CCSprite *tempSprite = CCSprite::create("spider.png");
	//算出一行可以放多少蜘蛛
	int numSpiders = size.width/tempSprite->getContentSize().width;

	this->spiders = CCArray::create(numSpiders);
	spiders->retain();
	for(int i=0;i<numSpiders;i++)
	{
		CCSprite *spider = CCSprite::create("spider.png");
		this->addChild(spider,0,2);
		spiders->addObject(spider);
	}
}

void GameScene::resetSpiders()
{
	CCSize size = CCDirector::sharedDirector()->getWinSize();

	CCSprite *tempSprite =(CCSprite *) spiders->lastObject();
	//精灵的宽度
	int contentW = tempSprite->getContentSize().width;
	int contentH = tempSprite->getContentSize().height;
	int numSprites = spiders->count();

	for (int i=0;i<numSprites;i++)
	{
		CCSprite *spider = (CCSprite *)spiders->objectAtIndex(i);
		if (spider!=NULL)
		{
			//把蜘蛛放在顶部
			spider->setPosition(ccp(contentW*i+contentW*0.5,size.height+contentH*0.5));
			spider->stopAllActions();
		}
	}
	//如果之前的预约动作方法如果还存在就去掉,否则没有spiders效果
	unschedule(schedule_selector(GameScene::spiderUpdate));
	schedule(schedule_selector(GameScene::spiderUpdate),0.7f);

	numSpiderMoved = 0;
	spiderMoveDuration = 4.0f;
}

void GameScene::spiderUpdate( float dela )
{
	for(int i=0;i<10;i++)
	{
		int randomSpiderIndex = CCRANDOM_0_1()*(spiders->count());
		CCSprite *spider = (CCSprite *)spiders->objectAtIndex(randomSpiderIndex);
		if(spider->numberOfRunningActions() == 0)
		{
			this->runSpiderMoveSequence(spider);
			//这样可以确保每次只有一只蜘蛛在移动
			runSpiderWiggleSequence(spider);
			break;
		}
	}

}

void GameScene::runSpiderMoveSequence( CCSprite *sprite )
{
	CCSize size = CCDirector::sharedDirector()->getWinSize();

	//随着时间的推移,增加移动的蜘蛛数量 
	numSpiderMoved ++;

	if(numSpiderMoved%8==0 && spiderMoveDuration>2.0f)
	{
		spiderMoveDuration -=0.1f;
	}
	CCPoint belowScreenPostion = CCPointMake(sprite->getPosition().x,-sprite->getContentSize().height*0.5);

	CCMoveTo *move = CCMoveTo::create(spiderMoveDuration,belowScreenPostion);
	CCCallFuncN *call = CCCallFuncN::create(this,callfuncN_selector(GameScene::spiderBelowScreen));

	CCFiniteTimeAction *sequence = CCSequence::create(move,call,NULL);

	sprite->runAction(sequence);

}

void GameScene::runSpiderWiggleSequence(CCSprite *sprite)
{
	CCScaleTo *scaleUp = CCScaleTo::create(CCRANDOM_0_1()*2+1,1.5f);
	CCEaseBackInOut *easeUp = CCEaseBackInOut::create(scaleUp);

	CCScaleTo *scaleDown = CCScaleTo::create(CCRANDOM_0_1()*2+1,0.75f);
	CCEaseBackInOut *easeDown = CCEaseBackInOut::create(scaleDown);

	CCActionInterval *sequence =(CCActionInterval*)CCSequence::create(easeUp,easeDown,NULL);
	sprite->runAction(sequence);
}

void GameScene::spiderBelowScreen( CCNode *obj )
{
	CCAssert((CCSprite*)obj,"obj is not spider");

	CCSize size = CCDirector::sharedDirector()->getWinSize();
	CCSprite * spider = (CCSprite *)obj;
	CCPoint pos = CCPointMake(spider->getPosition().x,size.height+spider->getContentSize().height*0.5);
	spider->setPosition(pos);
	//SimpleAudioEngine::sharedEngine()->playEffect("spider.mp3");
}

void GameScene::checkSpiderCollision( float dela )
{
	int pW = player->getContentSize().width;
	int pRadius = pW*0.4;//粗略的计算

	CCSprite *tempSprite = (CCSprite *)spiders->lastObject();
	int sW = tempSprite->getContentSize().width;
	int sRadius = sW*0.4;//粗略的计算

	int maxCollisionDis = pRadius+sRadius; //蜘蛛与player的最短无交点的距离为半径相加

	int numSpiders = spiders->count();
	for(int i =0;i<numSpiders;i++)
	{
		CCSprite *spider = (CCSprite *)spiders->objectAtIndex(i);
		if (spider->numberOfRunningActions()==0)
		{//没有运动的蜘蛛就不检测
			continue;
		}
		//检测在运动的蜘蛛是否与player有交点重合
		float actualDistance = ccpDistance(player->getPosition(),spider->getPosition());
		if (actualDistance<maxCollisionDis)
		{
			this->gameOver();
		}
	}
}

void GameScene::gameOver()
{
	CCObject *obj = NULL;
	if(spiders!=NULL){
		CCARRAY_FOREACH(spiders,obj){
			CCSprite *s = (CCSprite*)obj;
			s->stopAllActions();
		}
	}
	this->resetSpiders();
	this->setAccelerometerEnabled(false);
	this->setTouchEnabled(true);
	this->unscheduleAllSelectors();

	CCSize size = CCDirector::sharedDirector()->getWinSize();
	CCLabelTTF *ttf = CCLabelTTF::create("GameOver","Marker Felt",60);
	ttf->setPosition(ccp(size.width/2,size.height/3));
	this->addChild(ttf,100,100);

	//颜色渐变
	CCTintTo *tint1 = CCTintTo::create(2,255,255,0);
	CCTintTo *tint2 = CCTintTo::create(2,255,255,255);
	CCTintTo *tint3 = CCTintTo::create(2,255,0,0);
	CCTintTo *tint4 = CCTintTo::create(2,255,0,255);
	CCTintTo *tint5 = CCTintTo::create(2,0,255,0);
	CCTintTo *tint6 = CCTintTo::create(2,0,255,255);

	CCActionInterval *sequence =(CCActionInterval*) CCSequence::create(
		tint1,
		tint2,
		tint3,
		tint4,
		tint5,
		tint6,NULL);
	CCRepeatForever *repeatTint = CCRepeatForever::create(sequence);
	ttf->runAction(repeatTint);

	//动作:转动,颤动
	CCRotateTo *rotate1 = CCRotateTo::create(2,3);
	CCEaseBounceInOut *bounce1 = CCEaseBounceInOut::create(rotate1);

	CCRotateTo *rotate2 = CCRotateTo::create(2,-3);
	CCEaseBounceInOut *bounce2 = CCEaseBounceInOut::create(rotate2);

	CCActionInterval *sequence2 = (CCActionInterval*)CCSequence::create(bounce1,bounce2,NULL);

	CCRepeatForever *repeatRotate = CCRepeatForever::create(sequence2);
	ttf->runAction(repeatRotate);

	//动作:跳动
	CCJumpBy *jump	= CCJumpBy::create(3,CCPointZero,size.height/3,1);
	CCRepeatForever *repeatJump = CCRepeatForever::create(jump);
	ttf->runAction(repeatJump);

	CCLabelTTF *startTtf = CCLabelTTF::create("tap screen to play again","Arial",20);
	startTtf->setPosition(ccp(size.width*0.5,size.height*0.25));
	this->addChild(startTtf,100,101);

	//动作:闪烁
	CCBlink *blink = CCBlink::create(10,20);
	CCRepeatForever *repeatBlink = CCRepeatForever::create(blink);
	startTtf->runAction(repeatBlink);
}

void GameScene::ccTouchesEnded( CCSet *pTouches, CCEvent *pEvent )
{
	resetGame();
}

void GameScene::resetGame()
{
	this->setTouchEnabled(false);
	this->removeChildByTag(100,true);
	this->removeChildByTag(101,true);

	this->setAccelerometerEnabled(true);
	this->scheduleUpdate();

	score = 0;
	totalTime = 0;
	font->setString("0");
	this->resetSpiders();
}

代码里有注释。大致上是实现手机的重力感应控制一个player在水平方向上移动。然后躲避蜘蛛。  然后会根据赢的时间现实在手机屏幕上。



下面是在android手机上安装以上的游戏ndk编译后的截图

使用cocos2d-x 写的蜘蛛闪避小游戏------从cocos2d移植过来的~有哪里缺陷的可以提噢。亲。_第1张图片


使用cocos2d-x 写的蜘蛛闪避小游戏------从cocos2d移植过来的~有哪里缺陷的可以提噢。亲。_第2张图片



我的资源里面有win32和android 的工程。可以去资源里面下载放在cocos2d-x 2.0.1的库目录下。 我那资源文件夹里没有打包库。



你可能感兴趣的:(游戏,android,null,手机,float,ttf)