I’m trying to make it so a Star image gets created on my screen and when a player walks into it the star gets deleted and it reloads the timer to spawn a new star.
The player only moves along the X axis and does not move up or down at all. i Have the player and the star on the same Y cords but nothing happens when they collide. The following is the code I’m using for creating the star and for the star collision portion…
local Sprite = display.newImage ("Sprite.png")
Sprite.x = 330
Sprite.y = 290
groupOne:insert( Sprite )
local StarTime = 0
local function onStarCollision( self, event ) -- Delete Star
groupOne:remove( self )
self = nil
StarTime = 0
end
function newStar() -- Create new rocks
if ( gameState == "Play" ) then
local Star = display.newImage ("Star.png")
Star.x = math.random (140 , 470)
Star.y = 290
physics.addBody( Star, "static", { isSensor=true } )
Star.id = "Star"
Star.collision = onStarCollision
Star:addEventListener( "collision", Star )
groupOne:insert( Star )
end
end
local StarSpawn = display.newText( "..", -99, -99, native.systemFont, 1 )
StarSpawn:setTextColor( 0, 0, 0 )
groupOne:insert( StarSpawn )
-- Setup Timers
function StarSpawn:timer( event )
if ( gameState == "Play" ) then
StarTime = event.count
if ( StarTime == 5 ) then -- Create new star
newStar()
end
else
event.count = 0;
end
end
But my player just runs through the star and nothing happens. if I make the star fall from above down onto the player then the collision works fine, but it does not work when they are both at the same height and moving from side to side into each other. [import]uid: 69700 topic_id: 11672 reply_id: 311672[/import]
[import]uid: 52491 topic_id: 11672 reply_id: 42480[/import]
[import]uid: 69700 topic_id: 11672 reply_id: 42549[/import]