#include "GameScene.h"
#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
using namespace CocosDenshion;
USING_NS_CC;
Scene* GameScene::createScene()
{
auto gameScene = Scene::create();
auto gameLayer = GameScene::create();
gameScene->addChild(gameLayer);
return gameScene;
}
bool GameScene::init()
{
if(!Layer::init())
{
return false;
}
//背景1
auto bg = Sprite::create("background4.png");
bg->setPosition(Point::ZERO);
bg->setAnchorPoint(Point::ZERO);
bg->setTag(10);
this->addChild(bg);
//背景2
auto bg2 = Sprite::create("background4.png");
bg2->setPosition(Point::ZERO);
bg2->setAnchorPoint(Point::ZERO);
bg2->setPositionY(bg->getPositionY() + bg->getContentSize().height);
bg2->setTag(11);
this->addChild(bg2);
//创建分数
Label* scoreLabel = Label::create("Score:0","",20);
scoreLabel->setTag(99);
scoreLabel->setPosition(60, 450);
this->addChild(scoreLabel,2);
//移动背景
this->schedule(schedule_selector(GameScene::moveBackground),0.01);
//创建飞机
auto plane = Sprite::create();
plane->setPosition(Point(160,240));
plane->setTag(110);
this->addChild(plane);
Vector<SpriteFrame*> allFrame;
for(int i=0;i<4;i++)
{//设置飞机动画
SpriteFrame* sf = SpriteFrame::create("player.png",Rect(i*47,0,47,56));
allFrame.pushBack(sf);
}
Animation* ani = Animation::createWithSpriteFrames(allFrame,0.3);
plane->runAction(RepeatForever::create(Animate::create(ani)));
//设置飞机触摸事件
EventListenerTouchOneByOne* touchMove = EventListenerTouchOneByOne::create();
touchMove->setSwallowTouches(true);
touchMove->onTouchBegan = CC_CALLBACK_2(GameScene::onTouchBegan,this);
touchMove->onTouchMoved = CC_CALLBACK_2(GameScene::onTouchMoved,this);
touchMove->onTouchEnded = CC_CALLBACK_2(GameScene::onTouchEnded,this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(touchMove,this);
//初始化子弹
this->schedule(schedule_selector(GameScene::newBullet),0.3);
this->schedule(schedule_selector(GameScene::moveBullet),0.01);
//初始化敌机
this->schedule(schedule_selector(GameScene::newEnemy),0.4);
this->schedule(schedule_selector(GameScene::moveEnemy),0.01);
//Collision
this->scheduleUpdate();
return true;
}
bool GameScene::onTouchBegan(Touch *pTouch, Event *pEvent)
{
px = pTouch->getLocation().x;
py = pTouch->getLocation().y;
return true;
}
void GameScene::onTouchMoved(Touch *pTouch, Event *pEvent)
{
auto plane = this->getChildByTag(110);
auto move = MoveTo::create(0.1, plane->getPosition() + pTouch->getDelta());
plane->runAction(move);
// int mx = abs(pTouch->getLocation().x-px);
// int my = abs(pTouch->getLocation().y-py);
// auto plane = this->getChildByTag(110);
// plane->runAction(MoveBy::create(0,Point(mx,my)));
// px = pTouch->getLocation().x;
// py = pTouch->getLocation().y;
// plane->setPosition(Point(px,py));
}
void GameScene::onTouchEnded(Touch *pTouch, Event *pEvent)
{
}
void GameScene::moveBackground(float td)
{
auto bg1 = this->getChildByTag(10);
auto bg2 = this->getChildByTag(11);
bg1->setPositionY(bg1->getPositionY()-1);
if(bg1->getPositionY()< -bg1->getContentSize().height)
{
bg1->setPositionY(0);
}
bg2->setPositionY(bg1->getPositionY() + 680);
}
void GameScene::newBullet(float td)
{
auto plane = this->getChildByTag(110);
Sprite* bullet = Sprite::create("bullet3.png");
bullet->setPosition(plane->getPosition());
this->addChild(bullet);
this->allBullets.pushBack(bullet);
}
void GameScene::moveBullet(float td)
{
for(int i=0;i<allBullets.size();i++)
{
auto nowBullet = allBullets.at(i);
nowBullet->setPositionY(nowBullet->getPositionY()+3);
if(nowBullet->getPositionY() > Director::getInstance()->getWinSize().height)
{//删除子弹
nowBullet->removeFromParent();
allBullets.eraseObject(nowBullet);
i--;
}
}
}
void GameScene::newEnemy(float td)
{
Sprite* enemy = nullptr;
int num = rand()%10;
if(num>=3)
{
enemy = Sprite::create("aaa.png");
}else
{
enemy = Sprite::create("ccc.png");
}
enemy->setPosition(Point((rand()%300 + 10),500));
enemy->setTag(120);
this->addChild(enemy);
this->allEnemy.pushBack(enemy);
}
void GameScene::moveEnemy(float td)
{
for(int i=0;i<allEnemy.size();i++)
{
auto nowenemy = allEnemy.at(i);
nowenemy->setPositionY(nowenemy->getPositionY()-3);
if(nowenemy->getPositionY() < 0)
{//删除敌机
nowenemy->removeFromParent();
allEnemy.eraseObject(nowenemy);
i--;
}
}
}
//Collision
void GameScene::update(float td)
{
auto plane = this->getChildByTag(110);
Rect rp(plane->getPositionX(),plane->getPositionY(),45,54);
for (int i=0; i<allEnemy.size(); i++)
{
auto nowEnemy = allEnemy.at(i);
Rect er(nowEnemy->getPositionX(),nowEnemy->getPositionY(),35,45);
if (rp.intersectsRect(er))
{
newBomb(plane->getPositionX(), plane->getPositionY());
nowEnemy->removeFromParent();
allEnemy.eraseObject(nowEnemy);
i--;
SimpleAudioEngine::getInstance()->playEffect("explo.wav");
this->pause();
auto endSprite = Sprite::create("end.png");
endSprite->setPosition(Point::ZERO);
endSprite->setAnchorPoint(Point::ZERO);
this->addChild(endSprite);
//static CallFunc * create(const std::function<void()>& func);
auto act = Sequence::create(DelayTime::create(3.0),CallFunc::create(this,callfunc_selector(GameScene::jumpMenu)) ,NULL);
this->runAction(act);
}
for (int i=0; i<allBullets.size(); i++) {
auto nowBullet = allBullets.at(i);
Rect br(nowBullet->getPositionX(),nowBullet->getPositionY(),20,20);
if(er.intersectsRect(br))
{
newBomb(nowBullet->getPositionX(), nowBullet->getPositionY());
//---------------得分
Label* labelScore = (Label*)this->getChildByTag(99);
score += nowEnemy->getTag();
//char str[50];
//sprintf(str,"Score:%d",score);
labelScore->setString(String::createWithFormat("score:%d",score)->_string);
nowBullet->removeFromParent();
allBullets.eraseObject(nowBullet);
nowEnemy->removeFromParent();
allEnemy.eraseObject(nowEnemy);
i--;
SimpleAudioEngine::getInstance()->playEffect("explo.wav");
}
}
}
}
void GameScene::newBomb(int x, int y)
{
Vector<SpriteFrame*> allFrame;
for (int i=0; i<7; i++) {
SpriteFrame* sf = SpriteFrame::create("boom.png", Rect(i*44,0,44,47));
allFrame.pushBack(sf);
}
Animation* ani = Animation::createWithSpriteFrames(allFrame,0.03);
auto sprite = Sprite::create();
Action* act = Sequence::create(
Animate::create(ani),
CCCallFuncN::create(sprite,callfuncN_selector(GameScene::killMe)),
NULL);
this->addChild(sprite);
sprite->setPosition(Point(x,y));
sprite->runAction(act);
}
void GameScene::killMe(Node *pSender)
{
pSender->removeFromParentAndCleanup(true);
}
void GameScene::jumpMenu()
{
//SimpleAudioEngine::getInstance()->stopBackgroundMusic();
Director::getInstance()->replaceScene(HelloWorld::createScene());
}
#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
using namespace CocosDenshion;
USING_NS_CC;
Scene* GameScene::createScene()
{
auto gameScene = Scene::create();
auto gameLayer = GameScene::create();
gameScene->addChild(gameLayer);
return gameScene;
}
bool GameScene::init()
{
if(!Layer::init())
{
return false;
}
//背景1
auto bg = Sprite::create("background4.png");
bg->setPosition(Point::ZERO);
bg->setAnchorPoint(Point::ZERO);
bg->setTag(10);
this->addChild(bg);
//背景2
auto bg2 = Sprite::create("background4.png");
bg2->setPosition(Point::ZERO);
bg2->setAnchorPoint(Point::ZERO);
bg2->setPositionY(bg->getPositionY() + bg->getContentSize().height);
bg2->setTag(11);
this->addChild(bg2);
//创建分数
Label* scoreLabel = Label::create("Score:0","",20);
scoreLabel->setTag(99);
scoreLabel->setPosition(60, 450);
this->addChild(scoreLabel,2);
//移动背景
this->schedule(schedule_selector(GameScene::moveBackground),0.01);
//创建飞机
auto plane = Sprite::create();
plane->setPosition(Point(160,240));
plane->setTag(110);
this->addChild(plane);
Vector<SpriteFrame*> allFrame;
for(int i=0;i<4;i++)
{//设置飞机动画
SpriteFrame* sf = SpriteFrame::create("player.png",Rect(i*47,0,47,56));
allFrame.pushBack(sf);
}
Animation* ani = Animation::createWithSpriteFrames(allFrame,0.3);
plane->runAction(RepeatForever::create(Animate::create(ani)));
//设置飞机触摸事件
EventListenerTouchOneByOne* touchMove = EventListenerTouchOneByOne::create();
touchMove->setSwallowTouches(true);
touchMove->onTouchBegan = CC_CALLBACK_2(GameScene::onTouchBegan,this);
touchMove->onTouchMoved = CC_CALLBACK_2(GameScene::onTouchMoved,this);
touchMove->onTouchEnded = CC_CALLBACK_2(GameScene::onTouchEnded,this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(touchMove,this);
//初始化子弹
this->schedule(schedule_selector(GameScene::newBullet),0.3);
this->schedule(schedule_selector(GameScene::moveBullet),0.01);
//初始化敌机
this->schedule(schedule_selector(GameScene::newEnemy),0.4);
this->schedule(schedule_selector(GameScene::moveEnemy),0.01);
//Collision
this->scheduleUpdate();
return true;
}
bool GameScene::onTouchBegan(Touch *pTouch, Event *pEvent)
{
px = pTouch->getLocation().x;
py = pTouch->getLocation().y;
return true;
}
void GameScene::onTouchMoved(Touch *pTouch, Event *pEvent)
{
auto plane = this->getChildByTag(110);
auto move = MoveTo::create(0.1, plane->getPosition() + pTouch->getDelta());
plane->runAction(move);
// int mx = abs(pTouch->getLocation().x-px);
// int my = abs(pTouch->getLocation().y-py);
// auto plane = this->getChildByTag(110);
// plane->runAction(MoveBy::create(0,Point(mx,my)));
// px = pTouch->getLocation().x;
// py = pTouch->getLocation().y;
// plane->setPosition(Point(px,py));
}
void GameScene::onTouchEnded(Touch *pTouch, Event *pEvent)
{
}
void GameScene::moveBackground(float td)
{
auto bg1 = this->getChildByTag(10);
auto bg2 = this->getChildByTag(11);
bg1->setPositionY(bg1->getPositionY()-1);
if(bg1->getPositionY()< -bg1->getContentSize().height)
{
bg1->setPositionY(0);
}
bg2->setPositionY(bg1->getPositionY() + 680);
}
void GameScene::newBullet(float td)
{
auto plane = this->getChildByTag(110);
Sprite* bullet = Sprite::create("bullet3.png");
bullet->setPosition(plane->getPosition());
this->addChild(bullet);
this->allBullets.pushBack(bullet);
}
void GameScene::moveBullet(float td)
{
for(int i=0;i<allBullets.size();i++)
{
auto nowBullet = allBullets.at(i);
nowBullet->setPositionY(nowBullet->getPositionY()+3);
if(nowBullet->getPositionY() > Director::getInstance()->getWinSize().height)
{//删除子弹
nowBullet->removeFromParent();
allBullets.eraseObject(nowBullet);
i--;
}
}
}
void GameScene::newEnemy(float td)
{
Sprite* enemy = nullptr;
int num = rand()%10;
if(num>=3)
{
enemy = Sprite::create("aaa.png");
}else
{
enemy = Sprite::create("ccc.png");
}
enemy->setPosition(Point((rand()%300 + 10),500));
enemy->setTag(120);
this->addChild(enemy);
this->allEnemy.pushBack(enemy);
}
void GameScene::moveEnemy(float td)
{
for(int i=0;i<allEnemy.size();i++)
{
auto nowenemy = allEnemy.at(i);
nowenemy->setPositionY(nowenemy->getPositionY()-3);
if(nowenemy->getPositionY() < 0)
{//删除敌机
nowenemy->removeFromParent();
allEnemy.eraseObject(nowenemy);
i--;
}
}
}
//Collision
void GameScene::update(float td)
{
auto plane = this->getChildByTag(110);
Rect rp(plane->getPositionX(),plane->getPositionY(),45,54);
for (int i=0; i<allEnemy.size(); i++)
{
auto nowEnemy = allEnemy.at(i);
Rect er(nowEnemy->getPositionX(),nowEnemy->getPositionY(),35,45);
if (rp.intersectsRect(er))
{
newBomb(plane->getPositionX(), plane->getPositionY());
nowEnemy->removeFromParent();
allEnemy.eraseObject(nowEnemy);
i--;
SimpleAudioEngine::getInstance()->playEffect("explo.wav");
this->pause();
auto endSprite = Sprite::create("end.png");
endSprite->setPosition(Point::ZERO);
endSprite->setAnchorPoint(Point::ZERO);
this->addChild(endSprite);
//static CallFunc * create(const std::function<void()>& func);
auto act = Sequence::create(DelayTime::create(3.0),CallFunc::create(this,callfunc_selector(GameScene::jumpMenu)) ,NULL);
this->runAction(act);
}
for (int i=0; i<allBullets.size(); i++) {
auto nowBullet = allBullets.at(i);
Rect br(nowBullet->getPositionX(),nowBullet->getPositionY(),20,20);
if(er.intersectsRect(br))
{
newBomb(nowBullet->getPositionX(), nowBullet->getPositionY());
//---------------得分
Label* labelScore = (Label*)this->getChildByTag(99);
score += nowEnemy->getTag();
//char str[50];
//sprintf(str,"Score:%d",score);
labelScore->setString(String::createWithFormat("score:%d",score)->_string);
nowBullet->removeFromParent();
allBullets.eraseObject(nowBullet);
nowEnemy->removeFromParent();
allEnemy.eraseObject(nowEnemy);
i--;
SimpleAudioEngine::getInstance()->playEffect("explo.wav");
}
}
}
}
void GameScene::newBomb(int x, int y)
{
Vector<SpriteFrame*> allFrame;
for (int i=0; i<7; i++) {
SpriteFrame* sf = SpriteFrame::create("boom.png", Rect(i*44,0,44,47));
allFrame.pushBack(sf);
}
Animation* ani = Animation::createWithSpriteFrames(allFrame,0.03);
auto sprite = Sprite::create();
Action* act = Sequence::create(
Animate::create(ani),
CCCallFuncN::create(sprite,callfuncN_selector(GameScene::killMe)),
NULL);
this->addChild(sprite);
sprite->setPosition(Point(x,y));
sprite->runAction(act);
}
void GameScene::killMe(Node *pSender)
{
pSender->removeFromParentAndCleanup(true);
}
void GameScene::jumpMenu()
{
//SimpleAudioEngine::getInstance()->stopBackgroundMusic();
Director::getInstance()->replaceScene(HelloWorld::createScene());
}