How to trigger this event listener

Hey Dave,

  I fixed the collision problem.The laser beam is a dynamic object and the cows are static objects.The cow is a sprite object declared in another file.

But how do I get the cow to trigger the collision event? 

I can do this in two ways:

  1. Declaring the cow as a global variable,if I choose this option how can I make my for loop to work?

  2. declaring in in a local function,as a local variable,but if I choose the second option it will give me a nil value when I call the seSequence method.

Thank you!

local beam = display.newImageRect("Beam.png" ,90,200) beam.rotation = 90 beam.isVisible = false physics.addBody (beam,"dynamic") beam.gravityScale = 0 beam.isSensor = true beam:setLinearVelocity (0,0) -- This is the first method that I used. local function cows (event) local c = Cow:new() c.x = 100 c.y = math.random (0,500) physics.addBody (c,"static") end local function spawning (event) for i = 0,0 do cows() end end local function onCol (event) if event.phase == "began" then print ("works") end end timer.performWithDelay (10000,spawning,0) Runtime:addEventListener ("collision",onCol)

Return the cow:

[lua]

local function cows (event)
    local c = Cow:new()
    c.x = 100
    c.y = math.random (0,display.contentHeight)
    physics.addBody (c,“static”)
    return c
end

[/lua]

And add this to test:

[lua]

local beam = display.newImageRect(“Beam.png” ,90,200)
beam.rotation = 90
beam.isVisible = false
physics.addBody(beam,“dynamic”)
beam.gravityScale = 0
beam.isSensor = true
beam:setLinearVelocity(0,0)
beam.x, beam.y = display.contentWidth,500

local function fireLaser(event)
    beam.isVisible = true
    beam:setLinearVelocity(-2000,0)
end

Runtime:addEventListener(“touch”, fireLaser)

[/lua]

Touch the screen when there’s a cow at the same height (500) as the laser to see if it works.

BTW, I’m not sure why you have a loop like this: [lua]for i = 0,0 do[/lua]

Dave

Hi Dave,

  I have a different function that activates the beam and the only way this can work is via the collision event.I also tried non physics collision with no luck.

If I create a global variable for example :

var = cows()

and then to run the collision event:

var.collision = onCollision Runtime:addeventListener ("collision",onCollision)

Hope this will help me.I’ll come back with an update.

Thank you,

Bogdan

UPDATE

I manage to fix the collision problem.Thank you for the help.

But still having this issue.

And you can find the setSequence method in cowboys function and onCollisionWithCowboy function.

Thank you!

local function cowboys (event) local e = Cowboy:new() e.x = 48 e.y = math.random (600,800) e.rotation = 90 physics.addBody (e,"static")  e:setSequence ("cowboyWalking") e:play() local removeE = function () display.remove (e) end  transition.to (e,{time = 20000,y = -50,onComplete = removeE}) return e end  local function cowboySpawning (event) for i = 0,0 do  cowboys() end  end  varE = cowboys() local function onCollisionWithCowboy (self,event) if event.phase == "began" then  varE:setSequence ("shooting") varE:play() end  end 

local ve = cowboys() ve.collision = onCollisionWithCowboy Runtime:addEventListener ("collision",ve)

Untitled-10.jpg

As I mentioned before: setSequence() is a method of the SpriteObject.

And it looks like e is not a SpriteObject from this statement:

local e = Cowboy:new()

Hi Dave,

Thanks for the repply.

But how can I make any declared variable a member of the SpriteObject?

I also need to use my declared variable that triggeres an animation (e.g. e:setSequence(“walking”))

local function cowboys (event) local e = Cowboy:new() e.x = 48 e.y = math.random (600,800) e.rotation = 90 physics.addBody (e,"static") local removeE = function () display.remove (e) end transition.to (e,{time = 20000,y = -50,onComplete = removeE}) return e end 

And also on the collision function :

local varE = cowboys() local function onCollisionWithCowboy (self,event) if event.phase == "began" then varE:setSequence ("shooting") varE:play() end end 

And this is the Cowboy.lua fie.:

Cowboy = {} function Cowboy:new() local self = display.newGroup() local sheetData = {width = 70,height = 107,numFrames = 14,sheetContentWidth = 140,sheetContentHeight = 749} local mySheet = graphics.newImageSheet ("cowboy.png",sheetData) local sequenceData ={ {name = "shooting",frames = {9,10,11,12,13,14},time = 1000,loppcount = 1}, {name = "cowboyWalking", start = 1,count = 8,time = 1200,loopcount = 0} } local cowboy = display.newSprite (mySheet,sequenceData) self:insert(cowboy) return self end return Cowboy

Thanks!

The whole trick is how to figure out how to use the :setSequence() method inside those two functions.But how?

Try this:

[lua]

local function cowboys()
    local sp = Cowboy:new()[1]
    sp.x = 48
    sp.y = math.random(600,800)
    sp.rotation = 90
    physics.addBody(sp, “static”)

    local removeE = function()
        display.remove(sp)
    end

    transition.to(sp, {time=20000, y=-50, onComplete=removeE})

    return sp
end

local varE = cowboys()

local function onCollisionWithCowboy(event)
    if event.phase == “began” then
        varE:setSequence(“shooting”)
        varE:play()
    end
end

Runtime:addEventListener(“collision”, onCollisionWithCowboy)
[/lua]

Btw, I’d suggest using a different variable name than “self”, such as:

[lua]Cowboy = {}

function Cowboy:new()
    local grp = display.newGroup()

    local sheetData = {width=70, height=107, numFrames=14, sheetContentWidth=140, sheetContentHeight=749}
    local mySheet = graphics.newImageSheet(“cowboy.png”, sheetData)

    local sequenceData = {
        {name=“shooting”, frames={9,10,11,12,13,14}, time=1000, loopcount=1},
        {name=“cowboyWalking”, start=1, count=8, time=1200, loopcount=0}
    }

    local cowboy = display.newSprite(mySheet, sequenceData)
    grp:insert(cowboy)

    return grp
end

return Cowboy
[/lua]

Hi Dave,

  I tried it and it works only with the first spawned cowboy,and I don’t know why it doesn’t work with the rest of them.Any idea?

Thanks,

Bogdan

Each cowboy object needs to listen to the collision event. So this should do it:

[lua]

local function onCollisionWithCowboy(self, event)
    if event.phase == “began” then
        self:setSequence(“shooting”)
        self:play()
    end
end

local function cowboys()
    local sp = Cowboy:new()[1]
    sp.x = 48
    sp.y = math.random(600,800)
    sp.rotation = 90
    physics.addBody(sp, “static”)

    sp.collision = onCollisionWithCowboy
    sp:addEventListener(“collision”, sp)

    local removeE = function()
        display.remove(sp)
    end

    transition.to(sp, {time=20000, y=-50, onComplete=removeE})

    return sp
end

[/lua]

Thanks mate,but it didn’t worked.

I gues I’ll go ahead and do this the old way,adding each object andtrigger it’s event listeners manually.