cocos2dx-v3.0-图形绘制-draw()函数
来源:互联网 发布:淘宝网卖家信用高流量 编辑:程序博客网 时间:2024/05/23 15:38
cocos2dx已经封装了很多关于opengl的函数,可以方便的使用,在3.0v以前,是在draw编写绘制代码,3.0v以后,还是在draw函数里,但发生了点变化。
先来看一下Node类关于draw函数的申明
virtual void draw(Renderer *renderer, const Mat4& transform, uint32_t flags); virtual void draw() final;
第二函数已经被声明为final,也就是不能重载(C++11规定,final函数必需要加关键词virtual).所以不能再像老版本一样使用。
重写第一个方法来绘制你自己的节点。
被 TransitionFadeTR, TransitionSplitCols, TransitionTurnOffTiles, TransitionCrossFade, Sprite, LayerColor, Label, RenderTexture,SpriteBatchNode, PhysicsSprite, ProgressTimer, ParticleSystemQuad, ParticleBatchNode, TransitionScene, DrawNode, MotionStreak,VideoPlayer, TransitionPageTurn, AtlasNode , 以及 PhysicsDebugNode 重载.
下面给出一个例子,先看结果图
可以拖动四个结点改变曲线
声明代码
#ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__#include "cocos2d.h"class HelloWorld : public cocos2d::Layer{public: // there's no 'id' in cpp, so we recommend returning the class instance pointer static cocos2d::Scene* createScene(); // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone virtual bool init(); // a selector callback void menuCloseCallback(cocos2d::Ref* pSender);virtual void draw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &transform, uint32_t flags) override; // implement the "static create()" method manually CREATE_FUNC(HelloWorld);private:cocos2d::Sprite *yellow, *bule, *orange, *red;bool onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event);void onTouchMoved(cocos2d::Touch*touch, cocos2d::Event *event);};#endif // __HELLOWORLD_SCENE_H__
实现代码
// on "init" you need to initialize your instancebool HelloWorld::init(){ ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->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 auto closeItem = MenuItemImage::create( "CloseNormal.png", "CloseSelected.png", CC_CALLBACK_1(HelloWorld::menuCloseCallback, this)); closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 , origin.y + closeItem->getContentSize().height/2)); // create menu, it's an autorelease object auto menu = Menu::create(closeItem, NULL); menu->setPosition(Vec2::ZERO); this->addChild(menu, 1); ///////////////////////////// // 3. add your codes below... // add a label shows "Hello World" // create and initialize a label auto label = LabelTTF::create("Hello World", "Arial", 24); // position the label on the center of the screen label->setPosition(Vec2(origin.x + visibleSize.width/2, origin.y + visibleSize.height - label->getContentSize().height)); // add the label as a child to this layer this->addChild(label, 1); // add "HelloWorld" splash screen" auto sprite = Sprite::create("HelloWorld.png"); // position the sprite on the center of the screen sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); // add the sprite as a child to this layer // this->addChild(sprite, 0);red = Sprite::create("red.png");red->setPosition(10, 10);this->addChild(red,3); yellow = Sprite::create("yellow.png");yellow->setPosition(200, 200);this->addChild(yellow, 3);bule = Sprite::create("bule.png");bule->setPosition(400, 400);this->addChild(bule, 3);orange = Sprite::create("orang.png");orange->setPosition(500, 500);this->addChild(orange, 4);auto listener = EventListenerTouchOneByOne::create();listener->onTouchBegan = CC_CALLBACK_2( HelloWorld::onTouchBegan,this);listener->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved, this);this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener,bule);this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener->clone(), orange);this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener->clone(), yellow);this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener->clone(), red); return true;}void HelloWorld::menuCloseCallback(Ref* pSender){#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert"); return;#endif Director::getInstance()->end();#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) exit(0);#endif}void HelloWorld::draw(Renderer* renderer, const Mat4 &transform, uint32_t flags){auto array = PointArray::create(10);ccDrawLine(Vec2(100, 100), Vec2(900, 100));ccDrawLine(Vec2(100, 100), Vec2(100, 600));array->addControlPoint(Vec2(100, 100));array->addControlPoint(red->getPosition());array->addControlPoint(bule->getPosition());array->addControlPoint(yellow->getPosition());array->addControlPoint(orange->getPosition());ccDrawCardinalSpline(array, 0, 100);}bool HelloWorld::onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event){auto sprite = static_cast<Sprite*>(event->getCurrentTarget());auto point = sprite->convertToNodeSpace(touch->getLocation());auto rect = Rect(0, 0, sprite->getContentSize().width, sprite->getContentSize().height);if (rect.containsPoint(poi
0 0
- cocos2dx-v3.0-图形绘制-draw()函数
- cocos2dx-draw绘制
- cocos2dx V3.2 customer draw method
- cocos2dx 3.0 绘制图形
- cocos2dx sprite的draw函数
- Draw Shapes——绘制图形
- 和屌丝一起学cocos2dx-draw绘制
- Python 绘制函数图形
- OpenLayers 3 之 绘制图形(ol.interaction.Draw)详解
- SVG(可缩放矢量图形)绘制工具Method Draw
- draw 绘制
- cocos2dx 重载node绘制基本图形
- cocos2dx v3.0 主循环
- Win32App绘制封闭图形函数
- cocos2dx lua draw
- OpenLayers 3 之 绘制图形(ol.interaction.Draw)原理解析
- cocos2dx-v3.0命令行创建工程
- cocos2dx-v3.0-游戏素材获取
- kettle spoon pentaho
- 买牛奶(南阳oj892)
- dd大牛的《背包九讲》
- [caffe]深度学习之图像分类模型AlexNet解读
- 成功实现Django Admin图片上传与缩略图处理
- cocos2dx-v3.0-图形绘制-draw()函数
- Mega Man’s Missions
- 9.12 工程日志
- new, delete 的用法
- HDU 2425-Hiking Trip(BFS+优先队列)
- protel 99se 中间区域不敷铜
- 关于ArrayList与数组对象的直接输出
- 2034 人见人爱A-B
- android应用开发详解(十四)--------------Intent的Action属性(续)