I’ve posted this code before and did get a response. However, I still could not follow/understand how to make this happen.
I have a modified multipuck example that I’m trying to make the objects (in this case, diamonds) appear automatically. Right now I need to press a different object (button) to trigger the diamonds to appear. and then they only appear one at a time.
What i would like to have done is to drop the need to use a button to activate the diamonds and just have them populate (spawn) the screen automatically. I have been racking my brain, tried all kinds of examples but cannot get that function to work with “THIS” code.
Any help would be appreciated.
I’m basically not looking for an example so much as the actual code to place inside my code…
display.setStatusBar( display.HiddenStatusBar )
math.randomseed(os.time())
math.random()
math.random()
local physics = require(“physics”)
local gameUI = require(“gameUI”)
local easingx = require(“easingx”)
physics.start()
physics.setGravity( 0, 9.8) ------------------------------------------------------------------------------------------9.8 earth---------0,0= no gravity in any direction
popSound = audio.loadSound (“pop2_wav.wav”)
labelFont = gameUI.newFontXP{ ios=“Zapfino”, android=native.systemFont }
system.activate( “multitouch” )
local button = display.newImage ( “space1.png”)------------------------------------------------------------------------------------------------eventlistner attached
button.x = 50
button.y = 450
local diamondGfx = { “square-yellow.png”, “square-orange.png”, “square-blue.png”, “square-red.png”, “square-aqua.png”, “square-green.png”, “square-bomb.png”,“square-white.png”,}
local alldiamonds = {} ----------------------------------------------------------------------------------------------------------- empty table for storing objects
local function removeOffscreenItems()
for i = 1, #alldiamonds do
local onediamond = alldiamonds[i]
if (onediamond and onediamond.x) then
if onediamond.x < -100 or onediamond.x > display.contentWidth + 100 or onediamond.y < -100 or onediamond.y > display.contentHeight + 100 then
onediamond:removeSelf()
table.remove( alldiamonds, i )
end
end
end
end
local function dragBody( event )
return gameUI.dragBody( event )
end
----------------------------------------------------------------------------TOP SQUARES-----------------------------------------------------SPAWNdiamond
local function spawndiamond( event )
local phase = event.phase
if “ended” == phase then
audio.play( popSound )
randImage = diamondGfx[math.random( 1, 8 )]
alldiamonds[#alldiamonds + 1] = display.newImage( randImage )
local diamond = alldiamonds[#alldiamonds]
diamond.x = 60
diamond.y = 50
----------------------------------------------------------------------------------CODE TO MOVE DIAMONDS
local diamondmove = diamond
physics.addBody( diamondmove, { density=1.5, friction=0.6, radius=00.3, bounce=0.3 } )
diamondmove.x=40
diamondmove.y=60
local targetToMove = nil
local function setTarget(event)
local target = event.target
targetToMove = target
return true
end
local function movediamondmove(event)
if targetToMove ~= nil then
targetToMove.x = event.x
targetToMove.y = event.y
targetToMove = nil
end
return true
end
diamondmove:addEventListener(“tap”, setTarget)
Runtime:addEventListener(“tap”, movediamondmove)
diamond:addEventListener( “touch”, dragBody )
end
return true
end
button:addEventListener( “touch”, spawndiamond ) – touch the screen to create diamonds
local borderBottom = display.newRect( 30, 415, 258, 1 )
borderBottom:setFillColor( 255, 255, 255 )
physics.addBody( borderBottom, “static”, borderBodyElement )
[import]uid: 127842 topic_id: 22547 reply_id: 322547[/import]