/*渲染*/ void GameStage::render() { //人物移动和设置手臂和身体的角度 this->hand->setRotation(this->pukaManCore->roleVo->handRotation); this->role->setRotation(this->pukaManCore->roleVo->bodyRotation); this->role->setPosition(ccp(this->pukaManCore->roleVo->x, this->pukaManCore->roleVo->y)); //运动背景移动 CCDrawNode* drawNode; float dis; int length = this->drawBgList->count(); for (int i = 0; i < length; i+=1) { drawNode = static_cast<CCDrawNode *>(this->drawBgList->objectAtIndex(i)); if (drawNode->getPositionX() < -drawNode->getContentSize().width) { //计算移到出边界后的误差,并取绝对值。 重新设置背景的位置并减轻误差。 dis = (float) abs(drawNode->getPositionX()) - drawNode->getContentSize().width; drawNode->setPositionX(drawNode->getContentSize().width - dis); } drawNode->setPositionX(drawNode->getPositionX() - this->pukaManCore->vx *.5); } //前景 length = this->frontBgList->count(); CCDrawNode* lastDrawNode; CCDrawNode* prevDrawNode; for (int i = length - 1; i >= 0; i-=1) { drawNode = static_cast<CCDrawNode *>(this->frontBgList->objectAtIndex(i)); if (drawNode->getPositionX() <-drawNode->getContentSize().width - this->stageWidth) { //计算移到出边界后的误差,并取绝对值。 重新设置背景的位置并减轻误差。 dis = (float) abs(drawNode->getPositionX()) - (drawNode->getContentSize().width + this->stageWidth); lastDrawNode = static_cast<CCDrawNode *>(this->frontBgList->objectAtIndex(length - 1)); drawNode->setPositionX(lastDrawNode->getPositionX() + this->stageWidth - dis); this->frontBgList->removeObjectAtIndex(i); this->frontBgList->addObject(drawNode); } if(i > 0) { prevDrawNode = static_cast<CCDrawNode *>(this->frontBgList->objectAtIndex(i - 1)); drawNode->setPositionX(prevDrawNode->getPositionX() + this->stageWidth); } float scale = this->pukaManCore->bgWidth / this->stageWidth; drawNode->setContentSize(CCSizeMake(this->pukaManCore->bgWidth, drawNode->getContentSize().height)); drawNode->setScaleX(scale); drawNode->setPositionX(drawNode->getPositionX() - this->pukaManCore->vx); } }
/*创建运动背景*/ CCDrawNode* GameStage::createMotionWall(float x, float y, float bgWidth, float bgHeight, float gapH, ccColor4F color) { int num = (int)(this->stageWidth / (bgWidth + gapH)); CCDrawNode* drawNode = CCDrawNode::create(); for (int i = 0; i < num; i+=1) { int startX = i * (bgWidth + gapH) + x; CCPoint points[] = {CCPoint(startX, y), CCPoint(startX, bgHeight + y), CCPoint(startX + bgWidth, bgHeight + y), CCPoint(startX + bgWidth, y)}; //顶点列表,顶点数量,颜色 drawNode->drawPolygon(points, 4, color, 0, ColorUtil::getColor4F(0, 0, 0, 0)); } //如果不设置宽高则 始终为0,即使内部有复杂的绘图。 drawNode->setContentSize(CCSizeMake(num * (bgWidth + gapH), bgHeight)); return drawNode; }
/*创建运动前景*/ CCDrawNode* GameStage::createFrontMotionWall(float x, float y, float bgWidth, float bgHeight, int tag) { //前景列表 CCDrawNode* drawNode = CCDrawNode::create(); ccColor4F color = ColorUtil::getColor4F(141, 143, 129, 255); CCPoint points[] = {CCPoint(x, y), CCPoint(x, bgHeight + y), CCPoint(x + bgWidth, bgHeight + y), CCPoint(x + bgWidth, y)}; //顶点列表,顶点数量,颜色 drawNode->drawPolygon(points, 4, color, 0, ColorUtil::getColor4F(0, 0, 0, 0)); //前景中段 float my = y + 97; float mHeight = 93; color = ColorUtil::getColor4F(103, 107, 82, 255); CCPoint points2[] = {CCPoint(x, my), CCPoint(x, mHeight + my), CCPoint(x + bgWidth, mHeight + my), CCPoint(x + bgWidth, my)}; drawNode->drawPolygon(points2, 4, color, 0, ColorUtil::getColor4F(0, 0, 0, 0)); mHeight = 3; my = my - mHeight; color = ColorUtil::getColor4F(86, 90, 67, 255); CCPoint points3[] = {CCPoint(x, my), CCPoint(x, mHeight + my), CCPoint(x + bgWidth, mHeight + my), CCPoint(x + bgWidth, my)}; drawNode->drawPolygon(points3, 4, color, 0, ColorUtil::getColor4F(0, 0, 0, 0)); mHeight = 2; my = my - mHeight; color = ColorUtil::getColor4F(100, 103, 82, 255); CCPoint points4[] = {CCPoint(x, my), CCPoint(x, mHeight + my), CCPoint(x + bgWidth, mHeight + my), CCPoint(x + bgWidth, my)}; drawNode->drawPolygon(points4, 4, color, 0, ColorUtil::getColor4F(0, 0, 0, 0)); mHeight = 1; my = my - mHeight; color = ColorUtil::getColor4F(113, 116, 99, 255); CCPoint points5[] = {CCPoint(x, my), CCPoint(x, mHeight + my), CCPoint(x + bgWidth, mHeight + my), CCPoint(x + bgWidth, my)}; drawNode->drawPolygon(points5, 4, color, 0, ColorUtil::getColor4F(0, 0, 0, 0)); drawNode->setAnchorPoint(ccp(0, 0)); drawNode->setContentSize(CCSizeMake(bgWidth, bgHeight)); //创建阴影 设置注册点在top和center CCSprite* shadow = CCSprite::create("shadow.png"); shadow->setTag(tag); shadow->setAnchorPoint(ccp(.5, 1)); shadow->setPosition(ccp(shadow->getContentSize().width *.5, y)); drawNode->setTag(tag); drawNode->addChild(shadow); return drawNode; }