Your Original Question
is there any way that i can have multiple images under one name …
Answer: If I take your question at face value, then “No.” That is not a thing. All image files need have unique names on the ‘disk’.
Longer Answer:
Of course I don’t think that is what you meant. What I think you want is to be able to create game objects using any image you want, but to have all of those objects be considered ‘obstacles’, ‘pickups’, or some other category of things.
In other words, I think you want to employ logic in your game that treats objects by their category, and your question is, “How do I group objects by category?”
Use Arbitrary Fields (like id)
This has been pointed out to you and I think you mostly get it.
@nick_sherman ponited out that you can add an arbitrary field to any display object and later check for it.
Ex:
local obj = display.newImage( ... ) obj.id = "obstacle"
In my second-to-last post I quoted your post of this code:
ob1 = display.newImage("TLCorner.png") ob1.xScale = 0.45 ob1.yScale = 0.45 ob1.x = 201 ob1.y = 112 ob1.id = obstacle
I wanted to show that you could simplify it and wanted to show you how the concept could be applied using a builder.
What I didn’t mention was, “Your code is wrong.” Specifically this line:
ob1.id = obstacle
That line, all similar lines should have quotes around the word obstacle.
ob1.id = "obstacle"
Syntax Errors?
Later in the thread, you posted:
Contextually, I don’t know what you mean, but I’m guessing you typed in some code and got errors. We can’t do much to help you with that if we don’t see the code. Still, all you need to solve syntax errors is provided right in the console. Just go to the line specified, or the few lines before that and you should see the source of your syntax error.
Back To The Original Question
… i have multiple obstacle images and i dont want to code each ones interactions with the player, is there a way i can generalize multiple images into one name like obstacle?
Answer: I think what you want here is a collision listener for the player object that knows how to deal with different categories of collided objects.
You would do that like this:
function player.collision( self, event ) local phase = event.phase local other = event.other -- thing player collided with if( phase == "began" ) then if( other.id == "obstacle" ) then -- do whatever you are supposed to with obstacles here elseif( other.id == "pickup" ) then -- do whatever you are supposed to with pickups here elseif( other.id == "danger" ) then -- do whatever you are supposed to with dangers here end end return false end player:addEventListener( "collision" )
That is about all I can get out of all the posts I’ve seen in this thread. I hope this is what you’re looking for.