/** * その他UI */ void HelloWorld::addTalkOther(const std::string& str){ Size size = Director::getInstance()->getVisibleSize(); DrawNode* draw = DrawNode::create(); int originalX = OTHER_TEXT_X; int originalY = size.height - (TEXT_H * (index + 1)); int x = originalX - 10; int y = originalY - 60; int w = 300; int h = 60; Vec2 points[] = { Vec2(x, y), Vec2(x + w, y), Vec2(x + w, y + h), Vec2(x, y + h), }; this->addChild(draw); draw->drawPolygon(points, 4, Color4F(0.5, 0, 0, 1), 1, Color4F(1, 0, 0, 1)); auto text = Text::create(str, "fronts/arial.ttf", 40); text->setTextHorizontalAlignment(TextHAlignment::LEFT); text->setAnchorPoint(Point(0.0, 1.0)); text->setPosition(Point(originalX, originalY)); text->setColor(Color3B(255, 255, 0)); this->addChild(text); index++; }
bool CardBase::ccTouchBegan(Touch* touch, Event* event) { glLineWidth(16); ccDrawColor4B(255,0,0,255); Point vertices[] = { Point(0, 0), Point(50, 50), Point(100, 50), Point(100, 100) }; ccDrawPoly(vertices, 4, true); //glLineWidth(16); //ccDrawColor4B(0, 255, 0, 255); //ccDrawCircle( VisibleRect::center(), 100, 0, 10, false); Size s = getbackGround()->getContentSize(); DrawNode *draw = DrawNode::create(); addChild(draw, 10); // Draw polygons Point points[] = { Point(s.height/4,0), Point(s.width,s.height/5), Point(s.width/3*2,s.height) }; draw->drawPolygon(points, sizeof(points)/sizeof(points[0]), Color4F(1,0,0,0.5), 0, Color4F(0,0,0,0)); // Draw segment //draw->drawSegment(ccp(20,s.height), ccp(20,s.height/2), 10, ccc4f(0, 1, 0, 0.5)); //draw->drawSegment(ccp(10,s.height/2), ccp(s.width/2, s.height/2), 40, ccc4f(1, 0, 1, 0.5)); // this->addChild(shadow); // addChild(shadow); CCLog("ccTouchBegan"); return true; }
static void DrawPolygon(int count, const cpVect *verts, cpFloat r, cpSpaceDebugColor outline, cpSpaceDebugColor fill, cpDataPointer data) { const Color4F fillColor(fill.r, fill.g, fill.b, fill.a); const Color4F outlineColor(outline.r, outline.g, outline.b, outline.a); DrawNode* drawNode = static_cast<DrawNode*>(data); int num = count; Vec2* seg = new (std::nothrow) Vec2[num]; for(int i=0;i<num;++i) seg[i] = PhysicsHelper::cpv2point(verts[i]); drawNode->drawPolygon(seg, num, fillColor, 1.0f, outlineColor); delete[] seg; }
static void DrawCircle(cpVect p, cpFloat a, cpFloat r, cpSpaceDebugColor outline, cpSpaceDebugColor fill, cpDataPointer data) { const Color4F fillColor(fill.r, fill.g, fill.b, fill.a); const Color4F outlineColor(outline.r, outline.g, outline.b, outline.a); DrawNode* drawNode = static_cast<DrawNode*>(data); float radius = PhysicsHelper::cpfloat2float(r); Vec2 centre = PhysicsHelper::cpv2point(p); static const int CIRCLE_SEG_NUM = 12; Vec2 seg[CIRCLE_SEG_NUM] = {}; for (int i = 0; i < CIRCLE_SEG_NUM; ++i) { float angle = (float)i * M_PI / (float)CIRCLE_SEG_NUM * 2.0f; Vec2 d(radius * cosf(angle), radius * sinf(angle)); seg[i] = centre + d; } drawNode->drawPolygon(seg, CIRCLE_SEG_NUM, fillColor, 1, outlineColor); }
void ClippingNode::setSquareStencil() { isSquareStencil = true; if(getStencil() == nullptr || dynamic_cast<DrawNode*>(getStencil()) == nullptr) { setStencil(DrawNode::create()); //addChild(getStencil()); //If you need to see what the stencil is like, for debug purpose } DrawNode* stencil = (DrawNode*)getStencil(); stencil->clear(); Vec2 rectangle[4]; rectangle[0] = Vec2(0, 0); rectangle[1] = Vec2(this->getContentSize().width / this->getScaleX(), 0); rectangle[2] = Vec2(this->getContentSize().width / this->getScaleX(), this->getContentSize().height / this->getScaleY()); rectangle[3] = Vec2(0, this->getContentSize().height / this->getScaleY()); Color4F white(1, 1, 1, 1); stencil->drawPolygon(rectangle, 4, white, 1, white); }
FailScene::FailScene(void) { Vec2 points[] = { Vec2(0, 0), Vec2(0, 140), Vec2(400, 140), Vec2(400, 0) }; DrawNode* draw = DrawNode::create(); //±³¾° draw->drawPolygon(points, 4, ColorUtil::getColor4F(0x00 , 0xAD , 0xFF, 0xFF), 0, ColorUtil::getColor4F(0x00, 0xDA, 0xFF, 0xFF)); this->addChild(draw); draw->setAnchorPoint(Point(.5, .5)); draw->setPositionX(ScreenUtil::getCenter().x - 200); draw->setPositionY(ScreenUtil::getCenter().y - 140); Vec2 points2[] = { Vec2(0, 0), Vec2(0, 150), Vec2(230, 150), Vec2(230, 0) }; DrawNode* draw2 = DrawNode::create(); //±³¾° draw2->drawPolygon(points2, 4, ColorUtil::getColor4F(0x00 , 0xAD , 0xFF, 0xFF), 0, ColorUtil::getColor4F(0x00, 0xDA, 0xFF, 0xFF)); this->addChild(draw2); draw2->setAnchorPoint(Point(.5, .5)); draw2->setPositionX(ScreenUtil::getCenter().x - 115); draw2->setPositionY(ScreenUtil::getCenter().y + 40); LabelTTF* scoreTxt = LabelTTF::create("Score:", "Arial", 45); scoreTxt->setAnchorPoint(Point(0, .5)); scoreTxt->setPosition(Point(draw2->getPositionX() + 10, draw2->getPositionY() + 110)); this->addChild(scoreTxt); LabelTTF* bestScoreTxt = LabelTTF::create("Best:", "Arial", 45); bestScoreTxt->setAnchorPoint(Point(0, .5)); bestScoreTxt->setPosition(Point(scoreTxt->getPositionX(), draw2->getPositionY() + 50)); this->addChild(bestScoreTxt); LabelTTF* scoreValTxt = LabelTTF::create("0", "Arial", 45); scoreValTxt->setPositionX(scoreTxt->getPositionX() + scoreTxt->getContentSize().width + 25); scoreValTxt->setPositionY(scoreTxt->getPositionY()); scoreValTxt->setTag(scoreValTxtTag); this->addChild(scoreValTxt); LabelTTF* bestScoreValTxt = LabelTTF::create("0", "Arial", 45); bestScoreValTxt->setPositionX(scoreValTxt->getPositionX()); bestScoreValTxt->setPositionY(bestScoreTxt->getPositionY()); bestScoreValTxt->setTag(bestScoreValTxtTag); this->addChild(bestScoreValTxt); LabelTTF* replayTxt = LabelTTF::create("Replay", "Arial", 45); MenuItemLabel* replayBtn = MenuItemLabel::create(replayTxt); replayBtn->setTag(replayBtnTag); LabelTTF* shareTxt = LabelTTF::create("Share", "Arial", 45); MenuItemLabel* shareBtn = MenuItemLabel::create(shareTxt); Menu* menu = Menu::create(replayBtn, shareBtn, NULL); menu->setPositionX(ScreenUtil::getCenter().x); menu->setPositionY(ScreenUtil::getCenter().y - 70); menu->alignItemsHorizontallyWithPadding(50); menu->setTag(menuTag); this->addChild(menu); }
BNPolyObject::BNPolyObject(std::string pic,float* data,int count,Layer* layer) { int idata=0; pointData.clear(); for(int ii=0;ii<count;ii++) { pointData.push_back(Point(data[idata],data[idata+1])); idata=idata+2; } //创建多边形顶点序列 C2DPoint* points=new C2DPoint[count]; for(int i=0;i<count;i++) { points[i]=C2DPoint(data[i*2],data[i*2+1]); } //根据顶点数据创建多边形 cp=new C2DPolygon(points,(unsigned int)count,true); //创建显示精灵 sp = Sprite::create(pic); sp->setPosition(Point(0,0)); sp->setAnchorPoint(Point(0,0)); //创建剪裁用DrawNode DrawNode* shape = DrawNode::create(); //创建凸拆分 cp->CreateConvexSubAreas(); //用于获取凸拆分的 C2DPolygonSet cps; //获取凸拆分区域 cp->GetConvexSubAreas(cps); //获取凸拆分区域的数量 int tcount=cps.size(); //遍历所有凸拆分区域 for(int i=0;i<tcount;i++) { //获取一个凸拆分区域 C2DPolygon* cpTemp=cps.GetAt(i); //获取此凸拆分的顶点序列 C2DPointSet pointCopy; cpTemp->GetPointsCopy(pointCopy); //获取顶点的数量 int tempC=pointCopy.size(); //创建绘制用顶点数组 Point* pArray=new Point[tempC]; for(int j=0;j<tempC;j++) { C2DPoint* tempCP=pointCopy.GetAt(j); pArray[j]=Point(tempCP->x,tempCP->y); } //绘制形状 Color4F green(0, 1, 0, 1); Color4F red(1, 0, 0, 1); shape->drawPolygon(pArray,tempC, green, 2, red); } //创建剪裁节点 ClippingNode* clipper=ClippingNode::create(); //设置剪裁模板 clipper->setStencil(shape); //设置被剪裁节点 clipper->addChild(sp,0); clipper->setPosition(Point(0,0)); //将被剪裁节点放置到层中 layer->addChild(clipper, 0); }
BNPolyObject::BNPolyObject(std::string pic,C2DPolygon* cpIn,Layer* layer) { //创建多边形,去除影响 cp=cpIn; C2DPointSet pointCopyL; cp->GetPointsCopy(pointCopyL); cp=new C2DPolygon(); cp->Create(pointCopyL,true); //获取顶点的数量 int tempC11=pointCopyL.size(); pointData.clear(); for(int j=0;j<tempC11;j++) { C2DPoint* tempCP=pointCopyL.GetAt(j); pointData.push_back(Point(tempCP->x,tempCP->y)); } //创建显示精灵 sp = Sprite::create(pic); sp->setPosition(Point(0,0)); sp->setAnchorPoint(Point(0,0)); //创建剪裁用DrawNode DrawNode* shape = DrawNode::create(); //创建凸拆分 cp->ClearConvexSubAreas(); cp->CreateConvexSubAreas(); //用于获取凸拆分的 C2DPolygonSet cps; //获取凸拆分区域 cp->GetConvexSubAreas(cps); //获取凸拆分区域的数量 int tcount=cps.size(); //遍历所有凸拆分区域 for(int i=0;i<tcount;i++) { //获取一个凸拆分区域 C2DPolygon* cpTemp=cps.GetAt(i); //获取此凸拆分的顶点序列 C2DPointSet pointCopy; cpTemp->GetPointsCopy(pointCopy); //获取顶点的数量 int tempC=pointCopy.size(); //创建绘制用顶点数组 Point* pArray=new Point[tempC]; for(int j=0;j<tempC;j++) { C2DPoint* tempCP=pointCopy.GetAt(j); pArray[j]=Point(tempCP->x,tempCP->y); } //绘制形状 Color4F green(0, 1, 0, 0); Color4F red(1, 0, 0, 0.5f); shape->drawPolygon(pArray,tempC, green, 2, red); } //创建剪裁节点 clipper=ClippingNode::create(); //设置剪裁模板 clipper->setStencil(shape); //设置被剪裁节点 clipper->addChild(sp,0); clipper->setPosition(Point(0,0)); //将被剪裁节点放置到层中 layer->addChild(clipper, 0); }
//实现HelpLayer类中的init方法,初始化布景 bool HelpLayer::init() { //调用父类的初始化 if ( !Layer::init() ) { return false; } //获取可见区域尺寸 Size visibleSize = Director::getInstance()->getVisibleSize(); //获取可见区域原点坐标 Point origin = Director::getInstance()->getVisibleOrigin(); //创建背景 Sprite* background = Sprite::create("morning.png"); //设置锚点 background->setAnchorPoint(Point(0, 0)); //设置精灵对象的位置 background->setPosition(Point(origin.x,origin.y + visibleSize.height - background->getContentSize().height)); //将精灵添加到布景中 this->addChild(background, 0); //创建地面精灵1 Sprite* floor = Sprite::create("floor.png"); //设置锚点 floor->setAnchorPoint(Point(0, 0)); //设置精灵对象位置 floor->setPosition(Point(origin.x, origin.y)); this->addChild(floor); //设置地面滚动 floor->runAction(RepeatForever::create( Sequence::create( MoveTo::create(0.5, Point(-120, 0)), MoveTo::create(0, Point(0, 0)), NULL ))); Sprite* back = Sprite::create("helpBack.png"); back->setPosition(Point(visibleSize.width/2, visibleSize.height/2 - 5)); this->addChild(back, 0); for(int i=0;i<4;i++) { sp[i] = Sprite::create(StringUtils::format("help%d.png",i)); sp[i]->setPosition(180+i*360, 320); if(i!=0) { sp[i]->runAction(FadeOut::create(0)); } } //返回菜单 MenuItemImage* menuItem = MenuItemImage::create( "menu.png", "menu_off.png", CC_CALLBACK_1(HelpLayer::menuCallBack, this) //点击时执行的回调方法 ); menuItem->setPosition(Point(270,50)); Menu* menu = Menu::create(menuItem, NULL); //设置菜单位置 menu->setPosition(Point::ZERO); this->addChild(menu,10); Point pp[4]; pp[0] = Point(0, 0); pp[1] = Point(360, 0); pp[2] = Point(360, 640); pp[3] = Point(0, 640); Color4F green(0, 1, 0, 0); Color4F red(1, 0, 0, 0); //创建剪裁用DrawNode DrawNode* shape = DrawNode::create(); shape->drawPolygon(pp ,4, green, 1,red); //创建剪裁节点 ClippingNode* clipper=ClippingNode::create(); //设置剪裁模板 clipper->setStencil(shape); //设置被剪裁节点 for(int i=0; i<4; i++) { clipper->addChild(sp[i],0); } clipper->setPosition(Point(90, 160)); //将被剪裁节点放置到层中 this->addChild(clipper, 0); //创建一个单点触摸监听 EventListenerTouchOneByOne* listenerTouch = EventListenerTouchOneByOne::create(); //设置下传触摸 listenerTouch->setSwallowTouches(true); //开始触摸时回调onTouchBegan方法 listenerTouch->onTouchBegan = CC_CALLBACK_2(HelpLayer::onTouchBegan, this); //触摸结束时回调onTouchEnded方法 listenerTouch->onTouchEnded = CC_CALLBACK_2(HelpLayer::onTouchEnded, this); //添加到监听器 _eventDispatcher->addEventListenerWithSceneGraphPriority(listenerTouch, clipper); return true; }