We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Today I wanted to create a UI using p5.play, so I made some code for the class:
var testButton;
function uiButton(width, height, xPos, yPos)
{
this.width = width;
this.height = height;
this.x = xPos;
this.y = yPos;
function newSprite() {
this.createSprite(this.xPos, this.yPos, this.width, this.height);
this.setCollider('rectangle', 0, 0, this.width, this.height);
}
}
And some code to create the button:
testButton = new uiButton(100, 50, 100, 200);
testButton.newSprite();
However, this does not work. Is there something that needs to be changed? Would appreciate the help :)
Answers
I just quoted the Objects example in another answer :)
OK - editing to clarify your error: you've nested
this.createSprite()
andthis.setCollider()
underfunction newSprite()
. Assuming createSprite and setCollider are intended as methods ofuiButton
then you should simply remove function newSprite() altogether...Note that object child functions are attached to
this
(and not inside a nested function) - so try this structure:BUT... this is not a good example to follow if you plan to create many instances of the object. In that case you need to look into prototype-based 'methods' - essentially:
Or you can use an ES6 'class' which is essentially just some sugar-coating around the above prototypal inheritance...
Okay, I think a bit more elaboration and corrections on my end would help :)
Looking back at the code, what I had meant was to assign a created sprite to the newly created object. I wanted something like:
As for setting the collider for the sprite, I looked more at the p5.play API docs, and found a function that allows me to set the default collider for the newly created sprite. I imagine that it is used something like this:
Since there are no arguments to this, I assume it's a function inside p5.play, so that leaves me to worry about creating the actual sprite.
So basically, get the name of the object I'm creating and turn it into a sprite. Hope this makes sense. Please don't hesitate to point out any flaws I may have made in my explanation of what I'm trying to do, as I am very new to the concept of OOP.
:)
(PS, I appreciate the information you provided on classes and all that. I will look into them more as I continue using OOP).
this
inside a function refers to the current object which had invoked it:https://Developer.Mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this
https://DmitriPavlutin.com/gentle-explanation-of-this-in-javascript/
window.width
,window.ellipse()
,window.createSprite()
:https://Developer.Mozilla.org/en-US/docs/Web/API/Window/window
http://p5play.MolleIndustria.org/docs/classes/p5.play.html#method-createSprite
http://p5play.MolleIndustria.org/docs/classes/Sprite.html
class
:https://Developer.Mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/class
https://Developer.Mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/constructor
Nice! Would I then create a new UiButton object by doing this?
Or since there are already values in the constructor arguments (if I understand correctly), would it just be:
Those are default values in case they get undefined.
So for just
testButton = new UiButton;
, y = x = 0 & h = w = 1. :-@https://Developer.Mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters
It works! Thanks for sorting out my horrible code GoToLoop, and thanks blindfish for introducing me to more capabilities of OOP :)