Skip to content

Commit 6432817

Browse files
committed
added: Rope debug, segments, updateAnimation
1 parent 5a35e1a commit 6432817

8 files changed

Lines changed: 81 additions & 236 deletions

File tree

demo/assets/snake.png

-47.4 KB
Binary file not shown.

demo/demo.js

Lines changed: 0 additions & 17 deletions
This file was deleted.

demo/index.html

Lines changed: 0 additions & 186 deletions
This file was deleted.

src/gameobjects/GameObjectCreator.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,9 @@ Phaser.GameObjectCreator.prototype = {
170170
* @param {string|number} frame - If this Rope is using part of a sprite sheet or texture atlas you can specify the exact frame to use by giving a string or numeric index.
171171
* @return {Phaser.Rope} The newly created rope object.
172172
*/
173-
rope: function (x, y, width, height, key, frame) {
173+
rope: function (x, y, key, frame, points) {
174174

175-
return new Phaser.Rope(this.game, x, y, width, height, key, frame);
175+
return new Phaser.Rope(this.game, x, y, key, frame, points);
176176

177177
},
178178

src/gameobjects/GameObjectFactory.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,11 @@ Phaser.GameObjectFactory.prototype = {
213213
* @param {Phaser.Group} [group] - Optional Group to add the object to. If not specified it will be added to the World group.
214214
* @return {Phaser.TileSprite} The newly created tileSprite object.
215215
*/
216-
rope: function (x, y, width, height, key, frame, group) {
216+
rope: function (x, y, key, frame, points, group) {
217217

218218
if (typeof group === 'undefined') { group = this.world; }
219219

220-
return group.add(new Phaser.Rope(this.game, x, y, width, height, key, frame));
220+
return group.add(new Phaser.Rope(this.game, x, y, key, frame, points));
221221

222222
},
223223

src/gameobjects/Rope.js

Lines changed: 61 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,12 @@
1818
* @param {string|Phaser.RenderTexture|Phaser.BitmapData|PIXI.Texture} key - This is the image or texture used by the Rope during rendering. It can be a string which is a reference to the Cache entry, or an instance of a RenderTexture or PIXI.Texture.
1919
* @param {string|number} frame - If this Rope is using part of a sprite sheet or texture atlas you can specify the exact frame to use by giving a string or numeric index.
2020
*/
21-
Phaser.Rope = function (game, x, y, key, frame) {
21+
Phaser.Rope = function (game, x, y, key, frame, points) {
2222

2323
this.points = [];
24-
this.count = 0;
25-
var length = 918/20;
26-
for (var i = 0; i < 20; i++) {
27-
this.points.push(new Phaser.Point(i * length, 0));
28-
}
29-
console.log('points:', this.points);
24+
this.points = points;
25+
this._hasUpdateAnimation = false;
26+
this._updateAnimationCallback = null;
3027
x = x || 0;
3128
y = y || 0;
3229
key = key || null;
@@ -266,13 +263,8 @@ Phaser.Rope.prototype.preUpdate = function() {
266263
* @memberof Phaser.Rope
267264
*/
268265
Phaser.Rope.prototype.update = function() {
269-
270-
this.count += 0.1;
271-
for (var i = 0; i < this.points.length; i++) {
272-
273-
this.points[i].y = Math.sin(i * 0.5 + this.count) * 20;
274-
//this.points[i].x = i * length + Math.cos(i *0.3 + this.count) * 20;
275-
266+
if(this._hasUpdateAnimation) {
267+
this.updateAnimation.call(this);
276268
}
277269

278270
};
@@ -303,20 +295,6 @@ Phaser.Rope.prototype.postUpdate = function() {
303295
}
304296
};
305297

306-
/**
307-
* Sets this Rope to automatically scroll in the given direction until stopped via Rope.stopScroll().
308-
* The scroll speed is specified in pixels per second.
309-
* A negative x value will scroll to the left. A positive x value will scroll to the right.
310-
* A negative y value will scroll up. A positive y value will scroll down.
311-
*
312-
* @method Phaser.Rope#autoScroll
313-
* @memberof Phaser.Rope
314-
*/
315-
Phaser.Rope.prototype.autoScroll = function(x, y) {
316-
317-
this._scroll.set(x, y);
318-
319-
};
320298

321299

322300

@@ -603,7 +581,7 @@ Object.defineProperty(Phaser.Rope.prototype, "frameName", {
603581
});
604582

605583
/**
606-
* An Rope that is fixed to the camera uses its x/y coordinates as offsets from the top left of the camera. These are stored in Rope.cameraOffset.
584+
* A Rope that is fixed to the camera uses its x/y coordinates as offsets from the top left of the camera. These are stored in Rope.cameraOffset.
607585
* Note that the cameraOffset values are in addition to any parent in the display list.
608586
* So if this Rope was in a Group that has x: 200, then this will be added to the cameraOffset.x
609587
*
@@ -775,6 +753,60 @@ Object.defineProperty(Phaser.Rope.prototype, "y", {
775753

776754
});
777755

756+
/**
757+
* A Rope will call it's updateAnimation function on each update loop if it has one
758+
*
759+
* @name Phaser.Rope#updateAnimation
760+
* @property {function} updateAnimation - Set to a function if you'd like the rope to animate during the update phase. Set to false or null to remove it.
761+
*/
762+
Object.defineProperty(Phaser.Rope.prototype, "updateAnimation", {
763+
764+
get: function () {
765+
766+
return this._updateAnimation;
767+
768+
},
769+
770+
set: function (value) {
771+
if(value && typeof value === 'function') {
772+
this._hasUpdateAnimation = true;
773+
this._updateAnimation = value;
774+
} else {
775+
this._hasUpdateAnimation = false;
776+
this._updateAnimation = null;
777+
}
778+
779+
}
780+
781+
});
782+
783+
/**
784+
* The segments that make up the rope body as an array of Phaser.Rectangles
785+
*
786+
* @name Phaser.Rope#segments
787+
* @property {array} updateAnimation - Returns an array of Phaser.Rectangles that represent the segments of the given rope
788+
*/
789+
Object.defineProperty(Phaser.Rope.prototype, "segments", {
790+
get: function() {
791+
var segments = [];
792+
var index, x1, y1, x2, y2, width, height, rect;
793+
for(var i = 0; i < this.points.length; i++) {
794+
index = i * 4;
795+
x1 = this.verticies[index];
796+
y1 = this.verticies[index + 1];
797+
x2 = this.verticies[index + 4];
798+
y2 = this.verticies[index + 3];
799+
width = Phaser.Math.difference(x1,x2);
800+
height = Phaser.Math.difference(y1,y2);
801+
x1 += this.world.x;
802+
y1 += this.world.y;
803+
rect = new Phaser.Rectangle(x1,y1, width, height);
804+
segments.push(rect);
805+
}
806+
return segments;
807+
}
808+
});
809+
778810
/**
779811
* @name Phaser.Rope#destroyPhase
780812
* @property {boolean} destroyPhase - True if this object is currently being destroyed.

src/utils/Debug.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,21 @@ Phaser.Utils.Debug.prototype = {
431431

432432
this.rectangle(bounds, color, filled);
433433

434+
},
435+
/**
436+
* Renders the Rope's segments. Note: This is really expensive as it has to calculate new segments everytime you call it
437+
*
438+
* @method Phaser.Utils.Debug#ropeSegments
439+
* @param {Phaser.Rope} rope - The rope to display the segments of.
440+
* @param {string} [color] - Color of the debug info to be rendered (format is css color string).
441+
* @param {boolean} [filled=true] - Render the rectangle as a fillRect (default, true) or a strokeRect (false)
442+
*/
443+
ropeSegments: function(rope, color, filled) {
444+
var segments = rope.segments;
445+
segments.forEach(function(segment) {
446+
this.rectangle(segment, color, filled);
447+
}, this);
448+
434449
},
435450

436451
/**

tasks/manifests/phaser.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"src/gameobjects/Sprite.js",
4646
"src/gameobjects/Image.js",
4747
"src/gameobjects/TileSprite.js",
48+
"src/gameobjects/Rope.js",
4849
"src/gameobjects/Text.js",
4950
"src/gameobjects/BitmapText.js",
5051
"src/gameobjects/Button.js",

0 commit comments

Comments
 (0)