defining objects with specific value

hello i am trying to make an object that would have many properties and one of  this properties would be a picture.

i thought about this version:

something = { something1 = 0, somthing2 = 0, something 3 = 0 }

or something like this… something.picture = display.newImage(blablabla)

but i waned to define to this object something   a variable that will be a  picture (sprite) . I know how to add a sprite to a common object. But i dont know how to assign a sprite to a property of an object.

would code like this maybe work:

something = { something1 = 0, somthing2 = 0, something 3 = 0, something4 = display.newsprite (blabla) }

and why not if it doesnt work ?

i would like to basicly create an object. with some properties and then i want to add this object to a table with 320 times 480 places. usinag a for loop.

Any help would be appreciated.

Ty

Jan

Not sure what you’re asking exactly. You mean like have a table, and have a display object as one of its properties? Like this then:

local myTable = {a = 1, b = 2, c = 3} myTable.picture = display.newImage("crate.png")

well the rpoblem i am having is with events,

First i want to create an object with the touch event. I have set it so when i touch the background image the object appers.

Now i want that when i click on the objects that i have created with the previous event, to be changed, so, that there is another sprite  displaying on the same position of that object.

I would be happy if you could just ell me how to make an event touch influence an object that was previously created with an event touch. So the object i want to influence with the touch is already in the screen and was previously created with a touch event.

Ty for help.

[lua]local background = display.newImage(“background.jpg”)
    background.x, background.y = display.contentCenterX, display.contentCenterY
    local tapHandler
    local img
    
    local function changeImage()
        if img.id == “crate” then
            local newImage = display.newImage(“grass.png”)
            newImage.x, newImage.y = img.x, img.y
            – any other properties you want to transfer from the old image to the new one
            img:removeSelf()
            img = newImage
            img.id = “grass”
            img:addEventListener(“tap”, tapHandler)
        elseif img.id == “grass” then
            local newImage = display.newImage(“crate.png”)
            newImage.x, newImage.y = img.x, img.y
            – any other properties you want to transfer from the old image to the new one
            img:removeSelf()
            img = newImage
            img.id = “crate”
            img:addEventListener(“tap”, tapHandler)
        end
    end
    
    function tapHandler(event)
        if event.target == background and not img then
            img = display.newImage(“crate.png”)
            img.x, img.y = 160, 200
            img.id = “crate”
            img:addEventListener(“tap”, tapHandler)
        elseif event.target == img then
            changeImage()
            return true
        end
    end
    background:addEventListener(“tap”, tapHandler)[/lua]

I’m unclear exactly what you are asking, but the above might give you some ideas.

If this is nothing like what you are trying to do, maybe trying explaining it from the users perspective. For example:

“The user starts the app and is presented with a background image. Tapping the background creates a new image in the centre of the screen. Tapping this image changes it into a different image.” etc.

Ty very much i will try to “macgyver” some parts of this code… i guess i wasnt aware i needed to write another function. I thought corona and lua had this already handled through defoult… i guess i was expecting too much…

Thak you very much for the explenaton.

Jan 

Would the code work also if i had a touch event?? 

beacouse i was planing to have a touch event not a tap event.

?

Hmmm,…

actualy… first clikc on the background is suppose to spawn an object (image) at the position of the touch event. and second touch on the spoawneed object is supposed to lanch a sprite at the position of the firs spawned Image (object)

That is what i was trying to do… 

but for some reasoon i cannot apply the addeventlistener (click on  the spawned object)  on the spawned object… becouuse every new spawned object is spawnsed inside some function and the new add event listener thatis suppose to change the image when we clikc on it,  cannot see the image,…

thats my dillema… ??

any ideas?

If you’re referring to the changeImage function, when you say “i wasnt aware i needed to write another function”, you don’t. I just did it that way because I thought it was a bit tidier.

You could do this with a touch listener instead, yes.

If you want to set the position of the first object to the touch position, in your tapHandler, the event table includes x and y values representing the location of the event. So you can put: img.x = event.x, etc.

When you say “launch a sprite”, do you mean play an animation? If that’s why you’re looking to change the image, then you can just use display.newSprite(). In fact, that’s a good solution for changing the image while keeping its location. I should have thought of it before.

As for your current dilemma, I’d need to see your latest code in order to suggest anything.

I will get back… at you… when i try to empower the code i got … it would be nice if there exist a nice version of table where is writen … what each type, , event, … anything represents… that would be amazingly usefull… although the api reference i cool… it would be much easier to just have a table to know what you can mix with what… i was wondering if there exist already a good helper like that…

for example… to know that you can ask is event.target == background… i didnt know u could compare like this…

so basicly… event.target ad background are the same in a way… and they can be compared… it wouldbe nice if there would be a “map” that would show what you can compare and what can be pased from one to another…function or table etc…

event.target == background

is there a 2manual" that shows which “elements” can be used with some events, libraries and types… ???

the most problems i ha ve with scope… and i didnt quite understand why did you put … img:addEventListener(“tap”, tapHandler) at the end of If statements…in

  1. function tapHandler(event)
  2.         if event.target == background and not img then
  3.             img = display.newImage(“crate.png”)
  4.             img.x, img.y = 160, 200
  5.             img.id = “crate”
  6.             img:addEventListener(“tap”, tapHandler)
  7.         elseif event.target == img then
  8.             changeImage()
  9.             return true
  10.         end
  11.     end

 

… ty for the understanding of my uncompetance…

Jan

p.s. my code is rather big… and i dont know how to… simplyfy it in a nice clean version…sorry… .:confused:

event.target and background aren’t really the same. event.target is whatever the target of the event was. When you touch the screen, an event is dispatched to any objects under the touch that are listening for that event. For each object, an event table is created. This table has a number of properties including x, which is the x screen coordinate of the touch, and target which is a reference to the object receiving the particular event. Inside the listener function, you can access these properties, and their values, and use them desired. If you use the same function for different objects, and want to do different things when each is touched, you need to test which object was touched. This is why in the event handler I used:

if event.target == background and not img then -- stuff elseif event.target == img then -- stuff end

I was testing whether the background was being tapped or img was, and could take different steps accordingly. Notice as well that after checking event.target == background, I added  “and not img”. This was just to see whether I had created img already or not, so that I wouldn’t end up creating it again after I tapped multiple times. I could have used two different handlers for background and img, maybe that would have been easier to understand. Also, be aware that what I did is just one way of doing things.

You might like to read this:

https://docs.coronalabs.com/guide/events/detectEvents/index.html

When you say you are having trouble with scope, do you mean this line,

 img:addEventListener("tap", tapHandler)

being inside of tapHandler? Well, functions are allowed to call themselves. It’s quite useful for recursive algorithms.

yeah thats the line… and now i noticed that i didnt understand the NOT notation also…

Not sure what you’re asking exactly. You mean like have a table, and have a display object as one of its properties? Like this then:

local myTable = {a = 1, b = 2, c = 3} myTable.picture = display.newImage("crate.png")

well the rpoblem i am having is with events,

First i want to create an object with the touch event. I have set it so when i touch the background image the object appers.

Now i want that when i click on the objects that i have created with the previous event, to be changed, so, that there is another sprite  displaying on the same position of that object.

I would be happy if you could just ell me how to make an event touch influence an object that was previously created with an event touch. So the object i want to influence with the touch is already in the screen and was previously created with a touch event.

Ty for help.

[lua]local background = display.newImage(“background.jpg”)
    background.x, background.y = display.contentCenterX, display.contentCenterY
    local tapHandler
    local img
    
    local function changeImage()
        if img.id == “crate” then
            local newImage = display.newImage(“grass.png”)
            newImage.x, newImage.y = img.x, img.y
            – any other properties you want to transfer from the old image to the new one
            img:removeSelf()
            img = newImage
            img.id = “grass”
            img:addEventListener(“tap”, tapHandler)
        elseif img.id == “grass” then
            local newImage = display.newImage(“crate.png”)
            newImage.x, newImage.y = img.x, img.y
            – any other properties you want to transfer from the old image to the new one
            img:removeSelf()
            img = newImage
            img.id = “crate”
            img:addEventListener(“tap”, tapHandler)
        end
    end
    
    function tapHandler(event)
        if event.target == background and not img then
            img = display.newImage(“crate.png”)
            img.x, img.y = 160, 200
            img.id = “crate”
            img:addEventListener(“tap”, tapHandler)
        elseif event.target == img then
            changeImage()
            return true
        end
    end
    background:addEventListener(“tap”, tapHandler)[/lua]

I’m unclear exactly what you are asking, but the above might give you some ideas.

If this is nothing like what you are trying to do, maybe trying explaining it from the users perspective. For example:

“The user starts the app and is presented with a background image. Tapping the background creates a new image in the centre of the screen. Tapping this image changes it into a different image.” etc.

Ty very much i will try to “macgyver” some parts of this code… i guess i wasnt aware i needed to write another function. I thought corona and lua had this already handled through defoult… i guess i was expecting too much…

Thak you very much for the explenaton.

Jan 

Would the code work also if i had a touch event?? 

beacouse i was planing to have a touch event not a tap event.

?

Hmmm,…

actualy… first clikc on the background is suppose to spawn an object (image) at the position of the touch event. and second touch on the spoawneed object is supposed to lanch a sprite at the position of the firs spawned Image (object)

That is what i was trying to do… 

but for some reasoon i cannot apply the addeventlistener (click on  the spawned object)  on the spawned object… becouuse every new spawned object is spawnsed inside some function and the new add event listener thatis suppose to change the image when we clikc on it,  cannot see the image,…

thats my dillema… ??

any ideas?

If you’re referring to the changeImage function, when you say “i wasnt aware i needed to write another function”, you don’t. I just did it that way because I thought it was a bit tidier.

You could do this with a touch listener instead, yes.

If you want to set the position of the first object to the touch position, in your tapHandler, the event table includes x and y values representing the location of the event. So you can put: img.x = event.x, etc.

When you say “launch a sprite”, do you mean play an animation? If that’s why you’re looking to change the image, then you can just use display.newSprite(). In fact, that’s a good solution for changing the image while keeping its location. I should have thought of it before.

As for your current dilemma, I’d need to see your latest code in order to suggest anything.

I will get back… at you… when i try to empower the code i got … it would be nice if there exist a nice version of table where is writen … what each type, , event, … anything represents… that would be amazingly usefull… although the api reference i cool… it would be much easier to just have a table to know what you can mix with what… i was wondering if there exist already a good helper like that…

for example… to know that you can ask is event.target == background… i didnt know u could compare like this…

so basicly… event.target ad background are the same in a way… and they can be compared… it wouldbe nice if there would be a “map” that would show what you can compare and what can be pased from one to another…function or table etc…

event.target == background

is there a 2manual" that shows which “elements” can be used with some events, libraries and types… ???

the most problems i ha ve with scope… and i didnt quite understand why did you put … img:addEventListener(“tap”, tapHandler) at the end of If statements…in

  1. function tapHandler(event)
  2.         if event.target == background and not img then
  3.             img = display.newImage(“crate.png”)
  4.             img.x, img.y = 160, 200
  5.             img.id = “crate”
  6.             img:addEventListener(“tap”, tapHandler)
  7.         elseif event.target == img then
  8.             changeImage()
  9.             return true
  10.         end
  11.     end

 

… ty for the understanding of my uncompetance…

Jan