Search code examples
javascriptjquerycreatejs

How to clone an image and put them in random positions


I am attaching an image to my page using create.js. And I want to clone it several times and put each one in random positions. I've used Jquery clone() method but there was no use from it.

function handleComplete(e)
    var _obstacle = new createjs.Bitmap(queue.getResult("obstacle"));
    _obstacle.x= Math.floor((Math.random() * 799) + 1);
    _obstacle.y = Math.floor((Math.random() * 799) + 1);
    stage.addChild(_obstacle);
}

function tickHandler(e) {
    stage.update();
}

Thanks for attention!


Solution

  • @DeBanana is right, there is a method, but it is clone. It will duplicate most EaselJS objects.

    var bmp = otherBmp.clone();
    

    For objects with sub-children, or complex contents, there is a "deep copy" argument.

    var container = otherContainer.clone(); // Just the container object and properties
    var container = otherContainer.clone(true); // Includes children
    

    http://www.createjs.com/docs/easeljs/classes/DisplayObject.html#method_clone

    Here is a simple fiddle using your code: http://jsfiddle.net/md2fx1t4/

    Cheers.