Jump to content

Accessing containing ES6 class from mesh hits by extending mesh.


Nikos123
 Share

Recommended Posts

Hi, I can execute local functions fine but when I do a pick on the scene there is not a way to access the objects that are siblings to the mesh like this.isSelected.

Is the best way to solve this to add the parent class as a new property on the mesh, ie doing:

  constructor(scene, isOwn, isSelected = false) {    this.mesh = BABYLON.Mesh.CreateSphere("sphere1", 8, Common.MEDIUM_UNIT_SIZE, scene);    this.mesh.parentClass = this;

Then I can access the parentClass from the mesh object. Will this work/is it a good idea?

Link to comment
Share on other sites

Oh, I like this comment:

https://github.com/QuantumInformation/Density-Wars/blob/master/lib/gameUnits/Core.ts#L12

:)

 

I think you should first understand TypeScript a bit better in order to fix most of the functions in your class.

I am not sure what you expect to get from "this.select". It is nowhere to be found.

 

self = this is a javascript hack. In ES6 / TypeScript you can use arrow functions, which define the context of the function as its parent context. so:

select(e:ActionEvent) {    self.isSelected = true;    e.meshUnderPointer.showBoundingBox = true;  }

could be:

select = (e:ActionEvent) => {    this.isSelected = true;    e.meshUnderPointer.showBoundingBox = true;  }

This will however still fail, as "isSelected" is also not a member of the class.

To do that you could set it as a member (just like you did with other variables) or use some typescript magic:

//isSelected set to private and is now a member of this classconstructor(scene, private isSelected = false) {//.....}

same thing can be done with public, if you want it to be a public member.

 

TypeScript is truly wonderful. Being a superset of JavaScript means you can simply throw some JS code at it, and it will compile. But the real magic, and where it really helps is when you use it correctly. Read about it  at http://www.typescriptlang.org/Tutorial 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...