Creating containers as objects?

emptythetill's Avatar

emptythetill

24 Jan, 2013 04:12 PM

Hello.

I'm trying to tidy up some code, and wanted to start using objects (new to Javascript, come from C#) but am having a problem.

My code is below, I get no errors but also get no shape. Using alerts I know the instance is being created, but the shape is not showing.

var panelOne = new _container(1520, 750);
panelOne.x = 225;

var panelOneShape = new createjs.Shape();
panelOneShape.graphics.beginFill("rgba(255,255,255,1)").rect(0,0,1520,750);
panelOne.addChild(panelOneShape);

function _container(Width, Height) {
    var _contain = new createjs.Container();
    _contain.width = Width;
    _contain.height = Height;
}
  1. Support Staff 1 Posted by Lanny McNie on 24 Jan, 2013 04:31 PM

    Lanny McNie's Avatar

    A few things:
    1. Your _container function is not returning an instance to _contain. This means that panelOne is null. You also should not use "new" on the function. I show a fixed version below, but would recommend just creating the container, not wrapping it in a function.

    1. You can not set the width or height of a container. All a container is is a grouping of Objects that can be moved/transformed together.

    2. I also do not see is where you add the container to the stage. Is that just omitted from your post?

    Try this instead:

    var panelOne = _container();
    panelOne.x = 225;
    
    var panelOneShape = new createjs.Shape();
    panelOneShape.graphics.beginFill("rgba(255,255,255,1)").rect(0,0,1520,750);
    panelOne.addChild(panelOneShape);
    
    function _container() {
        return new createjs.Container();
    }
    
    // Ensure you add to stage, and update the stage.
    

    Please note that this forum is not intended to provide help with programming in general - only issues with CreateJS. I recommend you check out our documentation and examples, and JavaScript references in general to get a better feel for the language and syntax.

    Cheers.

  2. Lanny McNie closed this discussion on 24 Jan, 2013 04:31 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac