is there any way that i can have multiple images under one name so when i do interactions between objects i can just have it do stuff with a general thing like a player with obstacles, 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?
Yep, you just give them all a common .id field or whatever you want to call it when the objects are spawned. Then in your collision listeners etc. you run different code depending on the .id field i.e enemy, powerup, obstacle.
ok how do i do that?
Display objects are just sophisticated lua tables, therefore you can add custom keys to the object like you would any other lua table.
[lua]
local image1 = display.newRect(0,0, 100, 100)
image.id = “obstacle”
image.name = “Rock”
image.hitPoints = 10000
local image2 = display.newRect(200, 200, 50, 50)
image2.id = “enemy”
image2.name = “Tony the Alien”
image2.hitPoints = 50
local image3 = display.newRect(300, 300, 200, 10)
image3.id = “enemy”
image3.name = “Dave the Snake”
image3.hitPoints = 10
[/lua]
Sure, I’d expect Rock to be tough, but not 200 times tougher than Tony the Alien in terms of hp. That’s a great example, but I think your math is wrong here, unless that Rock happens to also be called Dwayne.
It’s all relative. Tony and Dave have absolutely no stomach for a fight.
Dave prefers origami… sometimes a bit of cross-stitch
doing that creates an error for me
You are seeing an error because nick has a few typos in his sample code. On line 2, he creates “image1”, but then he refers to just “image” on rows 3, 4 and 5.
If you run it locally, you’ll receive an error message. My error message is “main.lua:2: attempt to index global ‘image’ (a nil value)” and simply means that on row 2, there is no “local image”, i.e. it has a nil value. You need to learn some basic debugging instead of just copying the code.
i didnt copy his code, i put what i saw into mine, i dont typically use local since im told they arent necessary for what im doing
fixed it
Well, you may not have copied all of his code, but you copied enough to reproduce nick’s error.
Also, while using locals is not absolutely necessary, I would highly recommend on using them as locals are less risky than globals and they hog far less resources. Or in other words, globals are more prone to bugs, data leaks and will potentially make your game lag more than using locals.
You can read up on https://docs.coronalabs.com/tutorial/basics/globals/index.html.
ok so i have the whole id thing down but now that i think about it how will i call to it without just having to type the name anyways? i want to be able to just check if it falls under “obstacle” and thats when stuff happens
ob1 = display.newImage("TLCorner.png") ob1.xScale = 0.45 ob1.yScale = 0.45 ob1.x = 201 ob1.y = 112 ob1.id = obstacle ob2 = display.newImage("TMidCent.png") ob2.xScale = 0.45 ob2.yScale = 0.45 ob2.x = 240 ob2.y = 112 ob2.id = obstacle ob3 = display.newImage("TRCorner.png") ob3.xScale = 0.45 ob3.yScale = 0.45 ob3.x = 278 ob3.y = 112 ob3.id = obstacle ob4 = display.newImage("StubLeft.png") ob4.xScale = 0.45 ob4.yScale = 0.50 ob4.x = 160 ob4.y = 158 ob4.id = obstacle ob5 = display.newImage("mid.png") ob5.xScale = 0.45 ob5.yScale = 0.55 ob5.x = 201 ob5.y = 157 ob5.id = obstacle ob6 = display.newImage("mid.png") ob6.xScale = 0.45 ob6.yScale = 0.55 ob6.x = 240 ob6.y = 157 ob6.id = obstacle ob7 = display.newImage("mid.png") ob7.xScale = 0.45 ob7.yScale = 0.55 ob7.x = H ob7.y = 157 ob7.id = obstacle
There are numerous ways for doing that, so it depends on what exact use you need.
For instance, are they physics objects and you want to check their id for collision purposes? Then check https://docs.coronalabs.com/guide/physics/collisionDetection/index.html#local-collision-handling.
Or, if you want to know what the object’s id is when it is touched by the player, then check out https://docs.coronalabs.com/api/event/touch/index.html.
Or, if we are talking about that grid movement, you might want to check the nearby tiles ids to see if they are obstacles or not. In this case, a simple if else statement would check something like “if tile[xy].id == “obstacles” then” and so on.
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.