源代码下载地址:http://download.csdn.net/detail/danming60520/5276260
这篇文章设计的小游戏是根据 “无脑码农”的视频和cocos2d-x的官方帮助文档中的例子(感谢无脑的无私奉献),进行编写和学习的,由于官方的例子是横屏的,而且动画过于单调,所以按照自己的喜好重新设计,改为竖屏,同时加入了多个怪物以及他们行走的动作,也修改了他们随机出现和行走的速度,加入了杀死怪兽的计数器,说明一下cocos2d-x的版本是:cocos2d-2.1rc0-x-2.1.2-hotfix + VS2010。
这个游戏引入了一些资源和声音效果,先看一个怪物的图片资源(DIY的),主要是为了程序读取图片资源的时候节省一些内存;
这里只贴出主要代码如下:(稍后会附上源程序供大家参考)
bool HelloWorld::init()
{
bool bRet = false;
do
{
CC_BREAK_IF(! CCLayer::init());
CCSize size = CCDirector::sharedDirector()->getWinSize(); 获取屏幕大小//
//
// super init first
//
CCLabelTTF* pLabel = CCLabelTTF::create("Kill 100 Monsters will win!", "Arial", 20);
CC_BREAK_IF(! pLabel);
pLabel->setPosition(ccp(pLabel->getContentSize().width/2, size.height - 10));
pLabel->setColor(ccc3(50,255,50));
// Add the label to HelloWorld layer as a child layer.
this->addChild(pLabel, 1);
pLabeCount = CCLabelTTF::create("0", "Arial", 28);
CC_BREAK_IF(! pLabeCount);
pLabeCount->setPosition(ccp(pLabeCount->getContentSize().width, size.height - 40));
pLabeCount->setColor(ccc3(255,0,0));
// Add the label to HelloWorld layer as a child layer.
this->addChild(pLabeCount,1);
// 3. Add add a splash screen, show the cocos2d splash image.
CCSprite* pSpriteBK = CCSprite::create("background.png");
CC_BREAK_IF(! pSpriteBK);
// Place the sprite on the center of the screen
pSpriteBK->setPosition(ccp(size.width/2, size.height/2));
// Add the sprite to HelloWorld layer as a child layer.
this->addChild(pSpriteBK, 0);
///创建一个精灵(主角)//
//生成动画所需要的数据
CCTexture2D* pTexture = CCTextureCache::sharedTextureCache()->addImage("Angel.png");
CCSpriteFrame* pFrame = NULL;
CCArray* pArray = CCArray::createWithCapacity(4);
for (int i = 0; i < 4; i++)
{
pFrame = CCSpriteFrame::createWithTexture(pTexture,CCRectMake(48 * i, 48 * 0, 48, 48));
pArray->addObject(pFrame);
}
CCAnimation* pAnimation = CCAnimation::createWithSpriteFrames(pArray,0.1f);
pArray->release();
CCSprite* pSprite = CCSprite::createWithSpriteFrame(pFrame);
pSprite->setPosition(ccp(size.width/2, pSprite->getContentSize().height/2));
addChild(pSprite);
CCAnimate* pAnimate = CCAnimate::create(pAnimation);
pSprite->runAction(CCRepeatForever::create(pAnimate));
//schedule_selector 监听//
this->schedule( schedule_selector(HelloWorld::GameLogic), 1.0 );
this->schedule( schedule_selector(HelloWorld::MonsterMoveFinished)); //检测怪物移出屏幕时的响应
//每次绘制判断碰撞///
this->schedule( schedule_selector(HelloWorld::Update) );
//添加背景音乐//
CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic(
"background-music-aac.wav", true);
设置触摸有效///
this->setTouchEnabled(true);
bRet = true;
} while (0);
return bRet;
}
void HelloWorld::AddTarget()
{
//生成动画所需要的数据//
CCTexture2D* pTexture = CCTextureCache::sharedTextureCache()->addImage("Monster.png");
CCSpriteFrame* pFrame = NULL;
CCArray* pArray = CCArray::createWithCapacity(4);
int minPosition= (int) 0;
int maxPosition= (int) 8;
int rangePosition = maxPosition - minPosition;
int actualPosition = rand() % rangePosition;
for (int i = 0; i < 4; i++)
{
pFrame = CCSpriteFrame::createWithTexture(pTexture, CCRectMake(80*i, 96*actualPosition, 80,96));
pArray->addObject(pFrame);
}
CCAnimation* pAnimation = CCAnimation::createWithSpriteFrames(pArray,0.25f);
pArray->release();
CCSize size = CCDirector::sharedDirector()->getWinSize();
CCSprite* pSpriteMonster = CCSprite::createWithSpriteFrame(pFrame);
//计 算可绘制的范围// 为了避免只显示一部分精灵的图片,必须设定精灵可以出 现的位 置//
int minX = pSpriteMonster->getContentSize().width/2;
int maxX = size.width - minX;
//计算可随机基数
int rangeX = maxX - minX;
//随机出的基数*半个身位 = 最后的坐标点
int actualX = (rand() % rangeX) + minX;
pSpriteMonster->setPosition(ccp(actualX,size.height - pSpriteMonster->getContentSize().height/2));
this->addChild(pSpriteMonster);
pSpriteMonster->setTag(1); //设置怪物标识//
pArrayMonster->addObject(pSpriteMonster); //将产生的怪物加入数组//
CCAnimate* pAnimate = CCAnimate::create(pAnimation);
//计算移动速度 最慢5秒移动竖屏 最快1秒
int minDuration = (int) 1.0;
int maxDuration = (int) 5.0;
int rangeDuration = maxDuration - minDuration;
int actualDuration = (rand() % rangeDuration) +minDuration;
CCPoint moveByPosition = ccp(0,-(10 * actualDuration));
//计算目标坐标,用当前怪物坐标加上移动距离//
CCPoint monsterPosition = ccpAdd(pSpriteMonster->getPosition(), moveByPosition);
//利用CCSpawn将行走动画和移动同时执行
CCAction *action =CCSpawn::createWithTwoActions(pAnimate,
CCMoveBy::create(1.0f, moveByPosition));
// CCFiniteTimeAction* actionMoveDone = CCCallFuncN::create(this, callfuncN_selector(HelloWorld::SpriteMoveFinished));
pSpriteMonster->runAction(CCRepeatForever::create((CCActionInterval*)action));
}
void HelloWorld::ccTouchesEnded(cocos2d::CCSet* touches, cocos2d::CCEvent* event)
{
CCTouch* touch = (CCTouch*)touches->anyObject();
CCPoint location = touch->getLocationInView();
location = CCDirector::sharedDirector()->convertToGL(location);
CCSize size = CCDirector::sharedDirector()->getWinSize();
CCSprite* pSpriteWeapon = CCSprite::create("Weapon.png",CCRectMake(0,0,26,26));
pSpriteWeapon->setPosition(ccp(size.width / 2-15, 70));
int offX = location.x - pSpriteWeapon->getPosition().x;
int offY = location.y - pSpriteWeapon->getPosition().y;
if(offY <= 0) return;
//添加发子弹声音r//
CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(
"pew-pew-lei.wav");
this->addChild(pSpriteWeapon,1);
pSpriteWeapon->setTag(2);
pArrayWeapon->addObject(pSpriteWeapon);
int realY = size.height + (pSpriteWeapon->getContentSize().height / 2);
float ratio = (float)offX / (float)offY;
int realX = (realY * ratio) + pSpriteWeapon->getPosition().x;
CCPoint realDest = ccp(realX,realY);
int offRealX = realX - pSpriteWeapon->getPosition().x;
int offRealY = realY - pSpriteWeapon->getPosition().y;
float length = sqrtf((offRealX * offRealX) + (offRealY*offRealY));
float velocity = 480/1;
float realMoveDuration = length/velocity;
pSpriteWeapon->runAction(CCSequence::create(
CCMoveTo::create(realMoveDuration,realDest)
,CCCallFuncN::create(this,callfuncN_selector(HelloWorld::SpriteMoveFinished))
,NULL));
}
程序运行的效果:
程序还可以进行更多的修改和设置,有待大家继续努力。加油!!
源代码下载地址:http://download.csdn.net/detail/danming60520/5276260