Collision detection

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...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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

Does your player have a "kinematic" or "static" physics body?

Only "dynamic" bodies can collide with "kinematic" or "static" bodies.

My player is also Static so thats prob why. I can't have my Player or Star move down though, they have to stay at the same X access instead of falling. How can I take off the static part and keep them from falling off the screen? (I can't turn physics off, its needed for other parts of the app)

If you check out http://techority.com I've recently posted Corona For Newbies Part 4, which has a section on collision detection - it might solve your problem :)

I tried using your code for the collision detection with my Star and player, but it still does nothing when I run through my star with the player sprite.

My code now looks like the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
local Sprite = display.newImage ("Sprite.png")
        Sprite.x = 330
        Sprite.y = 290
        Sprite.itemName = "player"
                groupOne:insert( Sprite )
 
gameState = "Play"
 
        -- Setup Star and Points calling --
-- Display score
        GamePoints.DisplayPoints()
                local StarTime = 0      
 
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")
                        Star:addEventListener( "collision", Star )
                groupOne:insert( Star ) 
function Star:collision (event)
        if event.other.itemName == "player" then
                groupOne:remove( Star )
                        Star = nil
                StarTime = 0
        end
end     
        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

I don't see where the groupOne() display group is defined. Did you leave any code out or is what posted all of it? In your updated code I don't see a function being called when the star is collided with.

I left a whole lot of code out, I only posted the part that had to do with the Star/Sprite and their collision.

Also the collision function is under function Star:collision (event) in the above code

They both are still set as Static so I don't know if thats the only/main reason the collision functions are not working

yes that's why they are not colliding.

My player is also Static so thats prob why. I can't have my Player or Star move down though, they have to stay at the same X access instead of falling. How can I take off the static part and keep them from falling off the screen? (I can't turn physics off, its needed for other parts of the app)

You can try to create invisible horizontal floor and roof which only collide with Player and Star using collision filters

Ahh ok, adding a invisible line to be used as a second floor that only supports the Star worked so that it can now be dynamic without falling.

I also used Peach's physics examples and added names to all of my objects so their collider functions only work with the other objects I want them to instead of with everything. This way my player can gain points when they collide with the star instead of dieing when they collide with anything.

thanks a lot for the help, its all working now :D

views:1617 update:2011/9/29 9:22:17
corona forums © 2003-2011