Good way to using variables: cc.Vec2 created in functions

Good way to using variables: cc.Vec2 created in functions
0.0 0

#1

Hi, I have a question about regarding to memories management.
If I make function like below,

    // return:cc.Vec2
    // @param: cc.Vec2 inputPos
    getMovePosition: function (inputVec) {
        returnVec = new cc.Vec2(0, 0);
        // some calculation on returnVec, using inputVec
        // ...
        return returnVec;
    },

and use it in update, like below,

    update: function (dt) {
        // ....
        this.someObject.setPosition(this.getMovePosition( someVector ));
    },

Is this bad way to using memory?
and use it in update, like below,

    update: function (dt) {
        // ....
        this.someObject.setPosition(this.getMovePosition( someVector ));
    },

Is this bad way to using memory?
Shall I bretter to secure this working vector as global variable, to avoid a lot of garbage collection? I’m making action game on mobile with HTML5.

Thanks.


#2

Hi PaPiPuGames,

avoid new in the update loop, if you need performance.

Cheers

Mike


#3

Hi Mile,

That is as I thought!
I’ll remove all new from the earlier code.

Thanks.
-Hiroki


#4

Does getPosition() creating new cc.Vec2?
Is it I better to avoid using getPosition() in the update, if I really need performance?
If so, what is the good approach for doing it?

-Hiroki


#5

I don’t know. To be save I would try to get the position with node.x and node.y.


#6

Thanks. I will investigate and post here what functions are creating cc.Vec2.
getPosition(), cc.Vec2:add, sub, mul are suspicious.
Maybe better to use addSelf, subSelf, mulSelf. These seem prepared for not creating new cc.Vec2.

-Hiroki


#7

Better not use below functions, since they are creating new cc.Vec2.
getPosition(), Vec2: add, sub, mul, div, scale and other functions which have *Self.

Better to use below,
node.position, node.x, y, Vec2:addSelf, subSelf, mulSelf, divSelf, scalseSelf and other *Self functions.

** Important **
Good to use node.position, node.x, y for reference, but you should use node.setPosition() for setting values.
Since setPosition() is processing some calculation for display.
Thanks.

-Hitoki