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); } } }
void Level1State::populateCoins() { int locations[8][2] = { {1068 ,140}, {1092 ,140}, {1116 ,140}, {1128 ,140}, {1140 ,140}, {1164, 140}, {1188, 140}, {1200, 140} }; for (int i=0; i<8; i++) { Coin *c = new Coin(tileMap, renderTarget, 10); c->setPosition(locations[i][0], locations[i][1]); coins.push_back(c); } }
// on "init" you need to initialize your instance bool Interface::init() { ////////////////////////////// // 1. super init first if ( !CCLayer::init() ) { return false; } CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); ///////////////////////////// // 3. add your codes below... // add a label shows "Hello World" // create and initialize a label CCLabelTTF* pLabel = CCLabelTTF::create("0", "Arial", TITLE_FONT_SIZE); pLabel->setAnchorPoint(ccp(0.0f, 0.5f)); // position the label on the center of the screen pLabel->setPosition(ccp(origin.x + visibleSize.width/2, origin.y + visibleSize.height - pLabel->getContentSize().height)); pLabel->setTag(10); // add the label as a child to this layer this->addChild(pLabel, 1); Coin* coin = Coin::create(); coin->setAnchorPoint(ccp(0.5f, 0.5f)); coin->setScale(2.0f); coin->setPosition(ccp((origin.x + visibleSize.width/2) - coin->getContentSize().width, origin.y + visibleSize.height - pLabel->getContentSize().height)); // add the sprite as a child to this layer this->addChild(coin, 2); CCLabelTTF* parachuteLabel = CCLabelTTF::create("10 sec", "Arial", TITLE_FONT_SIZE); parachuteLabel->setAnchorPoint(ccp(0.0f, 0.5f)); #ifdef CC_TARGET_OS_MAC // position the label on the center of the screen parachuteLabel->setPosition(ccp(50.0f, origin.y + visibleSize.height - pLabel->getContentSize().height)); #endif #ifdef OUYA_BUILD // position the label on the center of the screen parachuteLabel->setPosition(ccp(60.0f, origin.y + visibleSize.height - pLabel->getContentSize().height)); #endif parachuteLabel->setTag(20); // add the label as a child to this layer this->addChild(parachuteLabel, 1); Parachute* parachute = Parachute::create(); parachute->setAnchorPoint(ccp(0.5f, 0.5f)); parachute->setScale(1.0f); #ifdef CC_TARGET_OS_MAC parachute->setPosition(ccp((50.0f) - parachute->getContentSize().width / 2.0f, origin.y + visibleSize.height - pLabel->getContentSize().height)); #endif #ifdef OUYA_BUILD parachute->setPosition(ccp((60.0f) - parachute->getContentSize().width / 2.0f, origin.y + visibleSize.height - pLabel->getContentSize().height)); #endif // add the sprite as a child to this layer this->addChild(parachute, 3); return true; }