0

Access another class property

Let pipe be a class class Pipe{ constructor(x = camera.x + width, y = player.h * int(random(-5, 0)), w = player.w * int(random(1, 13)), h = (player.h - y) - ((((player.h - y) / player.h) - int(random(1, (((player.h - y) / player.h) - 1)))) * player.h)){ this.x = x this.y = y this.w = w this.h = h } draw(){ image(pipeImg, this.x, this.y, this.w, this.h) if(this.y >= 0){ this.h = player.h-this.y } } } class Spike{ constructor(){ this.x = new Pipe().x this.y = new Pipe().y this.w = new Pipe().w this.h = new Pipe().h } draw(){ rect(this.x, this.y, this.w, this.h) } } What i wanted to do is obtain class Pipe property which are x, y etc. Thereafter, place that in class Spike property. How can i do that? https://code.sololearn.com/WlU5rd9oDq9V/?ref=app

16th Oct 2020, 3:55 AM
Tri Satria [NEW]
Tri Satria [NEW] - avatar
4 odpowiedzi
+ 1
If you use inheritance then you have access to parent class properties directly. https://code.sololearn.com/WOH8SO1yu1nW/?ref=app
16th Oct 2020, 4:24 AM
Raj Chhatrala
Raj Chhatrala - avatar
0
For example: var a = new A() a.x = 10 a.y = 10 But i want var b = new B() b.x = a.x b.y = a.y All the way around in class not literally the code i gave above but same purposes. To access parents class property. your given code concept is allow B to access A property and method as well with extends and super which isnt the goal.
16th Oct 2020, 4:42 AM
Tri Satria [NEW]
Tri Satria [NEW] - avatar
0
If you don't want to use inheritance then only work around is to create new objects of class A which isn't really that good.
16th Oct 2020, 4:57 AM
Raj Chhatrala
Raj Chhatrala - avatar
0
I modified your code and it works. https://code.sololearn.com/WAOrapeuNcEy/?ref=app But after i implemented it, the property became random not identical to its parents and ruined the game.
16th Oct 2020, 5:04 AM
Tri Satria [NEW]
Tri Satria [NEW] - avatar