#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
using namespace cocos2d;
using namespace CocosDenshion;
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;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//
// 1. super init first
if ( !CCLayer::init() )
{
return false;
}
//初始化数据
initData();
//载入背景
loadBackground();
loadPlayer();
madeBullet();
resetBullet();
//系统更新函数
scheduleUpdate();
return true;
}
void HelloWorld::initData()
{
adjustmentBG = 568;
isGameOver = false;
//子弹初速度是25
bulletSpeed = 25;
//各种类型飞机出现的时间间隔
smallPlaneTime = 0;
mediumPlaneTime = 0;
bigPlaneTime = 0;
bulletLastTime = 1200;
//空降物品计数
propTime = 0;
//得分计数
scoreInt = 0;
isBigBullet = false;
isChangeBullet = false;
foePlanes = CCArray::create();
foePlanes->retain();
}
void HelloWorld::loadBackground()
{
//下雪拉
CCParticleSystem* particleSyetem = CCParticleSnow::create();
particleSyetem->setTexture( CCTextureCache::sharedTextureCache()->addImage("snow.png"));
addChild(particleSyetem,1);
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("gameArts.plist");
CCSpriteBatchNode *spriteSheet =CCSpriteBatchNode::batchNodeWithFile("gameArts.png");
addChild(spriteSheet);
BG1= CCSprite::spriteWithSpriteFrameName("background_2.png");
BG2= CCSprite::spriteWithSpriteFrameName("background_2.png");
BG1->setPosition(ccp(0,0));
BG2->setPosition(ccp(0,566));
BG1->setAnchorPoint(ccp(0,0));
BG2->setAnchorPoint(ccp(0,0));
addChild(BG1,0,888);
addChild(BG2,0,999);
//显示分数的Label
scoreLabel = CCLabelTTF::create("Score:0000", "Zapfino", 20);
scoreLabel->setPosition(ccp(170,450));
addChild(scoreLabel,4,333);
//显示暂停按钮
strike=CCMotionStreak::streakWithFade(1.0f,//尾巴持续的时间
16.0f,//尾巴大小
16.0f,//图片的大小
ccc3(0,0,255),//颜色
"fire.png"//使用的图片
);
addChild(strike,1);
strike->setPosition(ccp(240,160));
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
"BurstAircraftPause.png",
"BurstAircraftPause.png",
this,
menu_selector(HelloWorld::GamePause) );
pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width -300, 460) );
// create menu, it's an autorelease object
CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
pMenu->setPosition( CCPointZero );
this->addChild(pMenu, 1);
}
void HelloWorld::scrollBackground()
{
CCSprite *bg1 = CCSprite::create();
bg1 = (CCSprite *)this->getChildByTag(888);
CCSprite *bg2 = CCSprite::create();
bg2 = (CCSprite *)this->getChildByTag(999);
bg1->setPositionY(bg1->getPositionY()-2);//背景1向下移动
bg2->setPositionY(bg1->getPositionY()+566);//背景2跟随背景1向下移动
if(bg2->getPositionY() == 0)
{
bg1->setPositionY(0);
}
}
void HelloWorld::loadPlayer()
{
//从gameArts.png上面截取一个小图片
player = CCSprite::create("gameArts.png", CCRectMake(432, 0, 66, 82));
//锚点
player->setAnchorPoint(ccp(0.5, 0.5));
addChild(player,2,555);
//创建动画
CCAnimation *animation = CCAnimation::create();
animation->setDelayPerUnit(0.1f);
//添加每个zhen
animation->addSpriteFrame(CCSpriteFrame::create("gameArts.png",CCRectMake(432, 0, 66, 82)));
animation->addSpriteFrame(CCSpriteFrame::create("gameArts.png",CCRectMake(432, 332, 66, 80)));
CCAnimate *anim = CCAnimate::create(animation);
player->runAction(::cocos2d::CCRepeatForever::create(anim));
player->setAnchorPoint(ccp(0.5, 0.5));
player->setPosition(ccp(160,50));
}
void HelloWorld::madeBullet()
{
bullet = CCSprite::spriteWithSpriteFrameName((!isBigBullet)?"bullet1.png":"bullet2.png");
bullet->setAnchorPoint(ccp(0.5, 0.5));
addChild(bullet);
}
void HelloWorld::resetBullet()
{
if ((isBigBullet&&isChangeBullet) || (!isBigBullet&&isChangeBullet))
{
bullet->removeFromParentAndCleanup(false);
madeBullet();
isChangeBullet = false;
}
bulletSpeed = (480 - (player->getPositionY() + 50))/15;
if (bulletSpeed<</span>5)
{
bulletSpeed=5;
}
bullet->setPosition(ccp(player->getPositionX(), player->getPositionY()+50));
}
void HelloWorld::firingBullet()
{
bullet->setPosition(ccp(bullet->getPositionX(), bullet->getPositionY()+bulletSpeed));
if (bullet->getPositionY()>460) {
resetBullet();
}
}
void HelloWorld::onEnter(){
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,0,false);
CCLayer::onEnter();
}
void HelloWorld::onExit(){
CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
CCLayer::onExit();
}
bool HelloWorld::ccTouchBegan(CCTouch* touch, CCEvent* event){
CCLOG("ccTouchBegan");
return true;
}
void HelloWorld::ccTouchMoved(CCTouch* touch, CCEvent* event){
//CCLOG("ccTouchMoved");
//飞机烟花么?
getLine(touch->getLocation());
strike->setPosition(touch->getLocation());
CCSprite *nowSprite = (CCSprite *)this->getChildByTag(555);
if(
(
(nowSprite->getPosition().x-touch->getLocation().x)<<span style="color: #272ad8">50
&&(nowSprite->getPosition().x-touch->getLocation().x)>-50
)
&&
(
(nowSprite->getPosition().y-touch->getLocation().y)<<span style="color: #272ad8">30
&&(nowSprite->getPosition().y-touch->getLocation().y)>-30
)
)
{
nowSprite->setPosition(touch->getLocation());
}
//CCLOG("nowSprite %f %f ",nowSprite->getPosition().x,nowSprite->getPosition().y);
//CCLOG("touch %f %f ",touch->getLocation().x,touch->getLocation().y);
}
void HelloWorld::ccTouchEnded(CCTouch* touch, CCEvent* event){
//CCLOG("ccTouchEnded");
getBoom(touch->getLocation());
}
void HelloWorld::update(float dt)
{
if(!isGameOver)
{
scrollBackground();
firingBullet();
addFoePlane();
moveFoePlane();
bulletLast();
collisionDetection();
}
}
void HelloWorld::bulletLast()
{
if (isBigBullet)
{
if (bulletLastTime > 0)
{
bulletLastTime --;
}
else
{
bulletLastTime = 1200;
isBigBullet = false;
isChangeBullet = true;
}
}
}
void HelloWorld::collisionDetection()
{
//检测子弹和敌方飞机是否碰撞
for (int i = 0;i<<span style="color: #4f8187">foePlanes->count();i++)
{
FoePlane *tmpPlane =(FoePlane *) foePlanes->objectAtIndex(i);
if (tmpPlane->boundingBox().intersectsRect(bullet->boundingBox()))
{
//CCLOG("子弹和敌方飞机相撞");
//子弹重设
resetBullet();
//判读敌人飞机是否还有血
tmpPlane->hp = tmpPlane->hp - (isBigBullet?2:1);//一个子弹一滴血
if (tmpPlane->hp <= 0)
{
//运行爆炸动画
//CCLOG("运行爆炸动画");
//各种类型的飞机爆炸的效果
foePlaneBlowup(tmpPlane);
foePlanes->removeObject(tmpPlane);
}
else
{
//CCLOG("每个子弹都有打击效果 主要是中等飞机和大飞机");
//每个子弹都有打击效果 主要是中等飞机和大飞机
//[self hitAnimationToFoePlane:tmpPlane];
}
}
}
//检测敌方飞机和我方飞机是否相撞
for (int i = 0;i<<span style="color: #4f8187">foePlanes->count();i++)
{
FoePlane *tmpPlane =(FoePlane *) foePlanes->objectAtIndex(i);
if (tmpPlane->boundingBox().intersectsRect(player->boundingBox()))
{
CCLOG("玩家飞机和敌方飞机相撞");
//游戏结束
GameOver();
//玩家飞机爆炸
playerPlaneBlowup();
//敌方飞机爆炸
foePlaneBlowup(tmpPlane);
//敌方飞机移出数组
}
}
}
void HelloWorld::GameOver()
{
isGameOver = true;
GamePause();
gameOverLabel = CCLabelTTF::create("GAME OVER", "Zapfino", 20);
gameOverLabel->setPosition(ccp(160, 300));
addChild(gameOverLabel,4);
CCMenuItemFont *gameOverItem = CCMenuItemFont::create("Restart",this,menu_selector(HelloWorld::GameRestart));
restart = CCMenu::create(gameOverItem,NULL);
restart->setPosition(ccp(160, 200));
addChild(restart,4);
}
void HelloWorld::GamePause()
{
if (isGameOver == false) {
CCMenuItemFont *gameOverItem = CCMenuItemFont::create("Start Game", this,menu_selector(HelloWorld::GameStart));
gameOverItem->setFontName("Zapfino");
gameOverItem->setFontSize(20);
restart = CCMenu::create(gameOverItem,NULL);
addChild(restart,4);
isGameOver = true;
}
else
{
//prop->stopAllActions;
}
}
void HelloWorld::GameStart()
{
if (isGameOver) {
removeChild(restart, true);
isGameOver = false;
}
}
void HelloWorld::GameRestart()
{
removeAllChildrenWithCleanup(true);
foePlanes->removeAllObjects();
initData();
loadBackground();
loadPlayer();
madeBullet();
resetBullet();
}
void HelloWorld::playerPlaneBlowup()
{
//首先飞机停止所有动画
player->stopAllActions();
//创建动画
CCAnimation *animation = CCAnimation::create();
animation->setDelayPerUnit(0.1f);
//添加每个zhen
animation->addSpriteFrame(CCSpriteFrame::create("gameArts.png",CCRectMake(432, 249, 66, 82)));
animation->addSpriteFrame(CCSpriteFrame::create("gameArts.png",CCRectMake(432, 83, 66, 82)));
animation->addSpriteFrame(CCSpriteFrame::create("gameArts.png",CCRectMake(432, 166, 66, 82)));
animation->addSpriteFrame(CCSpriteFrame::create("gameArts.png",CCRectMake(47, 569, 44, 52)));
CCAnimate *anim = CCAnimate::create(animation);
player->runAction(CCSequence::actions(anim,NULL));
}
void HelloWorld::foePlaneBlowup(FoePlane *foePlane)
{
int animationNum = 0;
if (foePlane->planeType == 1)
{
animationNum = 4;
scoreInt += 2000;
//首先飞机停止所有动画
foePlane->stopAllActions();
//创建动画
CCAnimation *animation = CCAnimation::create();
animation->setDelayPerUnit(0.1f);
//添加每个zhen
animation->addSpriteFrame(CCSpriteFrame::create("gameArts.png",CCRectMake(47, 657, 34, 24)));
animation->addSpriteFrame(CCSpriteFrame::create("gameArts.png",CCRectMake(420, 729, 34, 32)));
animation->addSpriteFrame(CCSpriteFrame::create("gameArts.png",CCRectMake(472, 719, 38, 34)));
animation->addSpriteFrame(CCSpriteFrame::create("gameArts.png",CCRectMake(479, 413, 32, 32)));
CCAnimate *anim = CCAnimate::create(animation);
CCCallFuncN *end = CCCallFuncN::actionWithTarget(this,callfuncN_selector(HelloWorld::blowupEnd));
foePlane->runAction(CCSequence::actions(anim,end,NULL));
}
if (foePlane->planeType == 3)
{
animationNum = 4;
scoreInt += 10000;
//首先飞机停止所有动画
foePlane->stopAllActions();
//创建动画
CCAnimation *animation = CCAnimation::create();
animation->setDelayPerUnit(0.1f);
//添加每个zhen
animation->addSpriteFrame(CCSpriteFrame::create("gameArts.png",CCRectMake(432, 538, 46, 60)));
animation->addSpriteFrame(CCSpriteFrame::create("gameArts.png",CCRectMake(432, 599, 46, 60)));
animation->addSpriteFrame(CCSpriteFrame::create("gameArts.png",CCRectMake(432, 476, 46, 62)));
animation->addSpriteFrame(CCSpriteFrame::create("gameArts.png",CCRectMake(0, 628, 46, 52)));
CCAnimate *anim = CCAnimate::create(animation);
CCCallFuncN *end = CCCallFuncN::actionWithTarget(this,callfuncN_selector(HelloWorld::blowupEnd));
foePlane->runAction(CCSequence::actions(anim,end,NULL));
}
if (foePlane->planeType == 2)
{
animationNum = 7;
scoreInt += 40000;
//首先飞机停止所有动画
foePlane->stopAllActions();
//创建动画
CCAnimation *animation = CCAnimation::create();
animation->setDelayPerUnit(0.1f);
//添加每个zhen
animation->addSpriteFrame(CCSpriteFrame::create("gameArts.png",CCRectMake(0, 754, 110, 169)));
animation->addSpriteFrame(CCSpriteFrame::create("gameArts.png",CCRectMake(321, 171, 110, 169)));
animation->addSpriteFrame(CCSpriteFrame::create("gameArts.png",CCRectMake(321, 514, 110, 169)));
animation->addSpriteFrame(CCSpriteFrame::create("gameArts.png",CCRectMake(321, 343, 110, 169)));
animation->addSpriteFrame(CCSpriteFrame::create("gameArts.png",CCRectMake(321, 0, 110, 169)));
animation->addSpriteFrame(CCSpriteFrame::create("gameArts.png",CCRectMake(213, 569, 94, 133)));
animation->addSpriteFrame(CCSpriteFrame::create("gameArts.png",CCRectMake(117, 569, 94, 133)));
CCAnimate *anim = CCAnimate::create(animation);
CCCallFuncN *end = CCCallFuncN::actionWithTarget(this,callfuncN_selector(HelloWorld::blowupEnd));
foePlane->runAction(CCSequence::actions(anim,end,NULL));
}
CCString *scoreString = CCString::createWithFormat("Score:%d",scoreInt);
scoreLabel->setString(scoreString->getCString());
}
void HelloWorld::blowupEnd(FoePlane *foePlane)
{
FoePlane *tmpPlane = (FoePlane *)foePlane;
tmpPlane->removeFromParentAndCleanup(false);
}
void HelloWorld::addFoePlane()
{
smallPlaneTime ++;
mediumPlaneTime ++;
bigPlaneTime ++;
if (smallPlaneTime > 25)
{
//CCLOG("制造小飞机");
FoePlane *smallPlane = HelloWorld::makeSmallFoePlane();
addChild(smallPlane,3);
foePlanes->addObject(smallPlane);
smallPlaneTime = 0;
}
if (mediumPlaneTime > 400)
{
//CCLOG("制造中飞机");
FoePlane *mediumPlane = HelloWorld::makemediumFoePlane();
addChild(mediumPlane,3);
foePlanes->addObject(mediumPlane);
mediumPlaneTime = 0;
}
if (bigPlaneTime > 700)
{
//CCLOG("制造大飞机");
FoePlane *bigPlane = HelloWorld::makeBigFoePlane();
addChild(bigPlane,3);
foePlanes->addObject(bigPlane);
// [self performSelector:@selector(bigPlaneOutSount) withObject:nil afterDelay:0.5];
bigPlaneTime = 0;
}
//CCLOG("foePlanes === %d",foePlanes->count());
}
FoePlane * HelloWorld::makeSmallFoePlane()
{
FoePlane *smallPlane =(FoePlane *) FoePlane::spriteWithSpriteFrameName("enemy1_fly_1.png");
smallPlane->setAnchorPoint(ccp(0, 0));
smallPlane->setPosition(ccp(arc4random()%290+17, 468));
smallPlane->planeType = 1;
smallPlane->hp = 1;
smallPlane->speed = arc4random()%4 + 2;
//CCLOG(" smallPlane x= %d y = %d ",smallPlane->getPositionX(),smallPlane->getPositionY());
return smallPlane;
}
FoePlane * HelloWorld::makemediumFoePlane()
{
FoePlane *mediumPlane =(FoePlane *) FoePlane::spriteWithSpriteFrameName("enemy3_fly_1.png");
mediumPlane->setAnchorPoint(ccp(0, 0));
mediumPlane->setPosition(ccp(arc4random()%280+23, 468));
mediumPlane->planeType = 3;
mediumPlane->hp = 15;
mediumPlane->speed = arc4random()%3 + 2;
//CCLOG(" mediumPlane x= %d y = %d ",mediumPlane->getPositionX(),mediumPlane->getPositionY());
return mediumPlane;
}
FoePlane * HelloWorld::makeBigFoePlane()
{
FoePlane *bigPlane =(FoePlane *) FoePlane::spriteWithSpriteFrameName("enemy2_fly_1.png");
bigPlane->setAnchorPoint(ccp(0, 0));
bigPlane->setPosition(ccp(arc4random()%210+55, 500));
bigPlane->planeType = 2;
bigPlane->hp = 25;
bigPlane->speed = arc4random()%2 + 2;
//CCLOG(" bigPlane x= %d y = %d ",bigPlane->getPositionX(),bigPlane->getPositionY());
return bigPlane;
}
void HelloWorld::moveFoePlane()
{
for (int i = 0 ; i<<span style="color: #4f8187">foePlanes->count(); i++)
{
FoePlane* temPlane = (FoePlane*)foePlanes->objectAtIndex(i);
//CCLog(" temPlane x= %d y = %d ",temPlane->getPositionX(),temPlane->getPositionY());
temPlane->setPosition(ccp(temPlane->getPositionX(), temPlane->getPositionY()-temPlane->speed));
if (temPlane->getPositionY() < (-75))
{
foePlanes->removeObject(temPlane);
temPlane->removeFromParentAndCleanup(false);
}
}
}
void HelloWorld::getLine(CCPoint pt)
{
cocos2d::CCParticleSystemQuad*mSystem=CCParticleSystemQuad::particleWithFile("himiParticle.plist");
//mSystem->initWithFile("Particle.plist");//plist文件可以通过例子编辑器获得
mSystem->setTextureWithRect(CCTextureCache::sharedTextureCache()->addImage("fire.png")
,CCRectMake(0,0,32,32));//加载图片,第一个参数是纹理,第二个参数是选择图片上的位置
// mSystem->setBlendAdditive(true);//这个调用必不可少
mSystem->setPosition(pt);//设置位置
mSystem->setDuration(0.000003f);
mSystem->setLife(0.000005f);
addChild(mSystem);
//mSystem->release();
//delete mSystem;
//CC_SAFE_DELETE(mSystem);
mSystem->setAutoRemoveOnFinish(true);
}
void HelloWorld::getBoom(CCPoint pt)
{
cocos2d::CCParticleSystemQuad *mSystem2 =CCParticleSystemQuad::particleWithFile("himiParticle.plist");
//mSystem2->initWithFile("Boom.plist");//plist文件可以通过例子编辑器获得
mSystem2->setTextureWithRect(CCTextureCache::sharedTextureCache()->addImage("stars.png")
,CCRectMake(0,0,32,32));//加载图片,第一个参数是纹理,第二个参数是选择图片上的位置
mSystem2->setBlendAdditive(true);//这个调用必不可少
mSystem2->setPosition(pt);//设置位置
mSystem2->setDuration(0.05f);
addChild(mSystem2);
//mSystem2->release();
//CC_SAFE_DELETE(mSystem2);
mSystem2->setAutoRemoveOnFinish(true);
}
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}
2飞机类
//
// FoePlane.h
// xiaoguodongAttackPlane
//
// Created by Dong on 13-10-9.
//
//
#ifndef __xiaoguodongAttackPlane__FoePlane__
#define __xiaoguodongAttackPlane__FoePlane__
#include
#include "cocos2d.h"
#include "cocos-ext.h"
using namespace cocos2d::extension;
class FoePlane:public cocos2d::CCSprite
{
public:
virtual bool init();
static cocos2d::CCScene* scene();
CREATE_FUNC(FoePlane);
int planeType;
int hp;
int speed;
};
#endif
直接运行肯定会崩溃的
需要加入游戏的一些资源文件
自己还添加了一些粒子效果
虽然风格和原来的黑白画面不太协调而且很花哨但是自己很喜欢。。。
没有加道具和音效,期待3.0版本吧,嘻嘻