Search code examples
javascripthtmloopcastingcreatejs

How can I use properties of external library object that returned from a function in javascript?


I'm using createjs to make html5 game bu there is a problem in this part of code:

function createGameObj(){
   var text = new createjs.Text("Some Text","16px Arial","white");
   var container = new createjs.Container();
   container.addChild(text);
   return container;
}

I have a function like above. When I call this and get an object that returned from it, I cant use its properties.

function prepareGameStage(){
   var obj = createGameObj(); //It should be typeof createjs.Container when we do typecasting which we get used to see at oop
   obj.x = canvas.width/2; //this doesnt make any errors but also doesnt work. So I cant access "x" property of "createjs.Container" type object.
   stage.addChild(obj); //error occurs here because of undefined type of obj. 
}

I tried to cast obj like this:

obj = Object.create(createjs.Container,createGameObj());

But it didn' work.

Do you guys have any solutions for that?


Solution

  • Ok, I solved it. Just like Bergi said I made a mistake on another statement.My createGameObj was like :

    function createGameObj(){
       var text = new createjs.Text("Some Text","16px Arial","white");
       var container = new createjs.Container();
       container.addChild(text);
       function(event){
          //do some here
          return container;
       }
    }
    

    and it returned the container object to createGameObj function.And Because of createGameObj function haven't return statement it returned 'undefined' as default. I realised it late :).Deleted nested function and solved.