How to add physics.addBody to this object

Hi

How would you go about going adding physics.addBody properties to the “star” in this code so I can detect collisions.

display.setStatusBar( display.HiddenStatusBar );
Variables

_W = display.contentWidth; --Returns Screen Width
_H = display.contentHeight; --Returns Screen Height
local starTable = {} – Set up star table
–Set Up Star Table

function initStar()
local star1 = {}
star1.imgpath = “star1.png”; --Set Image Path for Star
star1.movementSpeed = 10000; --Determines the movement speed of star in milliseconds
table.insert(starTable, star1); --Insert Star into starTable

local star2 = {}
star2.imgpath = “star2.png”;
star2.movementSpeed = 12000;
table.insert(starTable, star2);

local star3 = {}
star3.imgpath = “star3.png”;
star3.movementSpeed = 14000;
table.insert(starTable, star3);
end

function getRandomStar()
local temp = starTable[math.random(1, #starTable)]
local randomStar = display.newImage(temp.imgpath)
physics.addBody(randomStar, { isSensor = true } )
randomStar.myName = “star”
randomStar.movementSpeed = temp.movementSpeed;
randomStar.x = math.random(0,_W)
randomStar.y = _H + 50
randomStar.rotation = math.random(0,360)
starMove = transition.to(randomStar, {
time=randomStar.movementSpeed,
y=-45,
onComplete = function(self) self.parent:remove(self); self = nil; end
})
end

local temp = starTable[math.random(1, #starTable)]
Star Image Path

local randomStar = display.newImage(temp.imgpath)
Star Name and Speed

randomStar.myName = “star”
randomStar.movementSpeed = temp.movementSpeed; – in milliseconds
Star Starting Point

randomStar.x = math.random(0,_W)
randomStar.y = _H + 50
randomStar.rotation = math.random(0,360)
Moving the Star

starMove = transition.to(randomStar, {
time=randomStar.movementSpeed,
y=-45,
onComplete = function(self) self.parent:remove(self); self = nil; end
})

function startGame()
starTimer1 = timer.performWithDelay(1700,getRandomStar, 0)
starTimer2 = timer.performWithDelay(2300,getRandomStar, 0)
starTimer3 = timer.performWithDelay(2700,getRandomStar, 0)
end

initStar()
startGame()
the code was found at this url;
http://mobile.tutsplus.com/tutorials/corona/creating-a-scrolling-background-with-corona-sdk/

Any help is much appreciated! [import]uid: 23546 topic_id: 12561 reply_id: 312561[/import]

Hey @UniqDev it’s fairly simple all you have to do is at the top of the main.lua file you start the physics engine by writting this:

local physics = require("physics")  
physics.start()  

also to see the bodies you code change the view mode by also putting this in the top:

physics.setDrawMode( "hybrid" ) -- overlays collision outlines on normal Corona objects  

And now for the stars to be physics bodies…

In the getRandomStar function you type in this

physics.addBody( randomStar )  

below the randomStar.rotation = math.random(0,360) line

-Nick Homme

P.S. As a side note when posting code use the tags so it’s easier to read :slight_smile: [import]uid: 11769 topic_id: 12561 reply_id: 45951[/import]

Thank you so much! [import]uid: 23546 topic_id: 12561 reply_id: 45952[/import]

No problem!

-Nick Homme [import]uid: 11769 topic_id: 12561 reply_id: 45961[/import]

Just saw this post, did you get this from here: http://mobile.tutsplus.com/tutorials/corona/creating-a-scrolling-background-with-corona-sdk/? [import]uid: 14218 topic_id: 12561 reply_id: 45979[/import]

Yes. [import]uid: 23546 topic_id: 12561 reply_id: 45980[/import]

Very cool, I actually wrote that tutorial. I hope to write a few more on mobile tuts. [import]uid: 14218 topic_id: 12561 reply_id: 45996[/import]

That was really helpful. Actually looking forward to incorporate that piece of code in my new game. It was a pretty brilliant way to accomplish that task. Nice coding:) [import]uid: 23546 topic_id: 12561 reply_id: 46004[/import]

Thank you! [import]uid: 14218 topic_id: 12561 reply_id: 46054[/import]