博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【雷电】源代码分析(二)-- 进入游戏攻击
阅读量:5243 次
发布时间:2019-06-14

本文共 5144 字,大约阅读时间需要 17 分钟。

效果图:

程序分析:

初始化GameLayer场景触摸。背景、音乐、UI及定时间器bool GameLayer::init(){    if (!CCLayer::init()) {        return false;    }    // 开启触摸    this->setTouchEnabled(true);        // 创建数组,须要retain一下    play_bullet = CCArray::create();    play_bullet->retain();        enemy_bullet = CCArray::create();    enemy_bullet->retain();        enemy_items = CCArray::create();    enemy_items->retain();        m_state = statePlaying;//游戏開始,状态为0        Enemy::sharedEnemy();//缓存敌军飞船    Effect::sharedExplosion();//爆炸动画缓存        Config::sharedConfig()->resetConfig();//初始化分数为0、命数3条        winSize = CCDirector::sharedDirector()->getWinSize();    m_levelManager = new LevelManager(this);        //初始化背景    initBackground();    m_screenRec = CCRectMake(0, 0,  winSize.width, winSize.height + 10);        // 创建score    m_lbScore = CCLabelBMFont::create("Score:0", s_arial14_fnt);    m_lbScore->setAnchorPoint(ccp(1, 0));    m_lbScore->setAlignment(kCCTextAlignmentRight);//右对齐    addChild(m_lbScore, 1000);    m_lbScore->setPosition(winSize.width - 5, winSize.height - 30);        // ship life    CCTexture2D *shipTexture = CCTextureCache::sharedTextureCache()->addImage(s_ship01);    CCSprite *life = CCSprite::createWithTexture(shipTexture, CCRectMake(0, 0, 60, 38));    life->setScale(0.6);    life->setPosition(ccp(30,winSize.height-23));    addChild(life, 1, 5);        // 显示生命条数    char lifecount[2];    sprintf(lifecount, "%d",Config::sharedConfig()->getLifeCount());    m_lifeCount = CCLabelTTF::create(lifecount, "Arial", 20);    m_lifeCount->setPosition(ccp(60, winSize.height-20));    m_lifeCount->setColor(ccc3(255,0, 0));    addChild(m_lifeCount, 1000);        // 创建飞船    m_ship = Ship::create();    addChild(m_ship, m_ship->getZoder(), 1001);    	//游戏暂停button    CCMenuItemImage *pause = CCMenuItemImage::create("pause.png", "pause.png", this, menu_selector(GameLayer::doPause));    pause->setAnchorPoint(ccp(1, 0));    pause->setPosition(ccp(winSize.width, 0));    CCMenu *menu = CCMenu::create(pause, NULL);    menu->setAnchorPoint(ccp(0, 0));    addChild(menu, 1, 10);    menu->setPosition(CCPointZero);        // 调 update函数    scheduleUpdate();        // 每秒调一次 scoreCounter函数    schedule(schedule_selector(GameLayer::scoreCounter), 1);        if (Config::sharedConfig()->getAudioState()) {//背景音乐        SimpleAudioEngine::sharedEngine()->playBackgroundMusic(s_bgMusic, true);    }        return true;}

 

//主角出场, (主角)飞船创建、发射子弹、复活动画bool Ship::init(){    // super init first    if ( !CCSprite::init() )    {        return false;    }            // 初始化飞船    CCTexture2D * shipTextureCache = CCTextureCache::sharedTextureCache()->addImage(s_ship01);    CCRect rec = CCRectMake(0, 0, 60, 38);    this->initWithTexture(shipTextureCache,  rec);    this->setPosition(m_appearPosition);        //创建飞船动画    CCSpriteFrame *frame0 = CCSpriteFrame::createWithTexture(shipTextureCache, CCRectMake(0, 0, 60, 38));    CCSpriteFrame *frame1 = CCSpriteFrame::createWithTexture(shipTextureCache, CCRectMake(60, 0, 60, 38));    CCArray *animFrames = CCArray::create();    animFrames->addObject(frame0);    animFrames->addObject(frame1);        CCAnimation *animation = CCAnimation::createWithSpriteFrames(animFrames, 0.1);    CCAnimate *animate = CCAnimate::create(animation);    this->runAction(CCRepeatForever::create(animate));        // 子弹发射    this->schedule(schedule_selector(Ship::shoot), 0.16);        // 复活动画,由大变小    this->m_canBeAttack = false;    CCSprite *ghostSprite = CCSprite::createWithTexture(shipTextureCache, CCRectMake(0, 45, 60, 38));    ccBlendFunc cbl = {GL_SRC_ALPHA, GL_ONE};    ghostSprite->setBlendFunc(cbl);    ghostSprite->setScale(8);    ghostSprite->setPosition(ccp(this->getContentSize().width / 2, 12));    this->addChild(ghostSprite, 3000, 99999);    ghostSprite->runAction(CCScaleTo::create(0.5, 1, 1));        // 复活飞船须要闪烁出场    CCBlink *blinks = CCBlink::create(3, 9);        CCCallFuncN *makeBeAttack = CCCallFuncN::create(this, callfuncN_selector(Ship::makeAttack));        this->runAction(CCSequence::create(CCDelayTime::create(0.5), blinks, makeBeAttack, NULL));    return true;}

飞船的左右子弹发射

//飞船左右攻击子弹 0.16'一次 void Ship::shoot(float dt){//右边攻击子弹    int offset = 13;//子弹相对飞船的偏移量    CCPoint position = this->getPosition();//飞船坐标    CCSize contentSize = this->getContentSize();    Bullet *bullet_a = new Bullet(m_bulletSpeed, "W1.png", 1);//子弹构造    if (bullet_a) {        bullet_a->autorelease();        play_bullet->addObject(bullet_a);        this->getParent()->addChild(bullet_a, bullet_a->m_zorder, 901);        bullet_a->setPosition(ccp(position.x + offset, position.y + 3 + contentSize.height * 0.3));//子弹的由近至远, x轴不变, 尾随飞船移动    }else{        delete bullet_a;        bullet_a = 0;    }    //左边攻击子弹    Bullet *bullet_b = new Bullet(m_bulletSpeed, "W1.png", 1);    if (bullet_b) {        bullet_b->autorelease();        play_bullet->addObject(bullet_b);        this->getParent()->addChild(bullet_b, bullet_b->m_zorder, 901);        bullet_b->setPosition(ccp(position.x - offset, position.y + 3 + contentSize.height * 0.3));    }else{        delete bullet_b;        bullet_a = 0;    }}

 

转载于:https://www.cnblogs.com/lxjshuju/p/7077558.html

你可能感兴趣的文章
Python 数据类型
查看>>
Google Guava学习笔记——简介
查看>>
历时八年,HTML5 标准终于完工了
查看>>
17.树的子结构
查看>>
D - Mike and strings
查看>>
C++:多维数组的动态分配(new)和释放(delete)
查看>>
c#基础学习(0806)之抽象类实现多态
查看>>
S5PV210根文件系统的制作(一)
查看>>
51NOD 1244 莫比乌斯函数之和
查看>>
[bzoj1923]外星千足虫[高斯消元]
查看>>
centos下同时启动多个tomcat
查看>>
slab分配器
查看>>
分析 PHP大马-php_mof SHELL
查看>>
TCP/IP
查看>>
[推荐] 协同滤波 —— Collaborative Filtering (CF)
查看>>
python中使用中文
查看>>
数据清洗
查看>>
Android 动态加载 (二) 态加载机制 案例二
查看>>
MVC5 + EF6 + Bootstrap3 (10) 数据查询页面
查看>>
Windows下的Eclipse启动出现:a java runtime environment(JRE) or java development kit(JDK) must be.......
查看>>