コード例 #1
0
ファイル: GameScene.cpp プロジェクト: 2011conquer/MikuRun2
void GameScene::initFloorsAndCoins()
{
    int num = 0;//用来作为数组下标
    int count = 0;//用来控制山体的X坐标
    
    isFrontBlank = false;
    
    //创建20个山体和金币,放入数组中
    for (int i=0; i<floorCount; i++) {
        Floor *floor = Floor::createFloor();
        floor->setScale(scaleFix);
        this->addChild(floor);
        floorArray.pushBack(floor);
        
       Coin *coin = Coin::createCoin();
       this->addChild(coin);
       coinArray.pushBack(coin);
        
    }
    //循环遍历修改山体和金币的位置
    while (num<floorCount) {
        count++;
        if (num<floorCount/2) {
            //前10个山体和金币单独处理,count用来控制位置,根据player的初始化位置设置地面的位置
            floorArray.at(num)->setPosition(Point(count*35,winSize.height/2 -100));
            
            Floor *floortemp = floorArray.at(num);
            //金币放在山体上方一定距离
            coinArray.at(num)->setPosition(Point(count*35,floortemp->getPositionY()+80));
            num++;
        }else{
            //后10个山体和金币的处理
            randomNum = this->getRandomNumber(0, 2); //0~2的随机数
            if (randomNum == 0) {
                isFrontBlank = true;
                continue;
            }
            if (isFrontBlank) {
                //山体高度设置为100到320-220即100之间的随机数
                floorArray.at(num)->setPosition(Point(count*35, this->getRandomNumber(winSize.height/2 -100, 180)));
                //金币高度设置为山体以上30到100的高度
                float y = floorArray.at(num)->getPositionY() + this->getRandomNumber(50, 150);
                coinArray.at(num)->setPosition(Point(count*35, y));
                
                isFrontBlank = false;
            }else{
                //设置山体的高度为前一个山体的高度
                floorArray.at(num)->setPosition(Point(count*35, floorArray.at(num -1)->getPositionY()));
                
                //金币的高度为山体以上30到100的高度
                float y = floorArray.at(num)->getPositionY() + this->getRandomNumber(50, 150);
                coinArray.at(num)->setPosition(Point(count*35, y));
            }
            num++;
        }
    }
}
コード例 #2
0
ファイル: GameScene.cpp プロジェクト: 2011conquer/MikuRun2
void GameScene::updateFloorsAndCoins()
{
    //改变山体的位置
    for (int i =0; i<floorArray.size(); i++) {
        Floor *floor = floorArray.at(i);
        //forceX=300,speedFix = 0.01
        floor->setPosition(Point(floor->getPositionX()-forceX * speedFix, floor->getPositionY()));
    }
    //改变金币的位置
    for (int i = 0; i<coinArray.size(); i++) {
        Coin *coin = coinArray.at(i);
        coin->setPosition(Point(coin->getPositionX()-forceX * speedFix, coin->getPositionY()));
    }
    
    //当第一个山体移出屏幕
    if (floorArray.at(0)->getPositionX() <-50) {
        int num = floorArray.size()-1;//数组中最后一个元素下标
        float posx = floorArray.at(num)->getPositionX();//获取最后一个山体的X坐标
        
        //将第一个山体再加入数组中,这样它就成为最后一个元素
        floorArray.pushBack(floorArray.at(0));
        
        //删除数组中第一个元素,这样就完成了第一个山体移动到数组中最后的位置
        floorArray.eraseObject(floorArray.at(0));
        
        coinArray.pushBack(coinArray.at(0));
        coinArray.eraseObject(coinArray.at(0));
        
        //获取0~2的随机数
        randomNum = this->getRandomNumber(0, 2);
        if (randomNum == 0) {
            isFrontBlank = true;
            randomNum = this->getRandomNumber(1, 4);
        }
        
        Coin *coin = coinArray.at(num);
        //数组中最后一个元素,即原来的第一个元素移动到最后位置,再获取最后一个
       
        coin->setVisible(true);
        if (isFrontBlank) {
            //posx为数组中倒数第二个元素的X坐标,高度为随机数100~150之间
            //设置新的最后一个元素的位置
            floorArray.at(num)->setPosition(Point(posx + randomNum*35 + 35, this->getRandomNumber(winSize.height/2 -100,170)));
            coinArray.at(num)->setPosition(Point(posx + randomNum*35 + 35, floorArray.at(num)->getPositionY() + this->getRandomNumber(50, 150)));
            isFrontBlank = false;
        }else{
            //X轴坐标后移,高度同前一个山体
            floorArray.at(num)->setPosition(Point(posx + 35 , floorArray.at(num - 1)->getPositionY()));
            coinArray.at(num)->setPosition(Point(posx +  35, floorArray.at(num)->getPositionY() + this->getRandomNumber(50, 150)));
        }
        randomNum = this ->getRandomNumber(0, 2);
        if (randomNum == 0) {
            coin->setVisible(false);
        }
    }
}