image generalization

during player’s collision event, check against the other object’s id field, fe:

if (event.other.id == "enemy") then   print("player collided with an enemy") doEnemyCollisionStuff() elseif (event.other.id == "obstacle") then   print("player collided with an obstacle") doObstacleCollisionStuff() else   print("player collided with something that I forgot to assign an id to, oops! what should i do???") doNothingProbably() end

ok so thats one problem solved but i really want it so that say mid.png and stubleft.png are still seperate images but i dont need to type the image name, just have those specific images labeled as “obstacle” sure i could have it be that the image name is just obstacle but then that limits me on the amount of variation for the obstacles, i want multiple images but all of them to be referred to as “obstacle”

Wow, that is some super redundant code. Why not take this…
 

 
and do this instead…

local function newObstacle( img, x, y, params ) group = group or display.currentStage params = params or {} local obj = display.newImage( params.parent or display.currentStage, img, x, y ) obj.xScale = params.xScale or 0.45 obj.yScale = params.yScale or 0.45 obj.id = params.id or "obstacle" return obj end -- then later... ob1 = newObstacle( "TLCorner.png", 201, 112 ) ob2 = newObstacle( "TMidCent.png", 240, 112 ) ob3 = newObstacle( "TRCorner.png", 278, 112 ) ob4 = newObstacle( "StubLeft.png", 160, 158, { yScale = 0.5 } ) ob5 = newObstacle( "mid.png", 201, 157, { yScale = 0.55 } ) ob6 = newObstacle( "mid.png", 240, 157, { yScale = 0.55 } ) ob7 = newObstacle( "mid.png", H, 157, { yScale = 0.55 } )

Then, consider that if you wanted them all to have the same collision listener code you could do this:

local collision( self, event ) -- code for your listener here return false end local function newObstacle( img, x, y, params ) group = group or display.currentStage params = params or {} local obj = display.newImage( params.parent or display.currentStage, img, x, y ) obj.xScale = params.xScale or 0.45 obj.yScale = params.yScale or 0.45 obj.id = params.id or "obstacle" obj.collision = collision obj:addEventListener("collision") return obj end -- then later... ob1 = newObstacle( "TLCorner.png", 201, 112 ) ob2 = newObstacle( "TMidCent.png", 240, 112 ) ob3 = newObstacle( "TRCorner.png", 278, 112 ) ob4 = newObstacle( "StubLeft.png", 160, 158, { yScale = 0.5 } ) ob5 = newObstacle( "mid.png", 201, 157, { yScale = 0.55 } ) ob6 = newObstacle( "mid.png", 240, 157, { yScale = 0.55 } ) ob7 = newObstacle( "mid.png", H, 157, { yScale = 0.55 } )

@zetterlhean,

You might find this code interesting in terms of how to make generation and collision code compact:

https://forums.coronalabs.com/topic/74120-mechanics-in-250-or-less-with-corona-doodle-jump/

Pay particular attention to player.collision() … and createGameObject()

that isnt what i need, i dont care how redundant my code is, im doing what i know and i need the solution for what im asking for not having my code picked apart by people far more skilled than me and using techniques i have no clue how they work, id really appreciate if i can just get what im looking for and not everything else, i just want to put multiple images under ONE name and then i can work on it from there with what i know.

i dont know how to do stages, i dont think that i need local since everything is in one lua file, i dont understand “params” you guys are all taking this way too fast, i cant learn by reading documentations and stuff, i learn by doing, if i get given an example that needs ME to fill in parts and not have someone else do it and show it to me then i can figure it out, watching someone do something does nothing for me, i need to do it myself and im not saying that to help me you need to create a whole coding course for me i just need help with the problem at hand

sorry if i seem aggressive, im not in the greatest mood at the time of writing this but hopefully this clears things up for you guys

Sure, no prob.  I do want to say, that taking a little time refactoring will help.

By examining your code for the redundant parts and removing them you will improve your understanding and I think the important bits will start to be more apparent.   

As well, having less code to review and pore over when debugging and trying to make things work will help reduce stress.

As for the rest, I am having trouble understanding the problem you are outlining.  I will go back and re-read to see if it becomes more clear.

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.

I’m in the same boat as Ed in that I don’t seem to fully understand what it is that you are asking.

I’m just taking a blind guess here at what I think you might be asking.

If you create display objects called ob1, ob2, etc. then the only way that you can refer back to them specifically is by referring to the actual object, e.g. the only way to get the x coordinate or id of ob1 is to use ob1.x or ob1.id, etc.

Commonly, when you are creating a large amount of similar objects, such as tiles, then you create a table. For example, when using your code as you presented it:

local ob = {} ob[1] = display.newImage("TLCorner.png") ob[1].xScale = 0.45 ob[1].yScale = 0.45 ob[1].x = 201 ob[1].y = 112 ob[1].id = "obstacle" ob[2] = display.newImage("TMidCent.png") ob[2].xScale = 0.45 ob[2].yScale = 0.45 ob[2].x = 240 ob[2].y = 112 ob[2].id = "obstacle" -- etc.

With tables, you can refer to objects, like tiles, much easier. As you could check the x coordinate or id of any element in the ob table by using ob[n].x or ob[n].id, where n is a number you want.

In your specific use, if I still remember it correctly, if you have… let’s say X number of rows and Y number of columns, then you could create the tiles in the following loop.
 

local tile = {} local rowCount = 8 local columnCount = 12 for i = 1, rowCount do tile[i] = {} for j = 1, columnCount do tile[i][j] = display.newRect( x, y, width, height ) end end

This would create a table for each row and fill that table with as many tiles as there are columns. This style also allows you to control the size and coordinates of all the tiles from one location. But, with this method, you could refer to any specific tile by using tile[row][column] code, e.g. if you want to refer to tile on row 2, column 3, you’d write tile[2][3].

I hope this answer was at least close to what you were asking :stuck_out_tongue:

 

my teacher said something about having them in a table and then just checking if that name is in the table, how would i do that?

Your teacher?  Why isn’t your teacher helping with this?  

I’m sorry if you’re not getting sufficient help from your teacher, that bites.

… to the question 

The use of the term ‘name’ doesn’t make any sense in this context.  Anything you use as an index in a table must be unique or you are referencing the same position in the table.

So, if you did this:

local objs = {} objs["obstacle"] = display.new...() objs["obstacle"] = display.new...()

You will have created two objects, but only one (the last one created) will be referenced in the table.

Perhaps, your teacher meant or said, ‘reference objects by their ID’.

local objs = {} local tmp = display.new...() objs[tmp] = tmp local tmp = display.new...() objs[tmp] = tmp

The table above has two unique entries, each referencing one of the two created objects.

However, because you used non-numeric indices, you have to iterate over the table like this:

for k,v in pairs( objs ) do ... do some work on the objects here v is the object k is the index end

For a concrete example, lets change the color of all objects in the table to red:

for k,v in pairs( objs ) do v:setFillColor(1,0,0) end

Now, lets apply the ID concept.  Let’s add a few objects and set pickups to yellow and obstacles to red:

local objs = {} -- make two pickup objects local tmp = display.new...() tmp.id = "pickup" objs[tmp] = tmp local tmp = display.new...() tmp.id = "pickup" objs[tmp] = tmp -- make two obstacle objects local tmp = display.new...() tmp.id = "obstacle" objs[tmp] = tmp local tmp = display.new...() tmp.id = "obstacle" objs[tmp] = tmp

later…

for k,v in pairs( objs ) do if( v.id == "pickup" ) then v:setFillColor(1,1,0) elseif( v.id == "obstacle" ) then v:setFillColor(1,0,0) end end

What about removing objects from the table?

I’m going to use the collision code I provided in a prior post and assume that all objects are in the table objs and that objs is visible in the collision listener.

If all that is true, I can do this:

-- somewhere above the listener local score = 0 local objs = {} 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 -- lets increment the score, delete the pickup object, and remove it from the tracking -- table score = score + 1 display.remove(other) objs[other] = nil elseif( other.id == "danger" ) then -- do whatever you are supposed to with dangers here end end return false end player:addEventListener( "collision" )

If this is still not what you want, I suggest we back up a bit.

Tell us clearly and concisely what your game is and the mechanics in the game.  Please try to use clear sentences, distinct paragraphs,  and bullet lists if you can.

Knowing more about your project may help us to help you.

right now i know how to make tables and the stuff i need to do for it, i just dont know how to call to a table say for example, i make the table, now i just need to know how i can check the table for its contents, the rest im sure i can do from there

It really seems that you should ask your teacher. Ed and I have given you plenty of examples on tables already and it seems that none of our suggestions or advice are helping you out. It also seems that a lot of information is lost in translation.

If you want to output a table’s contents, you’d use https://docs.coronalabs.com/tutorial/data/outputTable/index.html, but I already know that this isn’t what you are actually asking, even if you said that you wanted to check the contents.

Since you create the tables yourself, then you already know all of the contents of those tables may have. Also, for clarity sake, display objects in Corona are also lua tables, which means that when you create a display object, they are treated as tables.

So, help us help you. What is it exactly that you want to do with tables?

  1. When you say that you want to check the table for its contents, what do you literally mean?
  • My previous post included examples of how you could refer to a table and get information from there, but apparently that wasn’t what you needed.

2) What kind of information will be in such a table and why do you need to check it?

  • It is important to understand what type of information you will be storing in your tables and the exact use you have for them, i.e. why do you need to check something from the table? Is it to identify if an object is an obstacle? Great, but in what sense and where do you need to detect it? For collisions, or?