We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I am trying to convert some code and am nearly there but realize I do not understand how javascript and P5.js handles passing in an instance of one class to a method of another class. In this case, I am passing an instance of a paddle into the puck for a pong style game.
` //in the main program we initialize
var puck = new Puck();
var left = new Paddle(true);
var right = new Paddle(false);
//and then call the method like this
puck.checkPaddleLeft(left);
puck.checkPaddleRight(right);
...`
` //in the puck class we have this
function Puck(){
this.x = 600/2; // Place the puck in the middle of the x axis.
this.y = 400/2; // Place the puck in the middle of the y axis.
this.xspeed = 0; // Sets the initial puck speed along the x axis.
this.yspeed = 0; // Sets the initial puck speed along the y axis.
this.r = 12; // Establishes the radius of the puck at 12 pixels.
//reset();
Puck.protoype.checkPaddleLeft = function(p) { // Function to determine collision between the puck and the left paddle.
if (y < p.y + p.h/2 && y > p.y - p.h/2 && this.x - this.r < p.x + p.w/2) {
if (this.x > p.x) {
var diff = y - (p.y - p.h/2);
var rad = radians(45);
var angle = map(diff, 0, p.h, -rad, rad);
this.xspeed = 5 * cos(angle);
this.yspeed = 5 * sin(angle);
this.x = p.x + p.w/2 + this.r;
}
}
}
...}`
When you try to run this, it returns "Cannot set property 'checkPaddleLeft' of undefined" Any assistance on how to structure such calls is appreciated.
Answers
https://Forum.Processing.org/two/discussion/15473/readme-how-to-format-code-and-text
http://JsBeautifier.org/
this.
in Puck::checkPaddleLeft() method.class
to define everything in 1 encapsulated body: *-:)https://Developer.Mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/class
Oh man, this is so frustrating. I kept coming across this particular style for making P5.js classes, so I thought we had to! All the examples you linked to make classes the same way I do in Processing and even C++, so I guess I was just complicating things. Thanks for responding to my poorly formatted question ;)
I will say that you did not answer my essential question, asking if there are issues or ways to pass in an object to a method in another class. I rebuilt my classes per your instructions/examples and the issue still remains - it says the property is undefined.
It's supposedly called progress :P
Note that class is essentially synctatic-sugar to create an object with prototype-inherited 'methods' (my emphasis below):
The reason you'll find most documentation using the 'old' style is browser compatibility. ES6 is not yet fully supported...
I'd also recommend reading You don't know JS - ES class (and the rest of the book) for a warning on the new 'class' keyword; especially if you're expecting JS 'classes' to work like those in other class-based languages.
And if your sketch still returns errors we'll need to see the updated code to determine the problem ;)
According to this Browser compatibility table:
https://Developer.Mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/class#Browser_compatibility
class
implementation available since version 42 (2015-Apr-14).Thus since 2016-Mar-08, keyword
class
has become available for all of those 4 mainstream desktop browser families! :)>-For mobile, we've got these dates for the
class
ES6 feature: B-)You should post your most recent runnable attempt here.
Even better, post it online as well: https://OpenProcessing.org/sketch/create
AFAIK, there are none! Just declare a parameter to hold the object argument, and access it within the method, just like you did at Puck::checkPaddleLeft(p). O:-)
In order to demonstrate better how to do it, I've converted this Processing Java Mode sketch:
http://Studio.ProcessingTogether.com/sp/pad/export/ro.9qPrLGrYGkr2o
To a p5.js sketch: http://Bl.ocks.org/GoSubRoutine/d0b7d3058d84970e83cf8685f8e69777
At method Ball::colliding(c), its parameter c holds a passed Chamber object argument:
c.x, c.y are Chamber properties. While RAD is a Chamber
static
constant.Pretty much works very similarly to its original Java/JS Cross-Mode version: :>
Below's its full code for you to check out here as well: :bz
"index.html":
"sketch.js":
We (a student and I) are working this out on openProcessing already! http://www.openprocessing.org/sketch/473930 Please ignore the comments, they were added by the student as part of the process. Your example is massively helpful, tho I clearly need to read up on why you are using the const { RAD } = ... It looks to me like you are using it to make a new instance, not pass in a reference to an existing object? Thank you again for all of the thoughtful input!
That's called "object destructuring assignment": https://Developer.Mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Object_destructuring
That doesn't instantiate anything. It's merely a shortcut to unbox properties outta objects into variables.
For example,
const { RAD } = Ball;
is exactly equivalent toconst RAD = Ball.RAD;
And
const { RAD } = c.constructor;
is also the same asconst RAD = c.constructor.RAD;
BtW,
c.constructor
is the same asChamber
if a Chamber object argument is passed to method Ball::colliding(c).Therefore
const { RAD } = c.constructor;
is equal toconst { RAD } = Chamber;
And
const { RAD } = Ball;
can be written asconst { RAD } = this.constructor;
.All objects in JS have a property called constructor which points to their constructor function: https://Developer.Mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor