Make a star rotate and change its position and scale when relaunch

Hi,

I’m a beginner. I have a problem. I want to make a star, and this star will rotate by its middle point (a blue circle). 

In addition, I want the position and scale of this star will change randomly when I relaunch.

Now I have written a code that the star can rotate in a fixed position.

Please help me and give me some hints.

Thank you very much.

This is my code:

local physics = require ("physics") physics.start() x1, y1, x2, y2 = 200, 90, 305, 165 local star = display.newLine( x1,y1, 227,165) star:append( x2,y2, 243,216, 265,290, 200,245, 135,290, 157,215, 95,165, 173,165, 200,90 ) star:setStrokeColor( 1, 0, 0, 1 ) star.strokeWidth = 3 star.y = 0 physics.addBody(star,"dynamic") star.rotation = 0 function getDistance() dis = ((x2-x1)^2 + (y2-y1)^2)^0.5 dis = dis/2 pp = dis\*(-1+(5^0.5))/(5-2\*(5^0.5))^0.5 return pp end local pointY = getDistance() local sdot = display.newCircle(0, 0, 10 ) sdot:setFillColor( 0, 0, 1, 1 ) sdot.x = x1 sdot.y = pointY physics.addBody(sdot,"static", {isSensor=true}) local starJoint = physics.newJoint( "pivot", sdot, star, sdot.x, sdot.y ) starJoint.isMotorEnabled = true starJoint.motorSpeed = 40 starJoint.maxMotorTorque = 1000
local physics = require ("physics") physics.start() x1, y1, x2, y2 = 200, 90, 305, 165 vertices = {x1, y1, 227, 165, x2,y2, 243,216, 265,290, 200,245, 135,290, 157,215, 95,165, 173,165} local star = display.newPolygon( 160, 240, vertices) star:setStrokeColor( 1, 0, 0, 1 ) star.strokeWidth = 3 star.fill = {0, 0, 0, 0} star.x = math.random(0, display.contentWidth - star.contentWidth/2) star.y = math.random(0, display.contentHeight - star.contentHeight/2) scale = math.random(25, 100)/100 star:scale(scale, scale) physics.addBody(star,"dynamic") star.rotation = 0 local sdot = display.newCircle(0, 0, 10 ) sdot:setFillColor( 0, 0, 1, 1 ) sdot.x, sdot.y = star:localToContent(0, 0) physics.addBody(sdot,"static", {isSensor=true}) sdot:scale(scale, scale) local starJoint = physics.newJoint( "pivot", sdot, star, sdot.x, sdot.y ) starJoint.isMotorEnabled = true starJoint.motorSpeed = 40 starJoint.maxMotorTorque = 1000

There you go.

I changed the object from a line to a polygon, it was because it’s way too complicated to scale and move. the random integers on line 12, 13 for the position may put the object slightly outside the screen, and I couldn’t fix it. 

local physics = require ("physics") physics.start() x1, y1, x2, y2 = 200, 90, 305, 165 vertices = {x1, y1, 227, 165, x2,y2, 243,216, 265,290, 200,245, 135,290, 157,215, 95,165, 173,165} local star = display.newPolygon( 160, 240, vertices) star:setStrokeColor( 1, 0, 0, 1 ) star.strokeWidth = 3 star.fill = {0, 0, 0, 0} star.x = math.random(0, display.contentWidth - star.contentWidth/2) star.y = math.random(0, display.contentHeight - star.contentHeight/2) scale = math.random(25, 100)/100 star:scale(scale, scale) physics.addBody(star,"dynamic") star.rotation = 0 local sdot = display.newCircle(0, 0, 10 ) sdot:setFillColor( 0, 0, 1, 1 ) sdot.x, sdot.y = star:localToContent(0, 0) physics.addBody(sdot,"static", {isSensor=true}) sdot:scale(scale, scale) local starJoint = physics.newJoint( "pivot", sdot, star, sdot.x, sdot.y ) starJoint.isMotorEnabled = true starJoint.motorSpeed = 40 starJoint.maxMotorTorque = 1000

There you go.

I changed the object from a line to a polygon, it was because it’s way too complicated to scale and move. the random integers on line 12, 13 for the position may put the object slightly outside the screen, and I couldn’t fix it.