对自己之前做过项目所做的一些笔记

来源:互联网 发布:淘宝网广场舞服装套装 编辑:程序博客网 时间:2024/04/30 08:43

=========================战神传说================

1.开始界面的飞机移动:StartMenu

//设置飞船位置 移动

CCPoint position = ccp(CCRANDOM_0_1() * winSize.width, 0);

    m_ship->setPosition(position);    m_ship->runAction(CCMoveBy::create(2, ccp(CCRANDOM_0_1() * winSize.width, position.y + winSize.height + 100)));
<span style="font-family: Arial, Helvetica, sans-serif;">//添加update回调函数</span>
    this->schedule(schedule_selector(StartMenu::update), 0.1);
<span style="font-family: Arial, Helvetica, sans-serif;">//飞船在初始界面移动函数</span>
void StartMenu::update(float dt){    if (m_ship->getPosition().y > winSize.height) {        CCPoint pos = ccp(CCRANDOM_0_1() * winSize.width, 10);        m_ship->setPosition(pos);        m_ship->runAction(CCMoveBy::create(floor(5 * CCRANDOM_0_1()), ccp(CCRANDOM_0_1() * winSize.width, pos.y + winSize.height)));            }}

float m_backSkyHeight;

float m_backTileMapHeight;

// 无限滚动地图,采用两张图循环加载滚动

void GameLayer::initBackground(){    m_backSky = CCSprite::create(s_bg01);    m_backSky->setAnchorPoint(ccp(0, 0));    m_backSkyHeight = m_backSky->getContentSize().height;    addChild(m_backSky, -10);        // Tile map    m_backTileMap = CCTMXTiledMap::create(s_level01);    addChild(m_backTileMap, -9);    m_backTileMapHeight = m_backTileMap->getMapSize().height * m_backTileMap->getTileSize().height;        m_backSkyHeight -= 48;    m_backTileMapHeight -= 200;    m_backSky->runAction(CCMoveBy::create(3, ccp(0, -48)));    m_backTileMap->runAction(CCMoveBy::create(3, ccp(0, -200)));        schedule(schedule_selector(GameLayer:: movingBackground),3);    }// 这里就是视差背景了void GameLayer::movingBackground(float dt){    m_backSky->runAction(CCMoveBy::create(3, ccp(0, -48)));    m_backTileMap->runAction(CCMoveBy::create(3, ccp(0, -200)));        // 每次移动48    m_backSkyHeight -= 48;        // 每次移动200    m_backTileMapHeight -= 200;        // 图的顶部到达屏幕顶部时    if (m_backSkyHeight <= winSize.height) {        if (!m_isBackSkyReload) {                        // 如果另一张图还没加载则create一个            m_backSkyRe = CCSprite::create(s_bg01);            m_backSkyRe->setAnchorPoint(ccp(0, 0));            addChild(m_backSkyRe, -10);            m_backSkyRe->setPosition(ccp(0, winSize.height));                        // 反转标志位            m_isBackSkyReload = true;        }        // 第二张图紧接着第一张图滚动        m_backSkyRe->runAction(CCMoveBy::create(3, ccp(0, -48)));    }        // 第一张图完全经过屏幕    if (m_backSkyHeight <= 0) {        m_backSkyHeight = m_backSky->getContentSize().height;        // 移除第一张的精灵        this->removeChild(m_backSky, true);                // 指向第二张图的精灵        m_backSky = m_backSkyRe;                // 第二张的精灵指针置空        m_backSkyRe = NULL;                // 反转标志位        m_isBackSkyReload = false;    }        if (m_backTileMapHeight <= winSize.height) {        if (!m_isBackTileReload) {            m_backTileMapRe = CCTMXTiledMap::create(s_level01);            this->addChild(m_backTileMapRe, -9);            m_backTileMapRe->setPosition(0, winSize.height);            m_isBackTileReload = true;        }        m_backTileMapRe->runAction(CCMoveBy::create(3, ccp(0, -200)));    }        if (m_backTileMapHeight <= 0) {        m_backTileMapHeight = m_backTileMap->getMapSize().height * m_backTileMap->getTileSize().height;        this->removeChild(m_backTileMap, true);        m_backTileMap = m_backTileMapRe;        m_backTileMapRe = NULL;        m_isBackTileReload = false;    }}

3.返回按钮的闪烁动画:

CCEaseSineInOut *ease1 = CCEaseSineInOut::create(CCFadeTo::create(1, 80));

    CCEaseSineInOut *ease2 = CCEaseSineInOut::create(CCFadeTo::create(1, 255));    CCFiniteTimeAction *seq = CCSequence::create(ease1, ease2, NULL);    // 参数必须要强转,不然ndk编译不过    goBack->runAction(CCRepeatForever::create((CCActionInterval*)seq));</span>

=========================大鱼吃小鱼====================

触摸:

touchListener = EventListenerTouchOneByOne::create();    touchListener->setSwallowTouches( true);    touchListener->onTouchBegan = CC_CALLBACK_2 (GameScene::onTouchBegan, this);    touchListener->onTouchMoved = CC_CALLBACK_2 (GameScene::onTouchMoved, this);    touchListener->onTouchEnded = CC_CALLBACK_2 (GameScene::onTouchEnded, this);    _eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
bool GameScene::onTouchBegan( Touch *touch, Event *event ){    auto target = static_cast< Sprite *>(event->getCurrentTarget());    touchLocation = target->convertToNodeSpace(touch ->getLocation());    return true;}void GameScene::onTouchMoved( Touch *touch, Event *event ){    Point convertedLocation = touch->getLocation();        Point fishLocation = mainFish->getPosition();    fishLocation.x+=convertedLocation.x-touchLocation.x;    fishLocation.y+=convertedLocation.y-touchLocation.y;    Rect rect=Rect(0,0,winSize.width,winSize.height);    if(rect.containsPoint(fishLocation))    {        if((convertedLocation.x-touchLocation.x)<0)            mainFish->setFlipX(true);        else            mainFish->setFlipX(false);        mainFish->setPosition(fishLocation);    }    touchLocation=convertedLocation;}

===============郑州boy魔法阵游戏=============

1.载入界面

在Init中():

this->setUpdateView();

CCTextureCache::sharedTextureCache()->addImageAsync("gmbg/welcomebg.png",this,callfuncO_selector(LoadingLayer::loadCallBack));

调用方法:

bool LoadingLayer::setUpdateView(){

bool isRet=false;do {// 设置进度条的背景图片 我们把他放到屏幕下方的1/5处    CCSprite* loadbackimg=CCSprite::create("gmbg/lodingbg.png");CC_BREAK_IF(!loadbackimg);loadbackimg->setPosition(ccp(getWinSize().width/2+getWinOrigin().x,getWinSize().height/5+getWinOrigin().y));this->addChild(loadbackimg,1);// 添加进度条CCSprite* loadimg=CCSprite::create("gmbg/longding.png");CC_BREAK_IF(!loadimg);CCProgressTimer* pt = CCProgressTimer::create(loadimg);pt->setType(kCCProgressTimerTypeBar);// 设置成横向的//可以看作是按矩形显示效果的进度条类型pt->setMidpoint(ccp(0,0)); //  用来设定进度条横向前进的方向从左向右或是从右向左pt->setBarChangeRate(ccp(1,0));//重新设置锚点float tex=getWinSize().width/2+getWinOrigin().x;float tey=getWinSize().height/5+getWinOrigin().y-5;pt->setPosition(ccp(tex,tey));pt->setPercentage(0);this->addChild(pt,2,1);isRet=true;} while (0);return isRet;}
void LoadingLayer::loadCallBack(CCObject* ped){loadingNum++; CCLOG("%d",loadingNum);    CCProgressTimer* pt=(CCProgressTimer*)this->getChildByTag(1);float now=pt->getPercentage();pt->setPercentage(100.0/totalNum+now);if(loadingNum<totalNum){}else{pt->setPercentage(100);// 加载完的时候跳转到响应的界面CCLOG("loading over");goWelcomeLayer();}}

2.关卡提示动画:

void RatingSprite::runAnimation(){this->stopAllActions();CCActionInterval* cd= CCScaleTo::create(2,1,1);CCActionInterval* cd2=CCScaleTo::create(2,1,0);CCCallFuncN *onComplete = CCCallFuncN::create(this, callfuncN_selector(RatingSprite::runAnimationCallBack));CCSequence* ce=CCSequence::create(cd,cd2,onComplete,NULL);this->runAction(ce);}
=========================赛车游戏================

创建敌人:

Sprite *roles[10];

<span style="font-size:14px;">void GameScene::menuStartCallback(Ref* pSender){    //添加触摸事件    addTouchEvent();    isPlaying=true;    mOverLabel->setVisible(false);    pStartItem->setVisible(false);    Device::setAccelerometerEnabled(true);    for(int i=0;i<10;i++)    {        int car=rand()%4;        this->removeChild(roles[i]);        if(car==0)        {            roles[i]=CCSprite::create("car1.png");        }else if(car==1){            roles[i]=CCSprite::create("car3.png");        }else if(car==2){            roles[i]=CCSprite::create("car2.png");        }else if(car==3){            roles[i]=CCSprite::create("car1.png");        }else if(car==4){            roles[i]=CCSprite::create("car1.png");        }else{            roles[i]=CCSprite::create("car1.png");        }        roles[i]->setAnchorPoint(Vec2(0,0));        int left=rand()%390;        int high=720+rand()%15000;//精灵的位置        roles[i]->setPosition(Vec2((visibleSize.width/2-pRoad1->getContentSize().width/2)+80+left,high));        this->addChild(roles[i],0);    }    CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("car_sound.mp3");}</span>
控制精灵的移动,我添加了触摸和重力感应

bool GameScene::onTouchBegan(Touch *touch, Event *event){    LocationPos=touch->getLocationInView();    if(LocationPos.x > mainRole->getPosition().x + mainRole->getContentSize().width)    {        runRight();    }    else if(LocationPos.x<mainRole->getPosition().x)    {        runLeft();    }    this->schedule(schedule_selector(GameScene::updateSel), 0.01f);      return true;}void GameScene::onTouchEnded( Touch *touch, Event * event ){    this->unschedule(schedule_selector(GameScene::updateSel));}void GameScene::updateSel( float t ){    if(LocationPos.x > mainRole->getPosition().x + mainRole->getContentSize().width)    {        runRight();    }    else if(LocationPos.x<mainRole->getPosition().x)    {        runLeft();    }}void GameScene::runRight(){    if(mainRole->getPosition().x+3<=visibleSize.width/2+220-73)    {        mainRole->setPosition(Vec2(mainRole->getPosition().x+3,0));    }}void GameScene::runLeft(){    if(mainRole->getPosition().x-3>=visibleSize.width/2-220)    {        mainRole->setPosition(Vec2(mainRole->getPosition().x-3,0));    }}
重力感应让精灵只能左右移动,并且不能超出马路范围。

void GameScene::onAcceleration(Acceleration* acc, Event* event){    Director* pDir = Director::getInstance();    if ( mainRole == NULL ) {        return;    }    Size pSpriteSize  = mainRole->getContentSize();    Point ptNow  = mainRole->getPosition();    Point ptTemp = pDir->convertToUI(ptNow);    //移动速度,值越大,速度越大    ptTemp.x += acc->x * 20.1f;    ptTemp.y -= acc->y * 20.81f;    Point ptNext = pDir->convertToGL(ptTemp);        FIX_POS(ptNext.x, (winSize.width/2-pRoad1->getContentSize().width/2+mainRole->getContentSize().width), (winSize.width/2+pRoad1->getContentSize().width/2-mainRole->getContentSize().width));    FIX_POS(ptNext.y, 0,0);    mainRole->setPosition(ptNext);}

我总共是添加了2个刷新函数:

this->schedule(schedule_selector(GameScene::update), 0.01f);

this->schedule(schedule_selector(GameScene::updateSel), 0.01f);  

void GameScene::update(float t){    if(!isPlaying)    {        return;    }    pRoad1->setPosition(Vec2(pRoad1->getPosition().x,pRoad1->getPosition().y-mSpeed));    pRoad2->setPosition(Vec2(pRoad2->getPosition().x,pRoad2->getPosition().y-mSpeed));    Score += (mSpeed*10);    String* scoreStr = String::createWithFormat("Score:%d",Score);    mScoreLable->setString(scoreStr->getCString());        mSpeed = 5+2*(Score/5000000);    if(speedLast != mSpeed)    {        speedLast = mSpeed;        CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("hurry.mp3");    }    for(int i=0;i<10;i++)    {        roles[i]->setPosition(Vec2(roles[i]->getPosition().x,roles[i]->getPosition().y-mSpeed+2));        if(roles[i]->getPosition().y+roles[i]->getContentSize().height<0)        {            int left=rand()%390;            int high=720+rand()%8000;            roles[i]->setPosition(Vec2(80+left+visibleSize.width/2-pRoad1->getContentSize().width/2,high));            for(int j=0;j<10;j++)            {                if(abs(roles[i]->getPosition().y-roles[j]->getPosition().y)<200&&abs(roles[i]->getPosition().x-roles[j]->getPosition().x)<100)                {                    if(i!=j)                    {                        roles[i]->setPosition(Vec2(roles[i]->getPosition().x,roles[i]->getPosition().y+260));                        j=0;                    }                }            }        }    }    //道路循环切换    if(pRoad1->getPosition().y+pRoad1->getContentSize().height<0)    {        pRoad1->setPosition(Vec2(pRoad1->getPosition().x,pRoad2->getPosition().y+pRoad2->getContentSize().height));    }    if(pRoad2->getPosition().y+pRoad2->getContentSize().height<0)    {        pRoad2->setPosition(Vec2(pRoad2->getPosition().x,pRoad1->getPosition().y+pRoad1->getContentSize().height));    }    //检测碰撞    for(int j=0;j<10;j++)    {        if (mainRole->getBoundingBox().intersectsRect(roles[j]->getBoundingBox()))        {                mOverLabel->setVisible(true);                Device::setAccelerometerEnabled(false);                _eventDispatcher->removeEventListener(listener);                 this->unschedule(schedule_selector(GameScene::updateSel));                isPlaying=false;                pStartItem->setVisible(true);                CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("boom.mp3");                Score=0;        }    }}

=======胜利和失败共用一个界面

#pragma once#include "cocos2d.h"USING_NS_CC;class GameResult : public CCLayerColor{public:    GameResult(void);    ~GameResult(void);    static CCScene* sceneWithWon(bool won);    static GameResult* createGameResult(bool won);    bool initWithWon(bool won);    void gameOverDone();};====#include "GameResult.h"#include "ChooseScene.h"GameResult::GameResult(void){}GameResult::~GameResult(void){}CCScene* GameResult::sceneWithWon( bool won ){    CCScene* scene = CCScene::create();    GameResult *layer = GameResult::createGameResult(won);    scene->addChild(layer);    return scene;}GameResult* GameResult::createGameResult( bool won ){    GameResult *pRet = new GameResult();    if (pRet && pRet->initWithWon(won))    {        pRet->autorelease();        return pRet;    }    CC_SAFE_DELETE(pRet);    return NULL;}bool GameResult::initWithWon( bool won ){    bool bRet = false;    do     {        CC_BREAK_IF(! CCLayerColor::initWithColor(ccc4(0,0,0,150)));        char *message;        if (won)        {            message = "You Won!";        }         else        {            message = "You Lose :[";        }        CCSize winSize = CCDirector::sharedDirector()->getWinSize();        CCLabelTTF *label = CCLabelTTF::create(message, "Arial", 32);        label->setColor(ccWHITE);        label->setPosition(ccp(winSize.width / 2, winSize.height / 2));        this->addChild(label);        this->runAction(CCSequence::create(CCDelayTime::create(2),             CCCallFunc::create(this, callfunc_selector(GameResult::gameOverDone)),            NULL));        bRet = true;    } while (0);    return bRet;}void GameResult::gameOverDone(){    CCDirector::sharedDirector()->replaceScene(ChooseScene::scene());}用法:CCScene *gameOverScene = GameResult::sceneWithWon(false);CCDirector::sharedDirector()->replaceScene(gameOverScene);












0 0
原创粉丝点击