Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Add eventListener to an Image
Started by stefannrafaa Mar 19 2020 02:51 AM

1 reply to this topic
corona eventlistener image
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

stefannrafaa

[GLOBAL: userInfoPane.html]
stefannrafaa
  • Observer

  • 1 posts
  • Corona SDK

So i started new project with game based psyhics and everything loaded up as it shoud. I am begginer in the corona language and the best way i can learn is through an examples... I want to make the falling box which is this piece of code

-- make a box(off-screen), position it, and rotate slightly
local box = display.newImageRect( "box.png", 40, 40 )
box.x, box.y = 160, -100
box.rotation = 33

to be clickable. Can anyone show me an example where should i add eventListeners or how can i make multiple boxes fall off-screen but without repeating the code... i copyed this code and pasted like 10 times to make 10 boxes fall from the sky but i know thats not correct way to do it.

this is inside `level1.lua` file as i said i created new project with "Physics-Based Game" options



[TOPIC: post.html]
#2

atanasovskyjiri

[GLOBAL: userInfoPane.html]
atanasovskyjiri
  • Observer

  • 11 posts
  • Corona SDK

Hello,

 

Try this example:

local function doSomething ( event ) --this happens when you tap each box
   local thisBox = event.target
   thisBox.x = 160 --return to original x (for example)
end

for i = 1, 10, 1 do --do 10 times
   -- make a box(off-screen), position it, and rotate slightly
   local box = display.newImageRect( "box.png", 40, 40 )
   box.x, box.y = 160, -100
   box.rotation = 33

   box:addEventListener ( "tap", doSomething )
end

Google for Lua (name of language) and "for loops".




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: corona, eventlistener, image