From 0018dc22d67a3613e176892af501de4815c79c0e Mon Sep 17 00:00:00 2001 From: Brad Buchanan Date: Fri, 15 Jul 2016 17:19:21 -0700 Subject: [PATCH 01/12] Upgrade ESLint dependency Fixes #93. Update from ">=2.5.3 <2.10.0" to "^3.1.0" which is the latest version. The major breaking change with this update is [dropping support for Node.js < 4](https://github.com/eslint/eslint/blob/master/docs/user-guide/migrating-to-3.0.0.md#dropping-support-for-nodejs--4) which shouldn't be an issue for this repo (as ESLint points out, Node 4 is the current LTS version). I've added the engines field in package.json to document the dependency on Node >= 4. Fixes the one new linting violation caught after the upgrade (and I'm actually surprised this one wasn't caught _before_ it). --- lib/p5.play.js | 4 +++- package.json | 5 ++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/p5.play.js b/lib/p5.play.js index 349f4db..ef17ad2 100644 --- a/lib/p5.play.js +++ b/lib/p5.play.js @@ -128,7 +128,9 @@ var round = p5.prototype.round; * @type {Group} */ -defineLazyP5Property('allSprites', function() { return new Group(); }); +defineLazyP5Property('allSprites', function() { + return new Group(); +}); p5.prototype.spriteUpdate = true; diff --git a/package.json b/package.json index 153a938..732efd0 100644 --- a/package.json +++ b/package.json @@ -4,11 +4,14 @@ "description": "A p5.js library for the creation of games and playthings.", "dependencies": {}, "devDependencies": { - "eslint": ">=2.5.3 <2.10.0", + "eslint": "^3.1.0", "http-server": "^0.9.0", "mocha-phantomjs": "^4.0.1", "yuidocjs": "^0.10.0" }, + "engines": { + "node" : ">=4.0.0" + }, "scripts": { "docs": "yuidoc .", "lint": "eslint lib/** test/unit/** examples/*.js", From 7c4aa2bfa2cdd2fc181bc7f9b46479a80ef8570e Mon Sep 17 00:00:00 2001 From: Brad Buchanan Date: Fri, 15 Jul 2016 17:30:49 -0700 Subject: [PATCH 02/12] Use Node.js 4.x on Travis CI --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 587bd3e..38b4d1c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1 +1,2 @@ language: node_js +node_js: 4 From 8151d9b1d7e4923b07956fd9646036536655d23a Mon Sep 17 00:00:00 2001 From: Paolo Pedercini Date: Tue, 26 Jul 2016 15:20:44 -0400 Subject: [PATCH 03/12] logic change: making drag possible Added a global !mouseIsPressed in addition to !this.mouseIsPressed because when you attempt to drag a sprite you are likely to roll out of its collider setting this.mouseIsPressed to false and triggering the release function. I hope it doesn't break anything. --- lib/p5.play.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/p5.play.js b/lib/p5.play.js index ef17ad2..55aef21 100644 --- a/lib/p5.play.js +++ b/lib/p5.play.js @@ -1369,7 +1369,7 @@ function Sprite(pInst, _x, _y, _w, _h) { else print('Warning: onMousePressed should be a function'); - if(mouseWasPressed && !this.mouseIsPressed && this.onMouseReleased !== undefined) + if(mouseWasPressed && !mouseIsPressed && !this.mouseIsPressed && this.onMouseReleased !== undefined) if(typeof(this.onMouseReleased) === 'function') this.onMouseReleased.call(this, this); else From 1587e251588c51b3c233bd0990c94cb10499a7fe Mon Sep 17 00:00:00 2001 From: Brad Buchanan Date: Fri, 29 Jul 2016 10:47:29 -0700 Subject: [PATCH 04/12] Tell linter about global mouseIsPressed --- lib/p5.play.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/p5.play.js b/lib/p5.play.js index 55aef21..ed2bac2 100644 --- a/lib/p5.play.js +++ b/lib/p5.play.js @@ -3,6 +3,7 @@ p5.play by Paolo Pedercini/molleindustria, 2015 http://molleindustria.org/ */ +/* global mouseIsPressed */ (function(root, factory) { if (typeof define === 'function' && define.amd) From 1afec328fac792d9e59f92d9ffe8ff57dd505937 Mon Sep 17 00:00:00 2001 From: Brad Buchanan Date: Fri, 29 Jul 2016 14:44:28 -0700 Subject: [PATCH 05/12] Get global mouseIsPressed from p5 instance Instead of referring to the global namespace, check pInst.mouseIsPressed so that we find the value in both global mode and instance mode. Remove now-unneeded linting hint. --- lib/p5.play.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/p5.play.js b/lib/p5.play.js index ed2bac2..999ed54 100644 --- a/lib/p5.play.js +++ b/lib/p5.play.js @@ -3,7 +3,6 @@ p5.play by Paolo Pedercini/molleindustria, 2015 http://molleindustria.org/ */ -/* global mouseIsPressed */ (function(root, factory) { if (typeof define === 'function' && define.amd) @@ -1370,7 +1369,7 @@ function Sprite(pInst, _x, _y, _w, _h) { else print('Warning: onMousePressed should be a function'); - if(mouseWasPressed && !mouseIsPressed && !this.mouseIsPressed && this.onMouseReleased !== undefined) + if(mouseWasPressed && !pInst.mouseIsPressed && !this.mouseIsPressed && this.onMouseReleased !== undefined) if(typeof(this.onMouseReleased) === 'function') this.onMouseReleased.call(this, this); else From 2821d612b60db31c5e1e2ebe1557c7eccc28cf67 Mon Sep 17 00:00:00 2001 From: Brad Buchanan Date: Fri, 29 Jul 2016 14:49:06 -0700 Subject: [PATCH 06/12] Add drag to "Mouse Events on Sprites" example --- examples/mouseEvents.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/examples/mouseEvents.js b/examples/mouseEvents.js index 5b1dded..e516859 100755 --- a/examples/mouseEvents.js +++ b/examples/mouseEvents.js @@ -3,6 +3,7 @@ var asterisk; var ghost; +var draggedSprite; function setup() { createCanvas(800, 400); @@ -34,11 +35,17 @@ function setup() { asterisk.onMousePressed = function() { this.changeAnimation('transform'); this.animation.goToFrame(this.animation.getLastFrame()); + if (draggedSprite == null) { + draggedSprite = this; + } }; asterisk.onMouseReleased = function() { this.changeAnimation('transform'); this.animation.goToFrame(0); + if (draggedSprite == this) { + draggedSprite = null; + } }; } @@ -46,6 +53,11 @@ function setup() { function draw() { background(255, 255, 255); + if (draggedSprite != null) { + draggedSprite.position.x = mouseX; + draggedSprite.position.y = mouseY; + } + //if a sprite is mouseActive true I can check if the mouse is over its collider //and if the button is pressed if(ghost.mouseIsOver) From 246c2d6c53442114d417eb8a47246ea1fe6a65bb Mon Sep 17 00:00:00 2001 From: Brad Buchanan Date: Fri, 29 Jul 2016 15:11:58 -0700 Subject: [PATCH 07/12] Add unit test over new behavior Pins "Don't call onMouseReleased on mouse-out event" rule. --- test/unit/sprite.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/test/unit/sprite.js b/test/unit/sprite.js index 6d0c9f6..dfeb220 100644 --- a/test/unit/sprite.js +++ b/test/unit/sprite.js @@ -77,4 +77,31 @@ describe('Sprite', function() { checkDirectionForVelocity([0, 1], 90); }); }); + + describe('mouse events', function() { + it('does not call onMouseReleased on mouse-out if mouse is still down', function() { + var sprite = pInst.createSprite(0, 0, 100, 100); + sprite.onMouseOver = sinon.spy(); + sprite.onMouseOut = sinon.spy(); + sprite.onMousePressed = sinon.spy(); + sprite.onMouseReleased = sinon.spy(); + sprite.update(); + expect(sprite.onMousePressed.called).to.be.false; + expect(sprite.onMouseReleased.called).to.be.false; + + pInst.mouseX = 0; + pInst.mouseY = 0; + pInst.mouseIsPressed = true; + sprite.update(); + expect(sprite.onMousePressed.called).to.be.true; + expect(sprite.onMouseReleased.called).to.be.false; + + pInst.mouseX = 200; + pInst.mouseY = 0; + pInst.mouseIsPressed = true; + sprite.update(); + expect(sprite.onMousePressed.called).to.be.true; + expect(sprite.onMouseReleased.called).to.be.false; + }); + }); }); From ee87101c04203fdd8b3a7718482aee18ec715dba Mon Sep 17 00:00:00 2001 From: Brad Buchanan Date: Fri, 29 Jul 2016 16:08:38 -0700 Subject: [PATCH 08/12] Add tests for all sprite mouse callbacks Refactors existing test and adds obvious tests surrounding it. --- test/unit/sprite.js | 162 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 149 insertions(+), 13 deletions(-) diff --git a/test/unit/sprite.js b/test/unit/sprite.js index dfeb220..9987766 100644 --- a/test/unit/sprite.js +++ b/test/unit/sprite.js @@ -79,29 +79,165 @@ describe('Sprite', function() { }); describe('mouse events', function() { - it('does not call onMouseReleased on mouse-out if mouse is still down', function() { - var sprite = pInst.createSprite(0, 0, 100, 100); + var sprite; + + beforeEach(function() { + // Create a sprite with centered at 50,50 with size 100,100. + // Its default collider picks up anything from 1,1 to 99,99. + sprite = pInst.createSprite(50, 50, 100, 100); sprite.onMouseOver = sinon.spy(); sprite.onMouseOut = sinon.spy(); sprite.onMousePressed = sinon.spy(); sprite.onMouseReleased = sinon.spy(); + }); + + function moveMouseTo(x, y) { + pInst.mouseX = x; + pInst.mouseY = y; sprite.update(); - expect(sprite.onMousePressed.called).to.be.false; - expect(sprite.onMouseReleased.called).to.be.false; + } - pInst.mouseX = 0; - pInst.mouseY = 0; + function moveMouseOver() { + moveMouseTo(1, 1); + } + + function moveMouseOut() { + moveMouseTo(0, 0); + } + + function pressMouse() { pInst.mouseIsPressed = true; sprite.update(); - expect(sprite.onMousePressed.called).to.be.true; - expect(sprite.onMouseReleased.called).to.be.false; + } - pInst.mouseX = 200; - pInst.mouseY = 0; - pInst.mouseIsPressed = true; + function releaseMouse() { + pInst.mouseIsPressed = false; sprite.update(); - expect(sprite.onMousePressed.called).to.be.true; - expect(sprite.onMouseReleased.called).to.be.false; + } + + it('mouseIsOver property represents whether mouse is over collider', function() { + moveMouseTo(0, 0); + expect(sprite.mouseIsOver).to.be.false; + moveMouseTo(1, 1); + expect(sprite.mouseIsOver).to.be.true; + moveMouseTo(99, 99); + expect(sprite.mouseIsOver).to.be.true; + moveMouseTo(100, 100); + expect(sprite.mouseIsOver).to.be.false; + }); + + describe('onMouseOver callback', function() { + it('calls onMouseOver when the mouse enters the sprite collider', function() { + moveMouseOut(); + expect(sprite.onMouseOver.called).to.be.false; + moveMouseOver(); + expect(sprite.onMouseOver.called).to.be.true; + }); + + it('does not call onMouseOver when the mouse moves within the sprite collider', function() { + moveMouseTo(0, 0); + expect(sprite.onMouseOver.callCount).to.equal(0); + moveMouseTo(1, 1); + expect(sprite.onMouseOver.callCount).to.equal(1); + moveMouseTo(2, 2); + expect(sprite.onMouseOver.callCount).to.equal(1); + }); + + it('calls onMouseOver again when the mouse leaves and returns', function() { + moveMouseOut(); + expect(sprite.onMouseOver.callCount).to.equal(0); + moveMouseOver(); + expect(sprite.onMouseOver.callCount).to.equal(1); + moveMouseOut(); + expect(sprite.onMouseOver.callCount).to.equal(1); + moveMouseOver(); + expect(sprite.onMouseOver.callCount).to.equal(2); + }); + }); + + describe('onMouseOut callback', function() { + it('calls onMouseOut when the mouse leaves the sprite collider', function() { + moveMouseOver(); + expect(sprite.onMouseOut.called).to.be.false; + moveMouseOut(); + expect(sprite.onMouseOut.called).to.be.true; + }); + + it('does not call onMouseOut when the mouse moves outside the sprite collider', function() { + moveMouseTo(0, 0); + expect(sprite.onMouseOut.called).to.be.false; + moveMouseTo(0, 1); + expect(sprite.onMouseOut.called).to.be.false; + }); + + it('calls onMouseOut again when the mouse returns and leaves', function() { + moveMouseOver(); + expect(sprite.onMouseOut.callCount).to.equal(0); + moveMouseOut(); + expect(sprite.onMouseOut.callCount).to.equal(1); + moveMouseOver(); + expect(sprite.onMouseOut.callCount).to.equal(1); + moveMouseOut(); + expect(sprite.onMouseOut.callCount).to.equal(2); + }); + }); + + describe('onMousePressed callback', function() { + it('does not call onMousePressed if the mouse was not over the sprite', function() { + expect(sprite.mouseIsOver).to.be.false; + pressMouse(); + expect(sprite.onMousePressed.called).to.be.false; + }); + + it('calls onMousePressed if the mouse was pressed over the sprite', function() { + moveMouseOver(); + pressMouse(); + expect(sprite.onMousePressed.called).to.be.true; + }); + + it('calls onMousePressed if the mouse was pressed outside the sprite then dragged over it', function() { + pressMouse(); + moveMouseOver(); + expect(sprite.onMousePressed.called).to.be.true; + }); + }); + + describe('onMouseReleased callback', function() { + it('does not call onMouseReleased if the mouse was never pressed over the sprite', function() { + expect(sprite.mouseIsOver).to.be.false; + pressMouse(); + releaseMouse(); + expect(sprite.onMouseReleased.called).to.be.false; + }); + + it('calls onMouseReleased if the mouse was pressed and released over the sprite', function() { + moveMouseOver(); + pressMouse(); + releaseMouse(); + expect(sprite.onMouseReleased.called).to.be.true; + }); + + it('calls onMouseReleased if the mouse was pressed, moved over the sprite, and then released', function() { + pressMouse(); + moveMouseOver(); + releaseMouse(); + expect(sprite.onMouseReleased.called).to.be.true; + }); + + it('does not call onMouseReleased on mouse-out if mouse is still down', function() { + pressMouse(); + moveMouseOver(); + moveMouseOut(); + expect(sprite.onMouseReleased.called).to.be.false; + }); + + it('does not call onMouseReleased on release if mouse has left sprite', function() { + moveMouseOver(); + pressMouse(); + moveMouseOut(); + releaseMouse(); + expect(sprite.onMouseReleased.called).to.be.false; + }); }); }); }); From 56afbe92cda0ad54c62d1f38e6b96d2756faf885 Mon Sep 17 00:00:00 2001 From: Caley Brock Date: Mon, 11 Jul 2016 17:17:22 -0700 Subject: [PATCH 09/12] Create internal copy of width and height in Sprite class --- lib/p5.play.js | 80 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 65 insertions(+), 15 deletions(-) diff --git a/lib/p5.play.js b/lib/p5.play.js index 349f4db..ed77774 100644 --- a/lib/p5.play.js +++ b/lib/p5.play.js @@ -1011,6 +1011,32 @@ function Sprite(pInst, _x, _y, _w, _h) { */ this.mouseIsPressed = false; + /** + * Width of the sprite's current image. + * If no images or animations are set it's the width of the + * placeholder rectangle. + * Used internally to make calculations. + * + * @property internalWidth + * @type {Number} + * @default 100 + */ + + this.internalWidth = _w; + + /** + * Height of the sprite's current image. + * If no images or animations are set it's the height of the + * placeholder rectangle. + * Used internally to make calculations. + * + * @property internalHeight + * @type {Number} + * @default 100 + */ + + this.internalHeight = _h; + /** * Width of the sprite's current image. * If no images or animations are set it's the width of the @@ -1020,6 +1046,18 @@ function Sprite(pInst, _x, _y, _w, _h) { * @type {Number} * @default 100 */ + + Object.defineProperty(this, 'width', { + enumerable: true, + configurable: true, + get: function() { + return this.internalWidth; + }, + set: function(value) { + this.internalWidth = value; + } + }); + if(_w === undefined) this.width = 100; else @@ -1034,6 +1072,18 @@ function Sprite(pInst, _x, _y, _w, _h) { * @type {Number} * @default 100 */ + + Object.defineProperty(this, 'height', { + enumerable: true, + configurable: true, + get: function() { + return this.internalHeight; + }, + set: function(value) { + this.internalHeight = value; + } + }); + if(_h === undefined) this.height = 100; else @@ -1048,7 +1098,7 @@ function Sprite(pInst, _x, _y, _w, _h) { * @type {Number} * @default 100 */ - this.originalWidth = this.width; + this.originalWidth = this.internalWidth; /** * Unscaled height of the sprite @@ -1059,7 +1109,7 @@ function Sprite(pInst, _x, _y, _w, _h) { * @type {Number} * @default 100 */ - this.originalHeight = this.height; + this.originalHeight = this.internalHeight; /** * True if the sprite has been removed. @@ -1172,17 +1222,17 @@ function Sprite(pInst, _x, _y, _w, _h) { { this.collider = this.getBoundingBox(); this.colliderType = 'image'; - this.width = animations[currentAnimation].getWidth()*abs(this.scale); - this.height = animations[currentAnimation].getHeight()*abs(this.scale); + this.internalWidth = animations[currentAnimation].getWidth()*abs(this.scale); + this.internalHeight = animations[currentAnimation].getHeight()*abs(this.scale); //quadTree.insert(this); } //update size and collider - if(animations[currentAnimation].frameChanged || this.width === undefined || this.height === undefined) + if(animations[currentAnimation].frameChanged || this.internalWidth === undefined || this.internalHeight === undefined) { //this.collider = this.getBoundingBox(); - this.width = animations[currentAnimation].getWidth()*abs(this.scale); - this.height = animations[currentAnimation].getHeight()*abs(this.scale); + this.internalWidth = animations[currentAnimation].getWidth()*abs(this.scale); + this.internalHeight = animations[currentAnimation].getHeight()*abs(this.scale); } } @@ -1217,11 +1267,11 @@ function Sprite(pInst, _x, _y, _w, _h) { } else if(this.colliderType === 'image') { - this.collider.extents.x = this.width * abs(cos(t)) + - this.height * abs(sin(t)); + this.collider.extents.x = this.internalWidth * abs(cos(t)) + + this.internalHeight * abs(sin(t)); - this.collider.extents.y = this.width * abs(sin(t)) + - this.height * abs(cos(t)); + this.collider.extents.y = this.internalWidth * abs(sin(t)) + + this.internalHeight * abs(cos(t)); } } @@ -1283,8 +1333,8 @@ function Sprite(pInst, _x, _y, _w, _h) { if(animations[currentAnimation] && (animations[currentAnimation].getWidth() !== 1 && animations[currentAnimation].getHeight() !== 1)) { this.collider = this.getBoundingBox(); - this.width = animations[currentAnimation].getWidth()*abs(this.scale); - this.height = animations[currentAnimation].getHeight()*abs(this.scale); + this.internalWidth = animations[currentAnimation].getWidth()*abs(this.scale); + this.internalHeight = animations[currentAnimation].getHeight()*abs(this.scale); //quadTree.insert(this); this.colliderType = 'image'; //print("IMAGE COLLIDER ADDED"); @@ -1296,7 +1346,7 @@ function Sprite(pInst, _x, _y, _w, _h) { } else //get the with and height defined at the creation { - this.collider = new AABB(pInst, this.position, createVector(this.width, this.height)); + this.collider = new AABB(pInst, this.position, createVector(this.internalWidth, this.internalHeight)); //quadTree.insert(this); this.colliderType = 'default'; } @@ -1559,7 +1609,7 @@ function Sprite(pInst, _x, _y, _w, _h) { { noStroke(); fill(this.shapeColor); - rect(0, 0, this.width, this.height); + rect(0, 0, this.internalWidth, this.internalHeight); } }; From 8d856a199cc9e89553994161f53d50e9fd3a9f9e Mon Sep 17 00:00:00 2001 From: Caley Brock Date: Tue, 12 Jul 2016 14:38:39 -0700 Subject: [PATCH 10/12] Update internal name and documentation --- lib/p5.play.js | 60 ++++++++++++++++++++++++-------------------------- 1 file changed, 29 insertions(+), 31 deletions(-) diff --git a/lib/p5.play.js b/lib/p5.play.js index ed77774..e4ec7c5 100644 --- a/lib/p5.play.js +++ b/lib/p5.play.js @@ -1011,31 +1011,31 @@ function Sprite(pInst, _x, _y, _w, _h) { */ this.mouseIsPressed = false; - /** + /* * Width of the sprite's current image. * If no images or animations are set it's the width of the * placeholder rectangle. - * Used internally to make calculations. + * Used internally to make calculations and draw the sprite. * - * @property internalWidth + * @private + * @property _internalWidth * @type {Number} * @default 100 */ + this._internalWidth = _w; - this.internalWidth = _w; - - /** + /* * Height of the sprite's current image. * If no images or animations are set it's the height of the * placeholder rectangle. - * Used internally to make calculations. + * Used internally to make calculations and draw the sprite. * - * @property internalHeight + * @private + * @property _internalHeight * @type {Number} * @default 100 */ - - this.internalHeight = _h; + this._internalHeight = _h; /** * Width of the sprite's current image. @@ -1046,15 +1046,14 @@ function Sprite(pInst, _x, _y, _w, _h) { * @type {Number} * @default 100 */ - Object.defineProperty(this, 'width', { enumerable: true, configurable: true, get: function() { - return this.internalWidth; + return this._internalWidth; }, set: function(value) { - this.internalWidth = value; + this._internalWidth = value; } }); @@ -1072,15 +1071,14 @@ function Sprite(pInst, _x, _y, _w, _h) { * @type {Number} * @default 100 */ - Object.defineProperty(this, 'height', { enumerable: true, configurable: true, get: function() { - return this.internalHeight; + return this._internalHeight; }, set: function(value) { - this.internalHeight = value; + this._internalHeight = value; } }); @@ -1098,7 +1096,7 @@ function Sprite(pInst, _x, _y, _w, _h) { * @type {Number} * @default 100 */ - this.originalWidth = this.internalWidth; + this.originalWidth = this._internalWidth; /** * Unscaled height of the sprite @@ -1109,7 +1107,7 @@ function Sprite(pInst, _x, _y, _w, _h) { * @type {Number} * @default 100 */ - this.originalHeight = this.internalHeight; + this.originalHeight = this._internalHeight; /** * True if the sprite has been removed. @@ -1222,17 +1220,17 @@ function Sprite(pInst, _x, _y, _w, _h) { { this.collider = this.getBoundingBox(); this.colliderType = 'image'; - this.internalWidth = animations[currentAnimation].getWidth()*abs(this.scale); - this.internalHeight = animations[currentAnimation].getHeight()*abs(this.scale); + this._internalWidth = animations[currentAnimation].getWidth()*abs(this.scale); + this._internalHeight = animations[currentAnimation].getHeight()*abs(this.scale); //quadTree.insert(this); } //update size and collider - if(animations[currentAnimation].frameChanged || this.internalWidth === undefined || this.internalHeight === undefined) + if(animations[currentAnimation].frameChanged || this._internalWidth === undefined || this._internalHeight === undefined) { //this.collider = this.getBoundingBox(); - this.internalWidth = animations[currentAnimation].getWidth()*abs(this.scale); - this.internalHeight = animations[currentAnimation].getHeight()*abs(this.scale); + this._internalWidth = animations[currentAnimation].getWidth()*abs(this.scale); + this._internalHeight = animations[currentAnimation].getHeight()*abs(this.scale); } } @@ -1267,11 +1265,11 @@ function Sprite(pInst, _x, _y, _w, _h) { } else if(this.colliderType === 'image') { - this.collider.extents.x = this.internalWidth * abs(cos(t)) + - this.internalHeight * abs(sin(t)); + this.collider.extents.x = this._internalWidth * abs(cos(t)) + + this._internalHeight * abs(sin(t)); - this.collider.extents.y = this.internalWidth * abs(sin(t)) + - this.internalHeight * abs(cos(t)); + this.collider.extents.y = this._internalWidth * abs(sin(t)) + + this._internalHeight * abs(cos(t)); } } @@ -1333,8 +1331,8 @@ function Sprite(pInst, _x, _y, _w, _h) { if(animations[currentAnimation] && (animations[currentAnimation].getWidth() !== 1 && animations[currentAnimation].getHeight() !== 1)) { this.collider = this.getBoundingBox(); - this.internalWidth = animations[currentAnimation].getWidth()*abs(this.scale); - this.internalHeight = animations[currentAnimation].getHeight()*abs(this.scale); + this._internalWidth = animations[currentAnimation].getWidth()*abs(this.scale); + this._internalHeight = animations[currentAnimation].getHeight()*abs(this.scale); //quadTree.insert(this); this.colliderType = 'image'; //print("IMAGE COLLIDER ADDED"); @@ -1346,7 +1344,7 @@ function Sprite(pInst, _x, _y, _w, _h) { } else //get the with and height defined at the creation { - this.collider = new AABB(pInst, this.position, createVector(this.internalWidth, this.internalHeight)); + this.collider = new AABB(pInst, this.position, createVector(this._internalWidth, this._internalHeight)); //quadTree.insert(this); this.colliderType = 'default'; } @@ -1609,7 +1607,7 @@ function Sprite(pInst, _x, _y, _w, _h) { { noStroke(); fill(this.shapeColor); - rect(0, 0, this.internalWidth, this.internalHeight); + rect(0, 0, this._internalWidth, this._internalHeight); } }; From 8e3067d19596115f75387d9a9891b15e63f0f268 Mon Sep 17 00:00:00 2001 From: Caley Brock Date: Fri, 29 Jul 2016 09:54:05 -0700 Subject: [PATCH 11/12] Document an example of modifying width and height --- lib/p5.play.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/p5.play.js b/lib/p5.play.js index e4ec7c5..84cc051 100644 --- a/lib/p5.play.js +++ b/lib/p5.play.js @@ -1037,6 +1037,22 @@ function Sprite(pInst, _x, _y, _w, _h) { */ this._internalHeight = _h; + /* + * _internalWidth and _internalHeight are used for all p5.play + * calculations, but width and height can be exteneded. For example, + * you may want users to always get and set a scaled width: + Object.defineProperty(this, 'width', { + enumerable: true, + configurable: true, + get: function() { + return this._internalWidth * this.scale; + }, + set: function(value) { + this._internalWidth = value / this.scale; + } + }); + */ + /** * Width of the sprite's current image. * If no images or animations are set it's the width of the From e471572760a6320660954a4fd7a7476feedd41f1 Mon Sep 17 00:00:00 2001 From: Caley Brock Date: Fri, 29 Jul 2016 10:32:52 -0700 Subject: [PATCH 12/12] Spelling update --- lib/p5.play.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/p5.play.js b/lib/p5.play.js index 84cc051..9e81280 100644 --- a/lib/p5.play.js +++ b/lib/p5.play.js @@ -1039,7 +1039,7 @@ function Sprite(pInst, _x, _y, _w, _h) { /* * _internalWidth and _internalHeight are used for all p5.play - * calculations, but width and height can be exteneded. For example, + * calculations, but width and height can be extended. For example, * you may want users to always get and set a scaled width: Object.defineProperty(this, 'width', { enumerable: true,