How to use ScaleTo() like

Hi. I was trying to create a ScaleTo action using the dimension I wanted to get as a scale factor. It took me a while to understand the difference between dimensions and scale factor, but now I thought that, to get a more accurate result, I could set my object’s dimension to 1 so that I can use ScaleTo to change dimension adding an ease effect.
Well, here’s where I’m stuck:

I have a class of dots:

#include "Dot.h"
USING_NS_CC;

bool Dot::init()
{
    if ( !DrawNode::init() )
        return false;
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();
    
    this->clear();
    drawDot(Vec2((arc4random() % (int)(visibleSize.width - visibleSize.width / 6)) + visibleSize.width / 12, (arc4random() % (int)(visibleSize.width - visibleSize.width / 3.5)) + visibleSize.width / 7), 1, 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;
}

What I aim to get is this dot “appearing” from nothing (hope it’s clear). Setting its dimension to 0 would cause it to never grow, but setting it to 1 has a strange effect… As I start the app in the simulator I see the dot already grown for just a frame, then only my background. The dot was declared in the init() function of my scene:

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

Can someone help me get this working? Thank you! : D

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;
}

Hi. Thank you, this was very helpful. So I didn’t consider the fact that I was setting the position in that node, not in the layer. Now it is much clearer. Thanks again :slight_smile:

No wait… If the setPosition() method sets the node’s position in the layer, why does using

Vec2 posVec =  Vec2((arc4random() % (int)(visibleSize.width - visibleSize.width / 6)) + visibleSize.width / 12, (arc4random() % (int)(visibleSize.height - visibleSize.height / 3.5)) + visibleSize.height / 7);

this->setPosition(posVec);

bring my dot off the screen (-y)?
With this posVec I just tried to set the dot’s position somewhere in the screen without making it go off of it. But this should theorically go only for 16:9 displays cause, knowing that diameter = visibleSize.width / 6, a 4:3 display would have something different…
Instead, setting the dot’s position in the center in either the dot’s class or the main scene causes it to never appear… Which is quite strange, because if, from the dot’s class, I can set the dot’s position in the node, theoretically from the scene I should be able to set the node’s position in the layer. Correct me if I’m wrong.
So, to overcome this problem, isn’t there a way to use the node’s contentSize for the defining of the position? Something like

Vec2 posVec = Vec2((arc4random() % (int)(visibleSize.width - [this->getContentSize] ) + [this->getContentSize] / 2, (arc4random() % (int)(visibleSize.height - [this->getContentSize] ) + [this->getContentSize] / 2);

this->setPosition(posVec);

But first of all, is the contentSize value being modified when scaling the node, or will it always remain

Size(diameter, diameter));

which is 2, 2?

So, reformulating this all, how would you do if you wanted to set this dot’s position to anywhere in the screen but preventing it to go off of it?

Thanks for all the help again! :smiley:

Sorry, my mistake. I was using portrait resolutions in landscape mode.
http://www.sonarlearning.co.uk/questions.php?question-topic=176