先上个图吧,实现的效果就如下图
这个项目包括以下几点知识点(重点):
1.画固定方向的x、y轴,并且为白色。
2.画其他颜色的不同线段,长度、角度随需求而改变。
运行环境:win10、cocos2d-x、vs2013。
接下来看代码吧,其实是挺简单的。
#include "DemoLayer.h"
USING_NS_CC;
CCScene* DemoLayer::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create();
// 'layer' is an autorelease object
DemoLayer *layer = DemoLayer::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool DemoLayer::init()
{
//
// 1. super init first
if ( !CCLayer::init() )
{
return false;
}
CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
/
// 2. add a menu item with "X" image, which is clicked to quit the program
// you may modify it.
// add a "close" icon to exit the progress. it's an autorelease object
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(DemoLayer::menuCloseCallback));
pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
origin.y + pCloseItem->getContentSize().height/2));
// create menu, it's an autorelease object
CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
pMenu->setPosition(CCPointZero);
this->addChild(pMenu, 1);
///
3. add your codes below...
add a label shows "Hello World"
create and initialize a label
//
//CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);
//
position the label on the center of the screen
//pLabel->setPosition(ccp(origin.x + visibleSize.width/2,
// origin.y + visibleSize.height - pLabel->getContentSize().height));
add the label as a child to this layer
//this->addChild(pLabel, 1);
add "HelloWorld" splash screen"
//CCSprite* pSprite = CCSprite::create("HelloWorld.png");
position the sprite on the center of the screen
//pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
add the sprite as a child to this layer
//this->addChild(pSprite, 0);
//
DrawVector();
return true;
}
void DemoLayer::DrawVector()
{
CCSize size = CCDirector::sharedDirector()->getWinSize();
CCPoint origin = ccp(size.width / 2, size.height / 2);
//用纹理缓存(资源池)加载纹理 这里的纹理就是一个点
CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage("tex.png");
//创建批处理渲染方式的精灵节点
CCSpriteBatchNode *mgr = CCSpriteBatchNode::createWithTexture(texture);
//添加到层中
this->addChild(mgr);
//画x轴
CCSprite *sp = CCSprite::createWithTexture(texture, CCRectMake(0, 0, 2, 2));
sp->setAnchorPoint(ccp(0, 0)); //设置精灵的锚点在(0,0)
sp->setPosition(origin); //将精灵放在屏幕中心
sp->setScaleX(300 / 2); //设置精灵x方向的缩放系数
mgr->addChild(sp);
//画y轴
//补充代码完成
sp = CCSprite::createWithTexture(texture, CCRectMake(0, 0, 2, 2));
sp->setAnchorPoint(ccp(0, 0)); //设置精灵的锚点在(0,0)
sp->setPosition(origin); //将精灵放在屏幕中心
sp->setScaleX(300 / 2); //设置精灵x方向的缩放系数
sp->setRotation(-90);
mgr->addChild(sp);
CCPoint v1 = ccp(13, 73);
CCPoint v2 = ccp(124, 124);
//画向量v1,红色
//补充代码完成
sp = CCSprite::createWithTexture(texture, CCRectMake(0, 0, 2, 2));
sp->setAnchorPoint(ccp(0, 0)); //设置精灵的锚点在(0,0)
sp->setPosition(origin); //将精灵放在屏幕中心
sp->setScaleX(ccpLength(v1) / 2); //设置精灵x方向的缩放系数
sp->setRotation(-(ccpToAngle(v1) / 3.1415 * 180));
sp->setColor(ccRED);
mgr->addChild(sp);
//画向量v2,绿色
//补充代码完成
sp = CCSprite::createWithTexture(texture, CCRectMake(0, 0, 2, 2));
sp->setAnchorPoint(ccp(0, 0)); //设置精灵的锚点在(0,0)
sp->setPosition(origin); //将精灵放在屏幕中心
sp->setScaleX(ccpLength(v2) / 2); //设置精灵x方向的缩放系数
sp->setRotation(-(ccpToAngle(v2) / 3.1415 * 180));
sp->setColor(ccGREEN);
mgr->addChild(sp);
//画向量v3 v3 = v1 + v2 黄色
//补充代码完成
CCPoint v3 = v1 + v2;
sp = CCSprite::createWithTexture(texture, CCRectMake(0, 0, 2, 2));
sp->setAnchorPoint(ccp(0, 0)); //设置精灵的锚点在(0,0)
sp->setPosition(origin); //将精灵放在屏幕中心
sp->setScaleX(ccpLength(v3) / 2); //设置精灵x方向的缩放系数
sp->setRotation(-(ccpToAngle(v3) / 3.1415 * 180));
sp->setColor(ccYELLOW);
mgr->addChild(sp);
//画向量v4 v4 = v1 - v2 橘色
//补充代码完成
CCPoint v4 = v1 - v2;
sp = CCSprite::createWithTexture(texture, CCRectMake(0, 0, 2, 2));
sp->setAnchorPoint(ccp(0, 0)); //设置精灵的锚点在(0,0)
sp->setPosition(origin); //将精灵放在屏幕中心
sp->setScaleX(ccpLength(v4) / 2); //设置精灵x方向的缩放系数
sp->setRotation(-(ccpToAngle(v4) / 3.1415 * 180));
sp->setColor(ccORANGE);
mgr->addChild(sp);
//画向量v5 v5 = v1 * -3 蓝色
//补充代码完成
CCPoint v5 = v1 * -3;
sp = CCSprite::createWithTexture(texture, CCRectMake(0, 0, 2, 2));
sp->setAnchorPoint(ccp(0, 0)); //设置精灵的锚点在(0,0)
sp->setPosition(origin); //将精灵放在屏幕中心
sp->setScaleX(ccpLength(v5) / 2); //设置精灵x方向的缩放系数
sp->setRotation(-(ccpToAngle(v5) / 3.1415 * 180));
sp->setColor(ccBLUE);
mgr->addChild(sp);
}
void DemoLayer::menuCloseCallback(CCObject* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
#endif
}
上面的就是DemoLayer.cpp的全部代码,看注释一般都能看懂啦。
在这里用的图片资源是一个2x2像素的白点。
由于太简单,也没有什么可以讲解的,不懂的小伙伴再评论区问我吧
项目源代码下载地址:http://file.yiyuen.com/detail.html?id=1649
这个项目记得要拷贝到D:\cocos2d-x-2.2.6\projects下面再打开哦,不然会提示头文件不能包含。
先到这里啦,下一个项目给大家带来几个cocos2d-x的实例。