Spawning multiple images

Hi

The below code loops ten times from the gametimer but only one image is displayed, or appears to be. What is wrong with my code?

Thanks

local storyboard = require( “storyboard” )
local scene = storyboard.newScene()

local centerX = display.contentCenterX;
local centerY = display.contentCenterY;
local ball1
_W = display.contentWidth
_H = display.contentHeight

local physics = require(“physics”)
physics.start( )
physics.setGravity( xGravity, yGravity)

local function startgame()

 ball1.x =  math.random(display.contentWidth)
 ball1.y = display.contentHeight-4 --tried different number here
 physics.addBody(ball1, {bounce = 0.6, friction =0.1, radius=45})
 ball1:setLinearVelocity(900,1500)
 end

function scene:createScene( event )
 local group = self.view
    local group = scene.view;

  local bg = display.newImageRect(group, “IMG/bgBlack.png”, totalWidth, totalHeight);
  bg.x, bg.y = centerX, centerY;
 

local left_wall = display.newRect(0,0,2,_H*2)
physics.addBody(left_wall,“static”, {bounce = 0.5, friction=0.5,filter = boarderCollsionFilter})
left_wall.myName = “Left Wall”

local right_wall = display.newRect(_W-1,0,2,_H*2)
physics.addBody(right_wall,“static”, {bounce = 0.5, friction=0.5,filter = boarderCollsionFilter})
right_wall.myName = “Right Wall”

local top_wall = display.newRect(0,0,_W*2,2)
physics.addBody(top_wall,“static”, {bounce = 0.5, friction=0.5,filter = boarderCollsionFilter})
top_wall.myName = “Top Wall”

local bottom_wall = display.newRect(0,_H,_W*2,2)
physics.addBody(bottom_wall,“static”, {bounce = 1, friction=0.5,filter = boarderCollsionFilter})
bottom_wall.myName = “Bottom Wall”

 
  gametimer = timer.performWithDelay( 20, startgame, 10)
  end
 
 

 – Called immediately after scene has moved onscreen:
function scene:enterScene( event )
local group = self.view
 ball1 = display.newImage(group, “IMG/image1.png”,10)

end
 

where is your code in spawning new objects? is it in the startgame function?

yes it is called from this timer gametimer = timer.performWithDelay( 20, startgame, 10)

you dont seem to be actually spawning any balls, just replacing the position of ball 1. Also its much more effecient to use a for loop instead of timer.performWithDelay for short periods of time, e.g

local ball = {} local random = math.random local \_W = display.viewableContentWidth local \_H = display.viewableContentHeight local physics = require("physics") physics.start( ) physics.setGravity( 0,0) for i = 1, 10 do     ball[i] = display.newImage(group, "IMG/image1.png",10)     ball[i].x = random(\_W)     ball[i].y = \_H \* .5     physics.addBody( ball[i] , {bounce = 0.6, friction =0.1, radius=45})      ball[i]:setLinearVelocity(900,1500)  end  

Much cleaner thank you

where is your code in spawning new objects? is it in the startgame function?

yes it is called from this timer gametimer = timer.performWithDelay( 20, startgame, 10)

you dont seem to be actually spawning any balls, just replacing the position of ball 1. Also its much more effecient to use a for loop instead of timer.performWithDelay for short periods of time, e.g

local ball = {} local random = math.random local \_W = display.viewableContentWidth local \_H = display.viewableContentHeight local physics = require("physics") physics.start( ) physics.setGravity( 0,0) for i = 1, 10 do     ball[i] = display.newImage(group, "IMG/image1.png",10)     ball[i].x = random(\_W)     ball[i].y = \_H \* .5     physics.addBody( ball[i] , {bounce = 0.6, friction =0.1, radius=45})      ball[i]:setLinearVelocity(900,1500)  end  

Much cleaner thank you