Skip to content

Commit 5eeaf6b

Browse files
committed
Merge pull request #1211 from dingpinglv/Iss3048_RectClone
Closed #3048: cc.rect can receiving different type parameters now
2 parents 0a43c3a + 27f653c commit 5eeaf6b

File tree

1 file changed

+39
-35
lines changed

1 file changed

+39
-35
lines changed

cocos2d/cocoa/CCGeometry.js

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -190,44 +190,35 @@ cc.sizeEqualToSize = function (size1, size2) {
190190

191191
/**
192192
* @class
193-
* @param {Number} x1
194-
* @param {Number} y1
195-
* @param {Number} width1
196-
* @param {Number} height1
193+
* @param {Number|cc.Point|cc.Rect} [x1] a Number value as x or a cc.Point object as origin or a cc.Rect clone object
194+
* @param {Number|cc.Size} [y1] x1 a Number value as y or a cc.Size object as size
195+
* @param {Number} [width1]
196+
* @param {Number} [height1]
197197
* Constructor
198198
*/
199199
cc.Rect = function (x1, y1, width1, height1) {
200-
switch (arguments.length) {
201-
case 0:
202-
this.origin = cc.p(0, 0);
203-
this.size = cc.size(0, 0);
204-
break;
205-
case 1:
206-
var oldRect = x1;
207-
if (!oldRect) {
208-
this.origin = cc.p(0, 0);
209-
this.size = cc.size(0, 0);
210-
} else {
211-
if (oldRect instanceof cc.Rect) {
212-
this.origin = cc.p(oldRect.origin.x, oldRect.origin.y);
213-
this.size = cc.size(oldRect.size.width, oldRect.size.height);
214-
} else {
215-
throw "unknown argument type";
216-
}
217-
}
218-
break;
219-
case 2:
220-
this.origin = x1 ? cc.p(x1.x, x1.y) : cc.p(0, 0);
221-
this.size = y1 ? cc.size(y1.width, y1.height) : cc.size(0, 0);
222-
break;
223-
case 4:
224-
this.origin = cc.p(x1 || 0, y1 || 0);
225-
this.size = cc.size(width1 || 0, height1 || 0);
226-
break;
227-
default:
228-
throw "unknown argument type";
229-
break;
200+
var argLen =arguments.length;
201+
if(argLen === 4){
202+
this.origin = new cc.Point(x1 || 0, y1 || 0);
203+
this.size = new cc.Size(width1 || 0, height1 || 0);
204+
return;
230205
}
206+
if(argLen === 1) {
207+
this.origin = new cc.Point(x1.origin.x, x1.origin.y);
208+
this.size = new cc.Size(x1.size.width, x1.size.height);
209+
return;
210+
}
211+
if(argLen === 0) {
212+
this.origin = new cc.Point(0, 0);
213+
this.size = new cc.Size(0,0);
214+
return;
215+
}
216+
if(argLen === 2) {
217+
this.origin = new cc.Point(x1.x, x1.y);
218+
this.size = new cc.Size(y1.width,y1.height);
219+
return;
220+
}
221+
throw "unknown argument type";
231222
};
232223

233224
/**
@@ -245,7 +236,20 @@ cc.RectMake = function (x, y, width, height) {
245236

246237
// backward compatible
247238
cc.rect = function (x, y, w, h) {
248-
return new cc.Rect(x,y,w,h);
239+
var argLen =arguments.length;
240+
if(argLen === 0)
241+
return new cc.Rect(0,0,0,0);
242+
243+
if(argLen === 1)
244+
return new cc.Rect(x.x, x.y, x.width, x.height);
245+
246+
if(argLen === 2)
247+
return new cc.Rect(x.x, x.y, y.width, y.height);
248+
249+
if(argLen === 4)
250+
return new cc.Rect(x,y,w,h);
251+
252+
throw "unknown argument type";
249253
};
250254

251255
// JSB compatbility: in JSB, cc._rect reuses objects instead of creating new ones

0 commit comments

Comments
 (0)