I have two objects they moving down together and how can I display the two objects again after the two objects are stop like tetris game
You would have to create new versions of them.
can you give me a simple example code
thanks
How did you create the first two objects? You probably want to use a table of objects rather than individual ones. Post some code showing what you’re doing to create the first two objects.
local block = display.newRect(50,50,50,50)
local block2 = display.newRect(100,50,50,50)
block2:setFillColor(200,0,200)
local function moveDown(self , event)
if self.y > 450 then
self.y = 451;
else
self.y = self.y + 3;
end
end
block.enterFrame = moveDown
Runtime:addEventListener(“enterFrame” , block)
block2.enterFrame = moveDown
Runtime:addEventListener(“enterFrame” , block2)
I would suggest that you read up on tables so that you know how they work. Since you want multiple of the same object on the screen you have to have references to each object. But you don’t want to duplicate all of that code over and over.
So I would consider something like this:
local function moveDown(self , event) if self.y \> 450 then self.y = 451; -- you might want to remove the event listener here for this block so that it stops trying to move it each time. else self.y = self.y + 3; end end local function spawnBlock(blockType) local X = 50 if blockType == 2 then X = 100 end local block = display.newRect(X, 50, 50, 50) if blockType == 2 then block:setFillColor(200,0,200) end block.enterFrame = moveDown Runtime:addEventListener("enterFrame", block) return block end local blocks = {} -- a table to keep track of each block local function makeTwoBlocks() blocks[#blocks+1] = spawnBlock(1) blocks[#blocks+1] = spawnBlock(2) end local spawnTimer = timer.performWithDelay(5000, makeTwoBlocks(), 0)
This way you have a function that will create an individual block. You have a table to track them. A timer that runs forever and every 5 seconds spits out 2 new blocks.
how to spits out first 2 blocks early or do you have another way to make blocks spits out after the blocks are stop
thanks
Call makeTwoBlocks() after the timer
one of my object have a rotate function can I put the two objects in spawnBlock(blockType)
You would have to create new versions of them.
can you give me a simple example code
thanks
How did you create the first two objects? You probably want to use a table of objects rather than individual ones. Post some code showing what you’re doing to create the first two objects.
local block = display.newRect(50,50,50,50)
local block2 = display.newRect(100,50,50,50)
block2:setFillColor(200,0,200)
local function moveDown(self , event)
if self.y > 450 then
self.y = 451;
else
self.y = self.y + 3;
end
end
block.enterFrame = moveDown
Runtime:addEventListener(“enterFrame” , block)
block2.enterFrame = moveDown
Runtime:addEventListener(“enterFrame” , block2)
I would suggest that you read up on tables so that you know how they work. Since you want multiple of the same object on the screen you have to have references to each object. But you don’t want to duplicate all of that code over and over.
So I would consider something like this:
local function moveDown(self , event) if self.y \> 450 then self.y = 451; -- you might want to remove the event listener here for this block so that it stops trying to move it each time. else self.y = self.y + 3; end end local function spawnBlock(blockType) local X = 50 if blockType == 2 then X = 100 end local block = display.newRect(X, 50, 50, 50) if blockType == 2 then block:setFillColor(200,0,200) end block.enterFrame = moveDown Runtime:addEventListener("enterFrame", block) return block end local blocks = {} -- a table to keep track of each block local function makeTwoBlocks() blocks[#blocks+1] = spawnBlock(1) blocks[#blocks+1] = spawnBlock(2) end local spawnTimer = timer.performWithDelay(5000, makeTwoBlocks(), 0)
This way you have a function that will create an individual block. You have a table to track them. A timer that runs forever and every 5 seconds spits out 2 new blocks.
how to spits out first 2 blocks early or do you have another way to make blocks spits out after the blocks are stop
thanks
Call makeTwoBlocks() after the timer
one of my object have a rotate function can I put the two objects in spawnBlock(blockType)