#include "GameScene.h"
#include "GameObjHero.h"
#include "GameObjEnemy.h"
#include "GameHeroBullet.h"
#include "GameEnemyBullet.h"
#include "GameAboutScene.h"
#include "GameMenuScene.h"
using namespace cocos2d;
CCScene* GameMain::scene()
{
CCScene * scene = NULL;
do
{
// 'scene' is an autorelease object
scene = CCScene::create();
CC_BREAK_IF(! scene);
// 'layer' is an autorelease object
GameMain *layer = GameMain::create();
CC_BREAK_IF(! layer);
// add layer as a child to scene
scene->addChild(layer);
} while (0);
// return the scene
return scene;
}
void GameMain::releaseenemyBullet(int x,int y)
{
//遍历子弹数组,不再使用的子弹释放
for (int i=0;i < enemybullets->capacity();i++)
{
if (!((GameEnemyBullet*)enemybullets->objectAtIndex(i))->getIsvisble())
{
//设置位置,并设置为显示
((GameEnemyBullet*)enemybullets->objectAtIndex(i))->setPosition(ccp(x,y));
((GameEnemyBullet*)enemybullets->objectAtIndex(i))->setIsVisable();
break;
}
}
}
void GameMain::releaseheroBullet(int x,int y){
//遍历子弹数组,不在使用的子弹释放
for(int i = 0;i < bullets->capacity();i ++){
if(!((GameHeroBullet *)bullets->objectAtIndex(i))->getIsvisble()){
//设置位置,并设置为显示
((GameHeroBullet *)bullets->objectAtIndex(i))->setPosition(ccp(x,y));
((GameHeroBullet *)bullets->objectAtIndex(i))->setIsVisable();
break;
}
}
}
// on "init" you need to initialize your instance
bool GameMain::init()
{
if (!CCLayer::init())
{
return false;
}
CCSize size = CCDirector::sharedDirector()->getWinSize();
//创建背景
bg1 = CCSprite::create("bg.png");
bg1->setScale(0.5);
bg2 = CCSprite::create("bg.png");
bg2->setScale(0.5);
bg1->setAnchorPoint(ccp(0,0));
bg2->setAnchorPoint(ccp(0,0));
bg1->setPosition(ccp(0,0));
bg2->setPosition(ccp(0,size.height));
this->addChild(bg1,0);
this->addChild(bg2,0);
//创建主角
hero = new GameObjHero();
hero->setPosition(ccp(size.width/2,-50));
hero->setScale(0.5);
addChild(hero,2,1);
hero->runAction(CCMoveBy::create(0.5,ccp(0,150)));
//创建敌人
enemys = CCArray::createWithCapacity(3);
for (int i=0;i<3;i++)
{
int type = i+1;
GameObjEnemy* enemy = new GameObjEnemy();
enemy->setPosition(ccp(size.width/4 *type ,size.height+50));
enemy->setScale(0.5);
enemy->setType(type);
enemys->addObject(enemy);
this->addChild(enemy,1);
enemy->movestart();
}
enemys->retain();
//创建血量
blood = 3;
CCSpriteBatchNode* ui = CCSpriteBatchNode::create("cat.png");
blood1 = CCSprite::createWithTexture(ui->getTexture());
blood1 ->setPosition(ccp(20,size.height - 20));
blood1 ->setScale(0.2);
ui ->addChild(blood1);
blood2 = CCSprite::createWithTexture(ui->getTexture());
blood2 ->setPosition(ccp(50,size.height - 20));
blood2 ->setScale(0.2);
ui ->addChild(blood2);
blood3 = CCSprite::createWithTexture(ui->getTexture());
blood3 ->setPosition(ccp(80,size.height - 20));
blood3 ->setScale(0.2);
ui ->addChild(blood3);
addChild(ui,4);
//初始化主角子弹
bullets = CCArray::createWithCapacity(5);
for (int i=0;i < bullets->capacity(); i++)
{
GameHeroBullet * mybullet = new GameHeroBullet;
mybullet->setIsNotVisable(this);
mybullet->setScale(0.5);
bullets->addObject(mybullet);
this ->addChild(mybullet,3);
}
bullets->retain();
//初始化敌人子弹
enemybullets = CCArray::createWithCapacity(10);
for (int i=0;i < enemybullets->capacity();i++)
{
GameEnemyBullet * mybullet = new GameEnemyBullet();
mybullet->setIsNotVisable(this);
mybullet->setScale(0.5);
enemybullets->addObject(mybullet);
this->addChild(mybullet,3);
}
gamemark = new GameMark();
addChild(gamemark,4);
enemybullets->retain();
//初始化游戏结束画面及按钮
gameover = CCSprite::create("gameover.png");
gameover->setAnchorPoint(ccp(0.5,0.5));
gameover->setPosition(ccp(0,0));
gameover->setPosition(ccp(size.width/2,size.height/2 +70 ));
gameover->setVisible(false);
gameover->setScale(0.5);
this->addChild(gameover,5);
CCMenuItemImage *pCloseItem = CCMenuItemImage::create("back.png","back.png",this,menu_selector(GameMain::menuCloseCallback));
pCloseItem->setPosition(ccp(size.width/2,size.height/2 -50 ));
pCloseItem->setScale(0.5);
CCMenu* pMenu = CCMenu::create(pCloseItem,NULL);
pMenu->setPosition(CCPointZero);
this->addChild(pMenu,5,25);
pMenu->setVisible(false);
pMenu->setEnabled(false);
isreduce = false;
isover = false;
scheduleUpdate();
return true;
}
void GameMain::menuCloseCallback(CCObject* pSender)
{
// "close" menu item clicked
CCDirector::sharedDirector()->replaceScene(GameMenuScene::scene());
}
void GameMain::update(float time)
{
bg1 ->setPosition(ccp(bg1->getPosition().x,bg1->getPosition().y - 2));
bg2 ->setPosition(ccp(bg2->getPosition().x,bg2->getPosition().y - 2));
if (bg2->getPosition().y < 0)
{
float temp = bg2->getPosition().y + 480;
bg1->setPosition(ccp(bg2->getPosition().x,temp));
}
if (bg1->getPosition().y < 0 )
{
float temp = bg1->getPosition().y +480;
bg2->setPosition(ccp(bg1->getPosition().x,temp));
}
CCPoint hpos = hero->getPosition();
//敌人和主角子弹的碰撞检测
for (int i=0;i<3;i++)
{
GameObjEnemy * enemy = ((GameObjEnemy*)enemys->objectAtIndex(i));
CCPoint epos = enemy->getPosition();
if (enemy->islife)
{
for (int i = 0;i < bullets->capacity(); i++)
{
if (((GameHeroBullet*)bullets->objectAtIndex(i))->getIsvisble())
{
if (isCollion(((GameHeroBullet*)bullets->objectAtIndex(i))->getPosition(),epos,5,13,21,28))
{
enemy->setdie();
gamemark->addnumber(200);
break;
}
}
}
}
//敌人和主角的碰撞检测
if (!isreduce && enemy->islife && isCollion(hpos,epos,21,22.5,21,28))
{
enemy->setdie();
setherohurt();
}
if (!isreduce)
{
for (int i = 0;i < enemybullets ->capacity(); i++ )
{
if (isCollion(hpos,((GameEnemyBullet*)enemybullets->objectAtIndex(i))->getPosition(),21,22.5,5,13))
{
setherohurt();
}
}
}
}
}
void GameMain::setherohurt()
{
//主角受伤,减血
hero->stopAllActions();
switch (blood)
{
case 3:
blood1->setVisible(false);
blood --;
break;
case 2:
blood2->setVisible(false);
blood --;
break;
case 1:
blood3->setVisible(false);
blood --;
break;
case 0:
if (!isover)
{
isover = true;
setover();
}
break;
}
CCActionInterval* action = CCBlink::create(3,6); //5秒内闪烁10次
hero->runAction(action);
schedule(schedule_selector(GameMain::resetreduce),3.0f); // 无敌时间
isreduce = true;
}
void GameMain::resetreduce(float dt)
{
isreduce = false;//重新能收到伤害
}
void GameMain::setover()
{
//设置游戏结束
CCMenu* pMenu = (CCMenu*)this->getChildByTag(25);
pMenu->setVisible(true);
pMenu->setEnabled(true);
gameover->setVisible(true);
gameover->setScale(0);
pMenu->setScale(0);
pMenu->runAction(CCScaleTo::create(0.5,1));
gameover->runAction(CCScaleTo::create(0.5,0.5));
}
bool GameMain::isCollion(CCPoint p1,CCPoint p2,int w1,int h1,int w2,int h2)
{
//判断两个矩形是否碰撞
if (abs(p1.x - p2.x) < w1 + w2 && abs(p1.y - p2.y) < h1 + h2 )
{
return true;
}
return false;
}
void GameMain::menuBackCallback(CCObject* pSender)
{
CCDirector::sharedDirector()->replaceScene(GameMenuScene::scene());
}
GameMain::GameMain(void)
{
}
GameMain::~GameMain(void)
{
}
void GameMain::onEnter()
{
CCLayer::onEnter();
}
void GameMain::onExit()
{
CCLayer::onExit();
}
- 1
- 2
前往页