Need help with Tap event

I need help with a part of my code. I’m just a newbie programmer (started a month ago), and I’m trying to spawn a monster where the user’s tap is (in the background). The monster is supposed to change it’s alpha from 0 to 1 in 5 milliseconds. However, when I run the code, it won’t spawn a monster where I click/tap it! Can someone please help me? Thanks!

(The “local monster” and “local startSpawning” is in the forward references.)

function startSpawning() monster = display.newImage("monster.png") monster.alpha = 0 local function spawnMonster(event) print( "This worked!" ) monster.x = event.x monster.y = event.y transition.to(monster, {time = 5, alpha =0}) end monster : addEventListener("tap", spawnMonster)end

function startSpawning()
        
    monster = display.newImage(“monster.png”)
    monster.alpha = 1 – change the alpha to 1, then it should work
    
    local function spawnMonster(event)
        print( “This worked!” )
        monster.x = event.x
        monster.y = event.y
        transition.to(monster, {time = 5, alpha =0})
    end
    monster : addEventListener(“tap”, spawnMonster)

end

It didn’t.  :frowning:

I test the code myself - it work

but

I didn’t put the thing inside the function startSpawning()

in main.lua, i put

    monster = display.newImage(“monster.png”)
    monster.alpha = 1
    
    local function spawnMonster(event)
        print( “This worked!” )
        monster.x = event.x
        monster.y = event.y
        transition.to(monster, {time = 5, alpha =0})
    end
    monster : addEventListener(“tap”, spawnMonster)

or

can u explain in more detail, what u actually want to do

hey cozymonster,

what you have doesn’t seem right, is this behavior more of what you’re after ? the bigger change is putting the tap handler to watch a background, not the monster.

anyway, i’ve added more structure than you originally had; this is more of how i would code it. hopefully it gives some additional insight.

-- setup background to get taps local background = display.newRect(0,0,display.actualContentWidth,display.actualContentHeight) background:setFillColor( 255, 255, 255 ) background:setReferencePoint(display.TopLeftReferencePoint) background.x, background.y = 0,0 -- reference to our monster image local monster -- tap handler moves monster to the location on which we tapped local function tapHandler(event) print( "This worked!" ) -- setup our "complex" monster movements, contains multiple steps -- show monster local show = function() transition.to(monster, {time=250, alpha=1}) end -- move monster local move = function() monster.x = event.x monster.y = event.y show() -- next step end -- hide monster local hide = function() -- use onComplete to goto next step after we've hidden transition.to(monster, {time=50, alpha=0, onComplete=move}) end hide() -- start our process return true end background:addEventListener("tap", tapHandler) function startSpawning() monster = display.newImage("selectmenu.png") end startSpawning()

function startSpawning()
        
    monster = display.newImage(“monster.png”)
    monster.alpha = 1 – change the alpha to 1, then it should work
    
    local function spawnMonster(event)
        print( “This worked!” )
        monster.x = event.x
        monster.y = event.y
        transition.to(monster, {time = 5, alpha =0})
    end
    monster : addEventListener(“tap”, spawnMonster)

end

It didn’t.  :frowning:

I test the code myself - it work

but

I didn’t put the thing inside the function startSpawning()

in main.lua, i put

    monster = display.newImage(“monster.png”)
    monster.alpha = 1
    
    local function spawnMonster(event)
        print( “This worked!” )
        monster.x = event.x
        monster.y = event.y
        transition.to(monster, {time = 5, alpha =0})
    end
    monster : addEventListener(“tap”, spawnMonster)

or

can u explain in more detail, what u actually want to do

hey cozymonster,

what you have doesn’t seem right, is this behavior more of what you’re after ? the bigger change is putting the tap handler to watch a background, not the monster.

anyway, i’ve added more structure than you originally had; this is more of how i would code it. hopefully it gives some additional insight.

-- setup background to get taps local background = display.newRect(0,0,display.actualContentWidth,display.actualContentHeight) background:setFillColor( 255, 255, 255 ) background:setReferencePoint(display.TopLeftReferencePoint) background.x, background.y = 0,0 -- reference to our monster image local monster -- tap handler moves monster to the location on which we tapped local function tapHandler(event) print( "This worked!" ) -- setup our "complex" monster movements, contains multiple steps -- show monster local show = function() transition.to(monster, {time=250, alpha=1}) end -- move monster local move = function() monster.x = event.x monster.y = event.y show() -- next step end -- hide monster local hide = function() -- use onComplete to goto next step after we've hidden transition.to(monster, {time=50, alpha=0, onComplete=move}) end hide() -- start our process return true end background:addEventListener("tap", tapHandler) function startSpawning() monster = display.newImage("selectmenu.png") end startSpawning()