In my game I’m spawning randomly big say birds, and as I spawn these birds in different sizes I want to assign each one of them a unique physics body being the same size. So this is my code at the moment:
local function choose1(event)
local choose = math.random(1, 2)
if choose == 1 then
local enemy = display.newImage(“Images/Enemy.png”, math.random(15, 300), -200)
local enemySize = math.random(10, 30)
enemy.yScale= enemySize
enemy.xScale = enemySize
physics.addBody(enemy, { friction=0.5, bounce=0, radius=enemySize})
end
end
timer.performWithDelay(300, choose1, 0)
I tried using scale to achieve my purpose but it won’t work since Scale and Radius are using different measurements. Is there any way I can decide and image’s size by giving it a value with pixels? I tried enemy.ySize / enemy.xSize but it doesn’t work for some reason
Thanks in advance! [import]uid: 14018 topic_id: 5114 reply_id: 305114[/import]
Assuming your image doesn’t have a border of transparent pixels:
Create your display object
Get the width of the object. Assuming a square png file, the radius will be half the width or height. So, this is the value you would use in your physics body constructor if you were NOT scaling the image
Select your random scale multiplier - as mentioned, xScale/yScale of 1 is 100%, so take your random number, divide it by 100, and add 1. That’ll get you a range of 1.1 to 1.3.
Use this value to scale up your image.
Take the radius you determined in step two, above, and multiply it by your random multiplier. Use that in your physics body constructor.
Not tested, but should work.
Darren [import]uid: 1294 topic_id: 5114 reply_id: 17323[/import]