cocos2dx-v3.0-图形绘制-draw()函数

cocos2dx-v3.0-图形绘制-draw()函数,第1张

概述       cocos2dx已经封装了很多关于opengl的函数,可以方便的使用,在3.0v以前,是在draw编写绘制代码,3.0v以后,还是在draw函数里,但发生了点变化。       先来看一下Node类关于draw函数的申明 virtual void draw(Renderer *renderer, const Mat4& transform, uint32_t flags);

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,TransitionSplitColsTransitionCrossFadeLayerColorRenderTexturePhysicsSpriteParticleSystemQuadTransitionSceneMotionStreakTransitionPageTurnPhysicsDebugNode重载.

下面给出一个例子,先看结果图


可以拖动四个结点改变曲线


声明代码
#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,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,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,sprite->getContentSize().wIDth,sprite->getContentSize().height);	if (rect.containsPoint(poi
总结

以上是内存溢出为你收集整理的cocos2dx-v3.0-图形绘制-draw()函数全部内容,希望文章能够帮你解决cocos2dx-v3.0-图形绘制-draw()函数所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1002470.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存