We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hei there, Sry for the stupid question but I just cant find my mistake...
So I have two Objects in my project (Player1 and Player2), I initialize them in the setup() function with var player1 = new Player1(); . But when I want to call a function from this Object (player1.show() ), there is a error code "player1 is not defined". So I guess my problem is, that I initialized the Objects in a wrong way... Here is my code:
function setup() {
createCanvas(1400, 800);
var player1 = new Player1();
var player2 = new Player2();
}
function draw() {
background(0, 0, 0);
player1.show();
player2.show();
}
Thanks for the help!
Answers
Both player1 & player2 variables are local-scoped inside
function
setup().Declare them globally instead. Then you can access them from anywhere:
If I do that I get an error "Block-scoped declarations (let, const, function, class) not yet supported outside strict mode" ... I honestly dont know what that means :)
This is a duplicate post.
Kf
Instead of let in line 1 try var
I treid that but then I get the error code "Player1 is not defined"
This is a duplicate post. Continue here https://forum.processing.org/two/discussion/20040/initializing-objects#latest
Kf
Never mind my last comment. @koogs closed it. The other post was better written... More details. Anyways, @Lukas post an updated and more complete vers of your code.
Kf
Just dont mind the last part of the code Im not sure yet how to manage whos turn it is. (I commented all of the code for player2 out because I wanted to get it right for player1 and then just copy it for player 2). Here the updated code:
Right I completle forgot that thanks but it doesnt solve my
not sure what this is:
I corrected my post - you need else or something!!!
My problem is, that the Object Player1 is "not defined" so its not initialized (if thats the correct term) proper.
well... when your class is called Player you need
and NOT new Player1(); with 1
But my class is calles Player1() -> capital letter. And im initialicing it with player1 -> with a lowercase letter. So I dont think the mistake is a spelling mistake but I probably used the wrong syntax (?)
the correct words:
Player is a class. From a class many objects player are derived.
The class is the cookie maker, the objects are the cookies.
A object is created from the class.
You can also say an object is instantiated from a class (since each object is an instance of the class).
https://www.processing.org/tutorials/objects/
Does the constructor contain no parameters?
No ... I think I gotta think that whole Object through again
But thanks you really helped me
Player with 1 or without 1?
see http://p5js.org/examples/objects-objects.html
with 1
to be honest, I don't really know p5, more processing.
but the example above should help you
I also know processing better but I want to learn javascript
https://GitHub.com/processing/p5.js/wiki/Processing-transition
https://developer.Mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects