My code in v2.2.1. used to look like this (no, it’s not actual working code, just an illustration):
var mySprite = cc.Sprite.crate...
...
var position = mySprite.getPosition();
position.x+= 20;
moveFunction(mySprite, position);
But then, when I updated to v2.2.2 I got errors, so I had to change position.x+=20
to position.setX(position.x+20)
. This, however, makes the sprite move instantly, since it turns out position
is a reference to the sprite’s position, rather than a copy of it.
So now, my code looks like this:
var mySprite = cc.Sprite.crate...
...
var temp = mySprite.getPosition();
var position = cc.p(temp.x, temp.y);
position.x+= 20;
moveFunction(mySprite, position);
Like this, everything works as it should, but I wonder: is there a better way to clone gometry objects than manually making a duplicate?
Hi,
I’m sorry that we change the usage of setPosition and setContentSize for better performance.
Here is the upgrade guide about this: http://www.cocos2d-x.org/wiki/Upgrade_Guide_from_Cocos2d-html5_v221_to_v222
Thanks for feedback.
David
Thanks. I saw that article after I created my post.
But still, what I wanted to know is if there is an easier and more straightforward way to get a copy of that object than having to do:
var temp = mySprite.getPosition();
var position = cc.p(temp.x, temp.y);
We haven’t implemented such functionality for cc.Node yet
What do you think about these api design for this functionality?
var position = cc.clone(mySprite.getPosition());
var position = cc.p(mySprite.getPosition());
var position = mySprite.clone("position");
Or tell us your suggestion.
To be more general, if you consider something not right or not easy to use in cocos2d-html5, please tell us and an api suggestion will be really appreciated.
Huabin
Maybe something like var position = mySprite.getPosition().clone()
?
var position = cc.p(mySprite.getPosition());
also sounds good to me.
Point is a massively used object, I don’t think it’s a good idea to add more functions to it. We will discuss it.
Thanks for your feedback
Oh, I’m stupid, we do have cc.clone in CCCommon.js
Huabin LING wrote:
Oh, I’m stupid, we do have cc.clone in CCCommon.js
If I try var myPos = cc.clone(mySprite.getPosition());
I get “Warning of _PointConst: Modification to const or private property is forbidden” in the console. So I’m sticking with var tempPos = mySprite.getPosition(); var myPos = cc.p(tempPos.x, tempPos.y);
for now.
cc.p and cc.size support two types of parameters now.
for example:
var myPos = cc.p(tempPos.x, tempPos.y);
and
var myPos = cc.p(tempPos);
I see, so I should be able to just do var myPos = cc.p(mySprite.getPosition());
and it should work fine, right?
Yes, In the latest version of develop on github, it works well.