For loop with objects

Hello everyone, i saw in the forum some people that create post and had the same doubt but not equivalent on all aspects with my doubt, i am trying to make a icon that when i touch him is created a block, my for loop is only 1 to 3, but the blocks was created all three at the same time, and i need one to one.

and the other doubt is when the object collides with remover object i need to make like feed me oil, example:

  • i have 3 blocks on the remover icon, and i touch on the remover icon and it will create a block, and the remover icon will have only 2 blocks because 1 was been created, and when the created block collides with remover icon, the block is removed and the remover icon will have 3 blocks again :slight_smile:

I have all the code, only need the for loop to create only X objects and to when the object collides with remover icon, the remover icon stay with one more object.

algumas dicas ou ajudas para resolver este passo?

Thanks again corona community

[code]
–Objects bar
local spawner = display.newRect( 50, 5, 40, 40 )
spawner:setFillColor(150, 0, 0)
physics.addBody(spawner, “static”, {isSensor = true})
spawner:addEventListener(“collision”, spawner)

local spawner2 = display.newRect( 100, 5, 40, 40 )
spawner2:setFillColor(150, 0, 0)
physics.addBody(spawner2, “static”, {isSensor = true})
spawner2:addEventListener(“collision”, spawner)
–remove function
function spawner:collision (event)
event.other:removeSelf()
end

–Objects and Drag
local function spawnMyObject (event)
if event.phase == “began” then

–objects and physics
local block1 = {}
for j = 1,3 do
block1[j] = display.newImage(“block.png”, 250, 300)
physics.addBody(block1[j], “kinematic”, { density=2, friction=0, bounce=0, shape=barril })
block1[j].isFixedRotation = true
local function startDrag( event )
local t = event.target
local phase = event.phase
if “began” == phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true

– Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y

– make the bodys dynamic without gravity

–physics.setGravity (0,0)

– Stop current motion, if any
event.target:setLinearVelocity( 0, 0 )
event.target.angularVelocity = 0

elseif t.isFocus then
if “moved” == phase then
t.x = event.x - t.x0
t.y = event.y - t.y0

event.target.bodyType = “dynamic”

elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false

– Switch body type to kinematic, to avoid gravity
if ( not event.target.isPlatform ) then
event.target.bodyType = “kinematic”

end

end
end

–return true

end

block1[j]:addEventListener(“touch”, startDrag)
end

end

end

spawner:addEventListener(“touch”, spawnMyObject)
spawner2:addEventListener(“touch”, spawnMyObject)
[/code] [import]uid: 26056 topic_id: 15519 reply_id: 315519[/import]

Hey there,

I haven’t actually played feed me oil, but is what you are trying to do have a block that spawns new blocks, but only up to 3 blocks at a time? I just want to make sure I understand correctly.

If that is your goal, could you use something like local blocks = 0 then when you spawn, do;
[lua]if blocks <= 3 then
–spawn here
–blocks = blocks + 1[/lua]?

Then you could do blocks = blocks-1 when one is removed. [import]uid: 52491 topic_id: 15519 reply_id: 57378[/import]

it is what is happening, when i only touch one time on the button he create 3 blocks and i only need to create 1 block per touch on the button, and when have a icon that it is the blocks creator, and i am working on that: the icon creator have 3 objects and when he create one object the icon image er example have one image that say, this object have 3 blocks, when he create one object the image change to per example " this icon have 2 blocks" is that that i need :slight_smile:
i will start now working on that [import]uid: 26056 topic_id: 15519 reply_id: 57384[/import]

i need to have something like it:

local icon\_object = {} --\> object icon that when touched, create the respective icon to game area  
local max\_objects = 3 --\> maximum of objects, cant create more when touch on icon object  
icon\_object[1] = display.newImage("icon\_object1.png")  
icon\_object[2] = display.newImage("icon\_object2.png")--\> if i dont touch on the icon\_object, he dont create any block so the icon object have the image 3 that means that exist 3 objects available to be created  
icon\_object[3] = display.newImage("icon\_object3.png")  

and now i need to make it:
if i click on the icon_object he will create the block, and the image of icon_object will be updated to icon_object[2] = display.newImage(“icon_object2.png” because i created one object and only have 2 remaining.
and need the opposite too, if i had created a object i only have 2 remaining but if i drag the object to the icon_object, the block will be removed and i am again with 3 objects available and the image is update to icon_object[3] = display.newImage(“icon_object3.png”)

what should i use to make it? for loop? if statement?

the other code with draggable objects and remover icons it on the top of topic [import]uid: 26056 topic_id: 15519 reply_id: 57411[/import]

For changing the icons, you need to remove the old one (or hide it) and show the new one. Alternatively, you could use a sprite sheet containing an image for each icon, 1, 2 and 3 - then set the frame depending on how many blocks have been spawned.

For the loop, is there a reason you’re using a loop rather than just spawning an object normally as I discussed above? It seems like it would be far easier to simply spawn a block if blocksCount <= 3.

If you count how many blocks are spawned and only spawn more if there are less than 3 that would keep it simple and clean.

Peach :slight_smile: [import]uid: 52491 topic_id: 15519 reply_id: 57455[/import]

yes i will try to make it, but when i use if blocksCount <=3 when i touch on the button that will create the block he creates 3 blocks at the same time, because i have
blocksCount=0
if blocksCount <=3

blocksCount = blocksCount +1
and he dont create only one block per touch on the button, create 2/3 blocks and if i touch again he create again more 2/3 blocks

P.S: Know some example that use sprite sheet with loop and other codes?to maybe help me because i never use it [import]uid: 26056 topic_id: 15519 reply_id: 57463[/import]

Try this code, it’s mostly your own, few changes, little rough but stops your loop with multiple spawns;

[lua]-- Set up physics
require ( “physics” )
physics.start()
physics.setGravity( 0, 0 )

local block1 = {}

–Objects bar
local spawner = display.newRect( 50, 5, 40, 40 )
spawner:setFillColor(150, 0, 0)
physics.addBody(spawner, “static”, {isSensor = true})
spawner:addEventListener(“collision”, spawner)

local spawner2 = display.newRect( 100, 5, 40, 40 )
spawner2:setFillColor(150, 0, 0)
physics.addBody(spawner2, “static”, {isSensor = true})
spawner2:addEventListener(“collision”, spawner)

–remove function
function spawner:collision (event)
event.other:removeSelf()
end

–Objects and Drag
local function spawnMyObject (event)
if event.phase == “began” then

–objects and physics
if #block1 < 3 then
block1[#block1+1] = display.newImage(“block.png”, 250, 300)
physics.addBody(block1[#block1], “kinematic”, { density=2, friction=0, bounce=0, shape=barril })
block1[#block1].isFixedRotation = true
local function startDrag( event )
local t = event.target
local phase = event.phase
if “began” == phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true

– Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y

– make the bodys dynamic without gravity

–physics.setGravity (0,0)

– Stop current motion, if any
event.target:setLinearVelocity( 0, 0 )
event.target.angularVelocity = 0

elseif t.isFocus then
if “moved” == phase then
t.x = event.x - t.x0
t.y = event.y - t.y0

event.target.bodyType = “dynamic”

elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false

– Switch body type to kinematic, to avoid gravity
if ( not event.target.isPlatform ) then
event.target.bodyType = “kinematic”

end

end
end

–return true

end

block1[#block1]:addEventListener(“touch”, startDrag)
end

end

end

spawner:addEventListener(“touch”, spawnMyObject)
spawner2:addEventListener(“touch”, spawnMyObject)[/lua] [import]uid: 52491 topic_id: 15519 reply_id: 57571[/import]

yes it works good, now i only need to complete the code because if the object is removed i ca create other object, but peach help me with this doubt because is very importante plz
i need to update my variable value see that.
see this code and will understand my doubt

  
local active = 0-----------------\>i need to update this value, because when i touch on the ball the value change to one supposedly, but we dont change  
local hello = function()  
 function ball1:touch( event )  
 if(event.phase == "ended") then  
 event.target.bodyType = "dynamic"  
  
 physics.setGravity (0,9.8)  
 active = 1 --------------------------\> when touch on the ball, active is equal to one but dont update and remains equal to zero  
 end  
end  
   
ball1:addEventListener( "touch", ball1 )  
  
end  
  
--very code...........  
..............  
..............  
-- and now i need to know if active is = 0 or = 1 to apply my definitions  
  
hello()  
 if active == 0 then  
 spawner:removeEventListener("touch", spawnMyObject)  
 block1:removeEventListener("touch", startDrag)  
 else   
 block1:addEventListener("touch", startDrag)  
 spawner:addEventListener("touch", spawnMyObject)  
  
 end  
  

[import]uid: 26056 topic_id: 15519 reply_id: 57578[/import]

I know why it isnt working well. because I have:

Local blockCount = 0  
if blockCount \<= 3 then  
.....  
end  
blockCount = blockCount + 1   

the problem is because the variable blockCount isnt updated, it’s always 0. because if I change the variable blockCount to 4 he does not allow to create blocks, how can I update the variable to make the script so well?

this is another small piece of code that also need to know to update the variable:

see this code and will understand my doubt

  
local active = 0-----------------\>i need to update this value, because when i touch on the ball the value change to one supposedly, but we dont change  
local hello = function()  
 function ball1:touch( event )  
 if(event.phase == "ended") then  
 event.target.bodyType = "dynamic"  
  
 physics.setGravity (0,9.8)  
 active = 1 --------------------------\> when touch on the ball, active is equal to one but dont update and remains equal to zero  
 end  
end  
   
ball1:addEventListener( "touch", ball1 )  
  
end  
  
--very code...........  
..............  
..............  
-- and now i need to know if active is = 0 or = 1 to apply my definitions  
  
hello()  
 if active == 0 then  
 spawner:removeEventListener("touch", spawnMyObject)  
 block1:removeEventListener("touch", startDrag)  
 else   
 block1:addEventListener("touch", startDrag)  
 spawner:addEventListener("touch", spawnMyObject)  
  
 end  
  

[import]uid: 26056 topic_id: 15519 reply_id: 57516[/import]

Need plug and play code, can’t test the above - no code for spawning the ball, etc. [import]uid: 52491 topic_id: 15519 reply_id: 57589[/import]

I need to update my active value, because when i touch on the ball i need that he change to 1, to prohibit create more objects

main.lua

  
display.setStatusBar( display.HiddenStatusBar )  
  
\_W = display.contentWidth;  
\_H = display.contentHeight;  
director = require("director")  
  
local mainGroup = display.newGroup()  
  
local function main()  
  
 mainGroup:insert(director.directorView)  
 director:changeScene("game")  
 return true  
end  
  
main()  
  
  
local physics = require "physics"  
physics.start()  
physics.setGravity (0,0)  
  
local block1 = {}  
  
local ball1 = display.newImage("ball.png")  
physics.addBody(ball1,"kinematic", { density=2, bounce=0.3, radius=25});  
ball1.x = 250; ball1.y = 200  
  
local active = 0---------------------when touch on the ball, active will be 1, but it doesnt happen  
local hello = function()  
 function ball1:touch( event )  
  
  
 if(event.phase == "ended") then  
 event.target.bodyType = "dynamic"  
  
 physics.setGravity (0,9.8)  
 active = 1 -------------this value should update active to 1  
 end  
end  
   
ball1:addEventListener( "touch", ball1 )  
end  
  
----code to drag and spawn  
  
hello()  
 if active == 0 then  
 spawner:addEventListener("touch", spawnMyObject)  
 block1[#block1]:addEventListener("touch", startDrag)  
 else   
 spawner:removeEventListener("touch", spawnMyObject)  
 block1[#block1]:removeEventListener("touch", startDrag)  
  
 end  
  

and finally the worst doubt, how i will update the icon image of object?it is, if i have 3 blocks remaining i have an image with 3, if i only have 2 blocks remaining i have an image with 2, it update automatically.

I have tried some ways but I have not had success, I searched a lot but can not find something like what I need, i really need some help to close this part.

  
 local spawner = display.newImage("icon3.png",50, 0)  
 physics.addBody(spawner, "static", {isSensor = true})  
 spawner:addEventListener("collision", spawner)  
  
  
  
 local function spawnMyObject (event)  
 if event.phase == "began" then  
  
 --remove function  
 function spawner:collision (event)  
 event.other:removeSelf()  
 end  
  
 --objects and physics  
  
 if #block1 \< 3 then  
 block1[#block1+1] = display.newImage("block.png", 250, 300)  
 physics.addBody(block1[#block1], "kinematic", { density=2, friction=0, bounce=0, shape=barril })  
 block1[#block1].isFixedRotation = true  
 end  
  
 --function to drag ......   
 .....  
  
spawner:addEventListener("touch", spawnMyObject)  
  

I’ve been thinking something with:

if #block1 == 1 then
–image1, etc
elseif #block1 == 2 then
–image2,etc
end

but i cant make nothing with this, need some help :frowning:
[import]uid: 26056 topic_id: 15519 reply_id: 57591[/import]

updated [import]uid: 26056 topic_id: 15519 reply_id: 57680[/import]

i am updating now all code [import]uid: 26056 topic_id: 15519 reply_id: 57590[/import]

I can’t test this as obviously it gives off errors.

What issues are you having with closing it? You need to put [lua]end[/lua] at the end of each function, with an addition [lua]end[/lua] if there is an [lua]if[/lua] statement.

For the block image, have you considered using a sprite, or no?

A sprite would be easiest because you can just change the frame depending on how many blocks are left to spawn. [import]uid: 52491 topic_id: 15519 reply_id: 57730[/import]

hi peach, yes i can use sprites but i never used it before, have some examples? i downloaded on Techority, one tutorial about sprites made by you but in this case is different, i need to know how to update variable values, like active on example above, because local active = 0 is without the hello function, and when the hello function run, the active value should be = 1 and it isnt update.
and when the block is removed the #block need to be #block - 1, have problems with this

main.lua:

display.setStatusBar( display.HiddenStatusBar )  
   
   
   
\_W = display.contentWidth;  
\_H = display.contentHeight;  
   
   
director = require("director")  
   
local mainGroup = display.newGroup()  
   
local function main()  
   
 mainGroup:insert(director.directorView)  
 director:changeScene("game")  
 return true  
end  
   
main()  

game.lua

[code]
module(…, package.seeall)
local physics = require “physics”
physics.start()
physics.setGravity (0,0)

local block1 = {}

local ball1 = display.newImage(“ball.png”)
physics.addBody(ball1,“kinematic”, { density=2, bounce=0.3, radius=25});
ball1.x = 250; ball1.y = 200

local active = 0
local hello = function()
function ball1:touch( event )

if(event.phase == “ended”) then
event.target.bodyType = “dynamic”

physics.setGravity (0,9.8)
active = 1
end
end

ball1:addEventListener( “touch”, ball1 )
end
hello()
–***************************************************

– Drag and colision objects

–***************************************************

local spawner = display.newImage(“icon3.png”,50, 0)
physics.addBody(spawner, “static”, {isSensor = true})
spawner:addEventListener(“collision”, spawner)

–Objects and Drag

local function spawnMyObject (event)
if event.phase == “began” then

–remove function
function spawner:collision (event)
event.other:removeSelf()
end

–objects and physics

if #block1 < 3 then
block1[#block1+1] = display.newImage(“block.png”, 250, 300)
physics.addBody(block1[#block1], “kinematic”, { density=2, friction=0, bounce=0, shape=barril })
block1[#block1].isFixedRotation = true

local function startDrag( event )
local t = event.target
local phase = event.phase
if “began” == phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true

– Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y

– Stop current motion, if any
event.target:setLinearVelocity( 0, 0 )
event.target.angularVelocity = 0

elseif t.isFocus then
if “moved” == phase then
t.x = event.x - t.x0
t.y = event.y - t.y0

event.target.bodyType = “dynamic”

elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false

– Switch body type to kinematic, to avoid gravity
if ( not event.target.isPlatform ) then
event.target.bodyType = “kinematic”

end – last if

end
end – 2º if “move” end

–return true

end – startdrag end

if active == 0 then
block1[#block1]:addEventListener(“touch”, startDrag)
else
block1[#block1]:removeEventListener(“touch”, startDrag)
end

end – if blockCount end

end – 1º if “begin” end

end --spawnMyObject end

–***************************************************

– End Drag and colision objects

–***************************************************
–Objects bar

if active == 0 then
spawner:addEventListener(“touch”, spawnMyObject)

else
spawner:removeEventListener(“touch”, spawnMyObject)

end

[/code] [import]uid: 26056 topic_id: 15519 reply_id: 57744[/import]

i edited your code posted on techority and have good results, it is simple, but now i need to implemente on my code, but first i need to know if you think that this code is good and workable.

[code]

display.setStatusBar (display.HiddenStatusBar)

require “sprite”

local background = display.newImage (“background.png”)

local herosheet = sprite.newSpriteSheet(“person.png”, 32, 36)

local heroset = sprite.newSpriteSet (herosheet, 1, 12)
sprite.add (heroset, “heroleft”, 10, 1, 300, 0)
sprite.add (heroset, “heroright”, 4, 1, 300, 0)
sprite.add (heroset, “heroup”, 1, 1, 300, 0)
sprite.add (heroset, “herodown”, 7, 1, 300, 0)

local hero = sprite.newSprite (heroset)
hero.x = 160
hero.y = 200

local block = 3
–imagine that here is a code to create more blocks and changes the amount of variable blocks
if block == 1 then
hero:prepare(“heroup”)

elseif block ==2 then
hero:prepare(“herodown”)

elseif block == 3 then
hero:prepare(“heroleft”)

end
[/code] [import]uid: 26056 topic_id: 15519 reply_id: 57785[/import]

peach see my 2 last posts and tell me your opinion about that, because i never used sprites and dont know if my edited code is good to my goal, I need to have this part of my game as fast as possible because this part will help me a lot in other parts of my game, so I’m asking for opinions and some help on the code in the parts that I’m having more difficulty sorry about that :confused:

Very thanks for all the help peach, i am new in corona and have learning to much on the last days, thanks [import]uid: 26056 topic_id: 15519 reply_id: 57875[/import]

Hello,

First up - you wouldn’t need all those animations because you aren’t animating anything. You’d have one sheet with three or four frames, however many block images you want.

Then you would set it at the frame that was appropriate, using;

[lua]spriteName.currentFrame = 1[/lua]

Or whatever number was appropriate. (Which frame you want to show.)

#block1 will update automatically because it’s returning the number of entries in the table.

If you have a variable you want to update, you’d do;
[lua]myVariable = myVariable +1[/lua]

Peach [import]uid: 52491 topic_id: 15519 reply_id: 57941[/import]

Thanks peach, only one question, about the spritename =currentframe + 1, know some example? or tutorial?because i need to learn it first [import]uid: 26056 topic_id: 15519 reply_id: 57952[/import]

i used texture packer to create the sprite, and it makes some automatically code, i saw the peach pellen example on the techority but it is a animation, anyone know some example/tutorial where i can learn about frames, how to use it etc with if statements etc… [import]uid: 26056 topic_id: 15519 reply_id: 58107[/import]