dav74 Posted December 15, 2013 Share Posted December 15, 2013 Hi,I'm trying inhritance (object-oriented programming) with babylonJS (for teaching it to my "advanced" students) :var canvas = document.getElementById("renderCanvas");var time=0;var tabEnnemi=[];var engine = new BABYLON.Engine(canvas, true); var scene = new BABYLON.Scene(engine);var camera = new BABYLON.ArcRotateCamera("cam", Math.PI/2, 0, 50, new BABYLON.Vector3(0,0,0), scene);camera.attachControl(canvas);var light = new BABYLON.PointLight("pointLumineux1", new BABYLON.Vector3(100, 100, 0), scene); var plan=BABYLON.Mesh.CreatePlane("sol",200,scene);plan.rotation.x=Math.PI/2;//class Ennemifunction Ennemi(pt_vie,i){ BABYLON.Mesh.CreateSphere.call(this,"ennemi"+i,20,1,scene) this.pt_vie=pt_vie;}Ennemi.prototype=Object.create(BABYLON.Mesh.prototype)//fin Class Ennemifor (var i=0;i<2;i++){ e=new Ennemi(5,i); console.log(e.isVisible); console.log(e.position); tabEnnemi.push(e);}scene.registerBeforeRender(function(){ time=time+(1/BABYLON.Tools.GetFps());});engine.runRenderLoop(function () { scene.render(); });All is ok for "isVisible" but i've "undifined" with "position" (console.log(e.isVisible) and console.log(e.position))What is wrong in my inheritance ?(I've take a look on file "babylon.mesh.js" (GitHub),but....i didn't find solution)thank you for your help Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted December 15, 2013 Share Posted December 15, 2013 If you want to inherit from mesh you have to do that:function Ennemi(pt_vie,i){ BABYLON.Mesh.call(this,"ennemi"+i,scene) this.pt_vie=pt_vie;} the CreateMesh function is a "static function" so that you cannot call it like an "instance" method Quote Link to comment Share on other sites More sharing options...
dav74 Posted December 15, 2013 Author Share Posted December 15, 2013 ok thank you for your answerWhere can i use the "static function" CreateSphere ? in my constructor "Ennemi" ? is it possible ? Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted December 15, 2013 Share Posted December 15, 2013 Alas no, and this is the same thing as Object oriented language (like C++ or C#).CreateSphere is static and returns a mesh. It does not update the 'this' value Quote Link to comment Share on other sites More sharing options...
dav74 Posted December 15, 2013 Author Share Posted December 15, 2013 ok thank youi found another way, is it the "best" solution ???var canvas = document.getElementById("renderCanvas");var time=0;var tabSph=[]var tabEnnemi=[];var nbrEn=5var engine = new BABYLON.Engine(canvas, true); var scene = new BABYLON.Scene(engine);var camera = new BABYLON.ArcRotateCamera("cam", Math.PI/2, 1.5*Math.PI/4, 50, new BABYLON.Vector3(0,0,0), scene);camera.attachControl(canvas);var light = new BABYLON.PointLight("pointLumineux1", new BABYLON.Vector3(100, 100, 0), scene); var plan=BABYLON.Mesh.CreatePlane("sol",200,scene);plan.rotation.x=Math.PI/2;for (var i=0;i<nbrEn;i++){ tabSph.push(BABYLON.Mesh.CreateSphere("sph"+i,20,2,scene));}function Ennemi(ptsVie,posX,posY,posZ){ this.ptsVie=ptsVie; this.posX=posX; this.posY=posY; this.posZ=posZ; this.vitesse=0.1*Math.random()+0.2;}Ennemi.prototype.NouvPos=function(i){ tabSph[i].position.x=this.posX; tabSph[i].position.y=this.posY; tabSph[i].position.z=this.posZ;}Ennemi.prototype.deplaEn=function(){ if (this.posY<2 || this.posY>15){ this.vitesse=-this.vitesse } this.posY=this.posY+this.vitesse}for (var i=0;i<nbrEn;i++){ tabEnnemi.push(new Ennemi(5,6*i,5,0));}scene.registerBeforeRender(function(){ time=time+(1/BABYLON.Tools.GetFps()); for (var i=0;i<nbrEn;i++){ tabEnnemi[i].NouvPos(i); tabEnnemi[i].deplaEn(); }});engine.runRenderLoop(function () { scene.render(); }); Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted December 16, 2013 Share Posted December 16, 2013 This works but there is no inheritance hereA good example of inheritance can be found in this article (when you create custom material):http://blogs.msdn.com/b/eternalcoding/archive/2013/08/06/babylon-js-creating-a-convincing-world-for-your-game-with-custom-shaders-height-maps-and-skyboxes.aspx Quote Link to comment Share on other sites More sharing options...
dav74 Posted December 16, 2013 Author Share Posted December 16, 2013 thank you for your answer, the inheritance will be in the next example (work in progress) ;-) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.