Search code examples
javascriptarrayscreatejs

How can I instantiate a circle of objects in Javascript?


I'm trying to build a circle with dynamic scripting in Javascript. Normally, I've been able to do it pretty easily in C# using Math.cos and Math.sin. I'm using EaselJS/CreateJS as my base library, and came up with this:

function BuildTileCircle()
{
    var countNumberSlots = BlockArray.length;
    var radius = 10;
    for (var i = 0; i < countNumberSlots; i++)
    {
        var angle = i * Math.PI * 2 / countNumberSlots;
        console.log(angle);

        var tempTile = new createjs.Sprite(Tiles, 0);
        WorldContainer.addChild(tempTile);

        tempTile.x = Math.cos(angle) * 100;
        tempTile.y = Math.sin(angle) * radius * 100;
    }   
}

It.. SORT of works. The circle it builds however, is highly skewed and not perfectly round. Am I on the right track?


Solution

  • Your x coordinate doesn't depend on the radius. Fix it.