Adding a collision event / listener.

Hiya,

I'm having an weird time with something here.

I'm trying to add a collision Sensor to an object.
I've created my 'object' and added physics to it and a 'isSensor' to it.

I've then created my collision function.
Simply put:

1
2
3
4
5
6
7
function StarSensor:collision (e)
        if(e.phase == "began") then
                audio.play(btnpress);
                trackStars(self);
        end
        return true;    
end

I've changed it to:

1
2
3
4
5
6
7
8
local function StarSensor(e)
                if(e.phase == "began") then
                        audio.play(btnpress);
                        trackStars(self);
                end
                return true;    
        end
Star1:addEventListener("collision", StarSensor);

You can make any image listen for collision provided it is a physics body - not just newRect or newCircle :)

For your error - was that the exact output? "objSensor"?

Where do you add star1? Were you trying to add a listener to it before it existed?

Peach :)

Sorry the exact error was 'StarSensor'
Which correlates to the name i'm trying to give my :collision function.

I add star1 up top when creating the level.

1
2
3
4
5
6
7
        
local Star1 = Stars:grabSprite("Star01", true, {Anim = {1, 12, 1200, 0}});
        Star1:show(_W/2+56, _H/2+20, 1, "c");
        Star1:playClip("Anim");
        Star1:scale(0.8, 0.8);
        physics.addBody(Star1, "static", {isSensor = true});
        Star1:addEventListener("collision", StarSensor); -- even if this is commented out it still errors StarSensor.
views:1460 update:2011/11/5 17:18:39
corona forums © 2003-2011