Skip to content

Commit 218b3b8

Browse files
committed
Merge pull request #1044 from xingsenma/cc.p,cc.size
Fixed #2441: Improve update function of Action, avoid using temp cc.p or cc.size
2 parents a0db1a9 + 007f1d1 commit 218b3b8

File tree

7 files changed

+379
-271
lines changed

7 files changed

+379
-271
lines changed

cocos2d/actions/CCAction.js

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ cc.Action = cc.Class.extend(/** @lends cc.Action# */{
4848
_tag:cc.ACTION_TAG_INVALID,
4949

5050
//**************Public Functions***********
51-
ctor:function(){
52-
this._originalTarget = null;
51+
ctor:function () {
52+
this._originalTarget = null;
5353
this._target = null;
5454
this._tag = cc.ACTION_TAG_INVALID;
5555
},
@@ -73,7 +73,7 @@ cc.Action = cc.Class.extend(/** @lends cc.Action# */{
7373
* returns a clone of action
7474
* @return {cc.Action}
7575
*/
76-
clone:function(){
76+
clone:function () {
7777
var action = new cc.Action();
7878
action._originalTarget = null;
7979
action._target = null;
@@ -211,7 +211,7 @@ cc.FiniteTimeAction = cc.Action.extend(/** @lends cc.FiniteTimeAction# */{
211211
//! duration in seconds
212212
_duration:0,
213213

214-
ctor: function () {
214+
ctor:function () {
215215
cc.Action.prototype.ctor.call(this);
216216
this._duration = 0;
217217
},
@@ -244,7 +244,7 @@ cc.FiniteTimeAction = cc.Action.extend(/** @lends cc.FiniteTimeAction# */{
244244
/**
245245
*
246246
*/
247-
clone:function(){
247+
clone:function () {
248248
return new cc.FiniteTimeAction();
249249
}
250250
});
@@ -261,8 +261,8 @@ cc.Speed = cc.Action.extend(/** @lends cc.Speed# */{
261261
_speed:0.0,
262262
_innerAction:null,
263263

264-
ctor:function(){
265-
cc.Action.prototype.ctor.call(this);
264+
ctor:function () {
265+
cc.Action.prototype.ctor.call(this);
266266
this._speed = 0;
267267
this._innerAction = null;
268268
},
@@ -297,7 +297,7 @@ cc.Speed = cc.Action.extend(/** @lends cc.Speed# */{
297297
* returns a clone of action
298298
* @returns {cc.Speed}
299299
*/
300-
clone:function(){
300+
clone:function () {
301301
var action = new cc.Speed();
302302
action.initWithAction(this._innerAction.clone(), this._speed);
303303
return action;
@@ -411,7 +411,7 @@ cc.Follow = cc.Action.extend(/** @lends cc.Follow# */{
411411
bottomBoundary:0.0,
412412
_worldRect:null,
413413

414-
ctor: function () {
414+
ctor:function () {
415415
cc.Action.prototype.ctor.call(this);
416416
this._followedNode = null;
417417
this._boundarySet = false;
@@ -427,7 +427,7 @@ cc.Follow = cc.Action.extend(/** @lends cc.Follow# */{
427427
this._worldRect = cc.RectZero();
428428
},
429429

430-
clone:function(){
430+
clone:function () {
431431
var action = new cc.Follow();
432432
var locRect = this._worldRect;
433433
var rect = new cc.Rect(locRect.x, locRect.y, locRect.width, locRect.height);
@@ -497,17 +497,20 @@ cc.Follow = cc.Action.extend(/** @lends cc.Follow# */{
497497
* @param {Number} dt
498498
*/
499499
step:function (dt) {
500+
var tempPosX = this._followedNode.getPositionX();
501+
var tempPosY = this._followedNode.getPositionY();
502+
tempPosX = this._halfScreenSize.x - tempPosX;
503+
tempPosY = this._halfScreenSize.y - tempPosY;
504+
500505
if (this._boundarySet) {
501506
// whole map fits inside a single screen, no need to modify the position - unless map boundaries are increased
502507
if (this._boundaryFullyCovered)
503508
return;
504509

505-
var tempPos = cc.pSub(this._halfScreenSize, this._followedNode.getPosition());
506-
507-
this._target.setPosition(cc.p(cc.clampf(tempPos.x, this.leftBoundary, this.rightBoundary),
508-
cc.clampf(tempPos.y, this.bottomBoundary, this.topBoundary)));
510+
this._target.setPosition(cc.clampf(tempPosX, this.leftBoundary, this.rightBoundary),
511+
cc.clampf(tempPosY, this.bottomBoundary, this.topBoundary));
509512
} else {
510-
this._target.setPosition(cc.pSub(this._halfScreenSize, this._followedNode.getPosition()));
513+
this._target.setPosition(tempPosX, tempPosY);
511514
}
512515
},
513516

cocos2d/actions/CCActionCatmullRom.js

Lines changed: 50 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
* @param {Number} t
4646
* @return {cc.Point}
4747
*/
48-
cc.CardinalSplineAt = function (p0, p1, p2, p3, tension, t) {
48+
cc.CardinalSplineAt = function (p0, p1, p2, p3, tension, t) {
4949
var t2 = t * t;
5050
var t3 = t2 * t;
5151

@@ -69,17 +69,17 @@
6969
* returns a new copy of the array reversed.
7070
* @return {Array}
7171
*/
72-
cc.reverseControlPoints = function( controlPoints ) {
72+
cc.reverseControlPoints = function (controlPoints) {
7373
var newArray = [];
7474
for (var i = controlPoints.length - 1; i >= 0; i--) {
7575
newArray.push(cc.p(controlPoints[i].x, controlPoints[i].y));
7676
}
7777
return newArray;
7878
};
7979

80-
cc.copyControlPoints = function( controlPoints){
80+
cc.copyControlPoints = function (controlPoints) {
8181
var newArray = [];
82-
for (var i = 0; i< controlPoints.length; i++)
82+
for (var i = 0; i < controlPoints.length; i++)
8383
newArray.push(cc.p(controlPoints[i].x, controlPoints[i].y));
8484
return newArray;
8585
};
@@ -90,8 +90,8 @@ cc.copyControlPoints = function( controlPoints){
9090
* @param {Number} pos
9191
* @return {Array}
9292
*/
93-
cc.getControlPointAt = function( controlPoints, pos ) {
94-
var p = Math.min( controlPoints.length-1, Math.max(pos,0));
93+
cc.getControlPointAt = function (controlPoints, pos) {
94+
var p = Math.min(controlPoints.length - 1, Math.max(pos, 0));
9595
return controlPoints[p];
9696
};
9797

@@ -160,8 +160,8 @@ cc.CardinalSplineTo = cc.ActionInterval.extend(/** @lends cc.CardinalSplineTo# *
160160
* returns a new clone of the action
161161
* @returns {cc.CardinalSplineTo}
162162
*/
163-
clone:function(){
164-
var action = new cc.CardinalSplineTo();
163+
clone:function () {
164+
var action = new cc.CardinalSplineTo();
165165
action.initWithDuration(this._duration, cc.copyControlPoints(this._points), this._tension);
166166
return action;
167167
},
@@ -172,7 +172,7 @@ cc.CardinalSplineTo = cc.ActionInterval.extend(/** @lends cc.CardinalSplineTo# *
172172
startWithTarget:function (target) {
173173
cc.ActionInterval.prototype.startWithTarget.call(this, target);
174174
// Issue #1441 from cocos2d-iphone
175-
this._deltaT = 1 / (this._points.length-1);
175+
this._deltaT = 1 / (this._points.length - 1);
176176

177177
this._previousPosition = this._target.getPosition();
178178
this._accumulatedDiff = cc.p(0, 0);
@@ -183,32 +183,39 @@ cc.CardinalSplineTo = cc.ActionInterval.extend(/** @lends cc.CardinalSplineTo# *
183183
*/
184184
update:function (time) {
185185
var p, lt;
186-
186+
var ps = this._points;
187187
// eg.
188188
// p..p..p..p..p..p..p
189189
// 1..2..3..4..5..6..7
190190
// want p to be 1, 2, 3, 4, 5, 6
191191
if (time == 1) {
192-
p = this._points.length - 1;
192+
p = ps.length - 1;
193193
lt = 1;
194194
} else {
195-
p = 0 | (time / this._deltaT);
196-
lt = (time - this._deltaT * p) / this._deltaT;
195+
var locDT = this._deltaT;
196+
p = 0 | (time / locDT);
197+
lt = (time - locDT * p) / locDT;
197198
}
198199

199200
var newPos = cc.CardinalSplineAt(
200-
cc.getControlPointAt( this._points, p - 1),
201-
cc.getControlPointAt( this._points, p - 0),
202-
cc.getControlPointAt( this._points, p + 1),
203-
cc.getControlPointAt( this._points, p + 2),
201+
cc.getControlPointAt(ps, p - 1),
202+
cc.getControlPointAt(ps, p - 0),
203+
cc.getControlPointAt(ps, p + 1),
204+
cc.getControlPointAt(ps, p + 2),
204205
this._tension, lt);
205206

206-
if(cc.ENABLE_STACKABLE_ACTIONS){
207-
var node = this._target;
208-
var diff = cc.pSub(node.getPosition(), this._previousPosition);
209-
if (diff.x != 0 || diff.y != 0) {
210-
this._accumulatedDiff = cc.pAdd(this._accumulatedDiff, diff);
211-
newPos = cc.pAdd(newPos, this._accumulatedDiff);
207+
if (cc.ENABLE_STACKABLE_ACTIONS) {
208+
var tempX, tempY;
209+
tempX = this._target.getPositionX() - this._previousPosition.x;
210+
tempY = this._target.getPositionY() - this._previousPosition.y;
211+
if (tempX != 0 || tempY != 0) {
212+
var locAccDiff = this._accumulatedDiff;
213+
tempX = locAccDiff.x + tempX;
214+
tempY = locAccDiff.y + tempY;
215+
locAccDiff.x = tempX;
216+
locAccDiff.y = tempY;
217+
newPos.x += tempX;
218+
newPos.y += tempY;
212219
}
213220
}
214221
this.updatePosition(newPos);
@@ -315,20 +322,24 @@ cc.CardinalSplineBy = cc.CardinalSplineTo.extend(/** @lends cc.CardinalSplineBy#
315322
}
316323

317324
// convert to "diffs" to "reverse absolute"
318-
var reverseArray = cc.reverseControlPoints( copyConfig );
325+
var reverseArray = cc.reverseControlPoints(copyConfig);
319326

320327
// 1st element (which should be 0,0) should be here too
321328
p = reverseArray[ reverseArray.length - 1 ];
322329
reverseArray.pop();
323330

324-
p = cc.pNeg(p);
331+
p.x = -p.x;
332+
p.y = -p.y;
333+
325334
reverseArray.unshift(p);
326-
for (i = 1; i < reverseArray.length; ++i) {
335+
for (var i = 1; i < reverseArray.length; ++i) {
327336
current = reverseArray[i];
328-
current = cc.pNeg(current);
329-
var abs = cc.pAdd(current, p);
330-
reverseArray[i] = abs;
331-
p = abs;
337+
current.x = -current.x;
338+
current.y = -current.y;
339+
current.x += p.x;
340+
current.y += p.y;
341+
reverseArray[i] = current;
342+
p = current;
332343
}
333344
return cc.CardinalSplineBy.create(this._duration, reverseArray, this._tension);
334345
},
@@ -338,16 +349,19 @@ cc.CardinalSplineBy = cc.CardinalSplineTo.extend(/** @lends cc.CardinalSplineBy#
338349
* @param {cc.Point} newPos
339350
*/
340351
updatePosition:function (newPos) {
341-
var p = cc.pAdd(newPos, this._startPosition);
342-
this._target.setPosition(p);
343-
this._previousPosition = p;
352+
var pos = this._startPosition;
353+
var posX = newPos.x + pos.x;
354+
var posY = newPos.y + pos.y;
355+
this._target.setPosition(posX, posY);
356+
this._previousPosition.x = posX;
357+
this._previousPosition.y = posY;
344358
},
345359

346360
/**
347361
* returns a new clone of the action
348362
* @returns {cc.CardinalSplineBy}
349363
*/
350-
clone:function(){
364+
clone:function () {
351365
var a = new cc.CardinalSplineBy();
352366
a.initWithDuration(this._duration, cc.copyControlPoints(this._points), this._tension);
353367
return a;
@@ -393,7 +407,7 @@ cc.CatmullRomTo = cc.CardinalSplineTo.extend(/** @lends cc.CatmullRomTo# */{
393407
* returns a new clone of the action
394408
* @returns {cc.CatmullRomTo}
395409
*/
396-
clone:function(){
410+
clone:function () {
397411
var action = new cc.CatmullRomTo();
398412
action.initWithDuration(this._duration, cc.copyControlPoints(this._points));
399413
return action;
@@ -438,7 +452,7 @@ cc.CatmullRomBy = cc.CardinalSplineBy.extend({
438452
* returns a new clone of the action
439453
* @returns {cc.CatmullRomBy}
440454
*/
441-
clone:function(){
455+
clone:function () {
442456
var action = new cc.CatmullRomBy();
443457
action.initWithDuration(this._duration, cc.copyControlPoints(this._points));
444458
return action;

cocos2d/actions/CCActionEase.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -746,19 +746,20 @@ cc.EaseElasticInOut = cc.EaseElastic.extend(/** @lends cc.EaseElasticInOut# */{
746746
*/
747747
update:function (time1) {
748748
var newT = 0;
749+
var locPeriod = this._period
749750
if (time1 === 0 || time1 == 1) {
750751
newT = time1;
751752
} else {
752753
time1 = time1 * 2;
753-
if (!this._period)
754-
this._period = 0.3 * 1.5;
754+
if (!locPeriod)
755+
locPeriod = this._period = 0.3 * 1.5;
755756

756-
var s = this._period / 4;
757+
var s = locPeriod / 4;
757758
time1 = time1 - 1;
758759
if (time1 < 0)
759-
newT = -0.5 * Math.pow(2, 10 * time1) * Math.sin((time1 - s) * Math.PI * 2 / this._period);
760+
newT = -0.5 * Math.pow(2, 10 * time1) * Math.sin((time1 - s) * Math.PI * 2 / locPeriod);
760761
else
761-
newT = Math.pow(2, -10 * time1) * Math.sin((time1 - s) * Math.PI * 2 / this._period) * 0.5 + 1;
762+
newT = Math.pow(2, -10 * time1) * Math.sin((time1 - s) * Math.PI * 2 / locPeriod) * 0.5 + 1;
762763
}
763764
this._inner.update(newT);
764765
},

0 commit comments

Comments
 (0)