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

前端之家收集整理的这篇文章主要介绍了cocos2dx-v3.0-图形绘制-draw()函数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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



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


声明代码

#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__

实现代码

@H_502_77@// on "init" you need to initialize your instance bool 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

猜你在找的Cocos2d-x相关文章