DrawNode() not drawing :/

Hi all. I have to create a class of dots. I have a Dot.h file:

#include "cocos2d.h"
USING_NS_CC;
class Dot : public cocos2d::DrawNode{
public:
    virtual bool init();
    CREATE_FUNC(Dot);
private:
    bool circumstanceA = 1;
    bool circumstanceB = 0;
};

and a Dot.cpp file:

#include "Dot.h"
USING_NS_CC;

bool Dot::init()
{
    if ( !DrawNode::init() )
        return false;
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();
    
    drawDot(Vec2((arc4random() % (int)(visibleSize.width - visibleSize.width / 6)) + visibleSize.width / 12, (arc4random() % (int)(visibleSize.width - visibleSize.width / 3.5)) + visibleSize.width / 7), 0, Color4F(1,1,1,1));
        auto scale = ScaleTo::create(0.5, (visibleSize.height / 6) + (arc4random() % (int)(visibleSize.height / 15)) - visibleSize.height /30);
    auto scale_ease = EaseBackOut::create(scale->clone());
    this->runAction(scale_ease);
    
    return true;
}

But when I try to create a new Dot from the current scene:

auto newDot = Dot::create();
this->addChild(newDot, 1);

no dot is displayed in the simulator.
I tried also creating it from the scene, like

auto prova = DrawNode::create();
prova->drawDot(Vec2(visibleSize.width / 2, visibleSize.height / 2), 50, Color4F::WHITE);
this->addChild(prova, 1);

but the result is the same :confused:

What am I missing?

I tried also with DrawLine, nothing is being displayed.

I’m kinda new to this cocos2d-x syntax, so any help would be very appreciated :smile:

Many thanks!

ps: Is it correct to use

this->runAction(scale_ease);

from the object’s class init() function?

Trying the same thing with another project created with Cocos2d-x 3.5 I noticed that, when I remove the code for implementing multi resolution support with different folders in Resource, the program in the iPhone simulator draws the dot…

Here’s the code I implement in AppDelegate.cpp:

auto fileUtils = FileUtils::getInstance();
auto screenSize = glview->getFrameSize();
std::vector<std::string> resDirOrders;

// check which assets the devices requires
if ( 2048 == screenSize.width || 2048 == screenSize.height ) // retina iPad
{
    resDirOrders.push_back("ipadhd");
    resDirOrders.push_back("ipad");
    resDirOrders.push_back("iphonehd5");
    resDirOrders.push_back("iphonehd");
    resDirOrders.push_back("iphone");
    
    glview->setDesignResolutionSize(1536, 2048, ResolutionPolicy::NO_BORDER);
}
else if ( 1024 == screenSize.width || 1024 == screenSize.height ) // non retina iPad
{
    resDirOrders.push_back("ipad");
    resDirOrders.push_back("iphonehd5");
    resDirOrders.push_back("iphonehd");
    resDirOrders.push_back("iphone");
    
    glview->setDesignResolutionSize(768, 1024, ResolutionPolicy::NO_BORDER);
}
else if ( 1136 == screenSize.width || 1136 == screenSize.height ) // retina iPhone (5 and 5S)
{
    resDirOrders.push_back("iphonehd5");
    resDirOrders.push_back("iphonehd");
    resDirOrders.push_back("iphone");
    
    glview->setDesignResolutionSize(640, 1136, ResolutionPolicy::NO_BORDER);
}
else if ( 960 == screenSize.width || 960 == screenSize.height ) // retina iPhone (4 and 4S)
{
    resDirOrders.push_back("iphonehd");
    resDirOrders.push_back("iphone");
    
    glview->setDesignResolutionSize(640, 960, ResolutionPolicy::NO_BORDER);
}
else // non retina iPhone and Android devices
{
    if ( 1080 < screenSize.width ) // android devices that have a high resolution
    {
        resDirOrders.push_back("iphonehd");
        resDirOrders.push_back("iphone");
        
        glview->setDesignResolutionSize(640, 960, ResolutionPolicy::NO_BORDER);
    }
    else // non retina iPhone and Android devices with lower resolutions
    {
        resDirOrders.push_back("iphone");
        
        glview->setDesignResolutionSize(320, 480, ResolutionPolicy::NO_BORDER);
    }
}

fileUtils->setSearchPaths(resDirOrders);

What do you think the problem could be? Thanks! :smile:

ps: Sorry for the wall!

The action ScaleTo() always scales based on AnchorPoint & ContentSize of the Node to which the action is applied. Here I analyzed the code, none of 2 these things set out which leads to a strange effect because both values will be 0 by default which misguides. Also the position, passed to drawDot() function is not aligned with the position of DrawNode. In general, drawing of content should be on center of node in which it is drawn. The center means a half of content size. The following modified code may help you understand to avoid strange effects.

bool Dot::init()
{
    if ( !DrawNode::init() )
        return false;
    
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();
    
    this->clear();
    
    float radius = 1;
    float diameter = radius*2;
    Vec2 posVec =  Vec2((arc4random() % (int)(visibleSize.width - visibleSize.width / 6)) + visibleSize.width / 12,
                     (arc4random() % (int)(visibleSize.width - visibleSize.width / 3.5)) + visibleSize.width / 7);
    
    this->setAnchorPoint(Point(0.5,0.5));
    this->setContentSize(Size(diameter, diameter));
    this->setPosition(posVec);
    
    drawDot(Vec2(radius,radius), radius, Color4F(1,1,1,1) );
    
    auto scale = ScaleTo::create(0.5,(visibleSize.height / 6) + (arc4random() % (int)(visibleSize.height / 15)) - visibleSize.height /30);
    auto scale_ease = EaseBackOut::create(scale->clone());
    this->runAction(scale_ease);
    
    return true;
}