hi everyOne,
I have a gaming requirement in which i need to slice a pizza/cake into different parts depending upon a random generated no between (1-10) ie if 6 is generated i need to cut the cake into 6 different parts.I need to do this dynamicaly(through coding) rather than on time lines.
I am not understanding how to go abt it.Could anyone please help me.
Thanks in advance
Deepthi
You’ll need some basic trigonometry, I think.
Try this:
// where is the centre of the pizza?
var centreX = 150;
var centreY = 150;
// define the radius
var radius:uint = 100;
// create a sprite on which we'll do our drawing
var mySprite:Sprite = new Sprite();
mySprite.graphics.lineStyle(1);
mySprite.graphics.beginFill(0xffffff); // white
// draw the pizza and put it on the stage
mySprite.graphics.drawCircle(centreX,centreY,radius);
mySprite.graphics.endFill();
addChild(mySprite);
// call the drawSlices function
drawSlices(7);
function drawSlices(numberOfSlices:uint):void
{
// cuts the pizza into 'numberOfSlices' pieces
if(numberOfSlices > 1)
{
for (var i:uint = 0; i < numberOfSlices; i++)
{
mySprite.graphics.moveTo(centreX,centreY); // centre of pizza
// use trigonometry to find the point on the circumference of the
// circle to which we draw a straight line
var theta:Number = i*2*Math.PI/numberOfSlices; // angle in radians
mySprite.graphics.lineTo(centreX + radius*Math.sin(theta), centreY + radius*Math.cos(theta));
}
}
}
Hi ,
Thats really a cool bit of code.
Thanks a lot for the code snippet you have provided. It has helped me a lot.
With Regards
Deepthi