Collision score set up

Hi guys Im quiet stuck and could use your expert help. I cannot figure out how to set up a score system where a ship collides with a star and it increments your score by 1 to get to the required amount.

So far I have set up the vairiable for score and its textfield

I know this is the first part to it but could anyone help me in providing the right code for making it link up with the stars collision with the ships collision?

your help is much appreciated guys.
thanks

[code]score = display.newImage(‘score.png’, 126, 0)

scoreN = display.newText(‘0’, 187, 5, native.systemFont, 15)
socreN:setTextColor(255, 255, 255) [code]

[import]uid: 220607 topic_id: 37160 reply_id: 67160[/import]

Set up your ship and stars with physics and “collision listeners”
[lua]
ship = display.newRect( 120,120, 20, 20)
ship.name = “ship”
ship:setReferencePoint(display.BottomLeftReferencePoint)
ship:setFillColor(200,0,0)
physics.addBody( ship, “dynamic”, { density=1, bounce = 1, friction = 1 } )

star = display.newRect( 220,220, 20, 20)
star.name = “star”
star:setReferencePoint(display.BottomLeftReferencePoint)
star:setFillColor(0,200,0)
physics.addBody(star, “dynamic”, { density=1, bounce = 1, friction = 1 } )

–function for collision
function ship:collision (event)
if (event.other.name == ‘star’) then
–removes star
event.other:removeSelf()
event.other = nil
–increases score
score=score+1
print(‘collision successful’)
end
end
–event listener
ship:addEventListener(“collision”,ship)

[/lua]

You will need to add some physics(gravity), and also how do you plan on moving your ship or stars around because right now they will not collide because they are stagnant.
[import]uid: 75779 topic_id: 37160 reply_id: 145415[/import]

Hey russm305 thanks for the help. The game is actually a side scroller with only the ship stagnant.
Sorry I did not mention that, my bad.
The scroller is working, just needed this part. Thanks for the code example, I’ll try this one out. [import]uid: 220607 topic_id: 37160 reply_id: 145419[/import]

Hey I tried the code above and, although it worked and the score increments by 1 per star, there seems to be a new problem. What happens now is the star is forcing the ship to bounce and lose its gravity when collision is detected. Its like all the ships functions are deactivated once collision with the star is made and just falls.

Any ideas whats wrong. [import]uid: 220607 topic_id: 37160 reply_id: 145648[/import]

Set up your ship and stars with physics and “collision listeners”
[lua]
ship = display.newRect( 120,120, 20, 20)
ship.name = “ship”
ship:setReferencePoint(display.BottomLeftReferencePoint)
ship:setFillColor(200,0,0)
physics.addBody( ship, “dynamic”, { density=1, bounce = 1, friction = 1 } )

star = display.newRect( 220,220, 20, 20)
star.name = “star”
star:setReferencePoint(display.BottomLeftReferencePoint)
star:setFillColor(0,200,0)
physics.addBody(star, “dynamic”, { density=1, bounce = 1, friction = 1 } )

–function for collision
function ship:collision (event)
if (event.other.name == ‘star’) then
–removes star
event.other:removeSelf()
event.other = nil
–increases score
score=score+1
print(‘collision successful’)
end
end
–event listener
ship:addEventListener(“collision”,ship)

[/lua]

You will need to add some physics(gravity), and also how do you plan on moving your ship or stars around because right now they will not collide because they are stagnant.
[import]uid: 75779 topic_id: 37160 reply_id: 145415[/import]

Hey russm305 thanks for the help. The game is actually a side scroller with only the ship stagnant.
Sorry I did not mention that, my bad.
The scroller is working, just needed this part. Thanks for the code example, I’ll try this one out. [import]uid: 220607 topic_id: 37160 reply_id: 145419[/import]

Hey I tried the code above and, although it worked and the score increments by 1 per star, there seems to be a new problem. What happens now is the star is forcing the ship to bounce and lose its gravity when collision is detected. Its like all the ships functions are deactivated once collision with the star is made and just falls.

Any ideas whats wrong. [import]uid: 220607 topic_id: 37160 reply_id: 145648[/import]