HI,
I am playing around with multi-puck example that ships with SDK and I have two scenarios, one where a user taps the screen to fire a ball and one where it will be done as part of the runtime event loop.
The problem is the spawning of objects from a touch event works, as part of the event loop it doesnt and I suspect its because the touch event is attached to the background object and the runtime event isnt. Some code to illustrate;
local spawnBlue = function(event)
media.playEventSound( popBlueSound )
playerBearings[#playerBearings + 1] = display.newImage( "ballgrey.png" )
local disk = playerBearings[#playerBearings]
disk.x = display.contentWidth / 2
disk.y = display.contentHeight - 10
disk.xScale = 0.8
disk.yScale = 0.8
disk.isBullet = true
local puckDensity = 0.8
local puckFriction = 0.6
local objRadius = 8
physics.addBody( disk, { density=puckDensity, friction=puckFriction, radius=objRadius } )
disk.linearDamping = 0.4
disk.angularDamping = 0.6
local bearingSpeed = 150
blueGun:rotateTo(event)
local mScale = {}
mScale.x = math.cos(math.rad(blueGun.rotation - 90))
mScale.y = math.sin(math.rad(blueGun.rotation - 90))
local velocity = {}
velocity.x = mScale.x \* bearingSpeed
velocity.y = mScale.y \* bearingSpeed
disk:applyForce(velocity.x, velocity.y, disk.x, disk.y)
end
Now this gets called from a function called SpawnDisk
local function spawnDisk( event )
local phase = event.phase
local touchX = event.x
local touchY = event.y
gameStarted = true
if ("ended" == phase) then
myLabel.isVisible = false
myLabel2.isVisible = false
if (touchY \>= display.contentHeight / 2) and #playerBearings \< MAX\_BALLS then
spawnBlue(event)
end
if (touchY \<= display.contentHeight / 2 and #enemyBearings \< MAX\_BALLS) then
spawnRed(event)
end
if #playerBearings == MAX\_BALLS and isP1Reloading == false then
isP1Reloading = true
p1Reload.isVisible = true
media.playEventSound( p1soundReload )
end
if #enemyBearings == MAX\_BALLS and isP2Reloading == false then
isP2Reloading = true
p2Reload.isVisible = true
media.playEventSound( p2soundReload )
end
end
end
And the event handlers are defined so;
bkg:addEventListener( "touch", spawnDisk ) -- touch the screen to create disks
Runtime:addEventListener( "enterFrame", removeOffscreenItems ) -- clean up offscreen disks
So if I call spawnBlue from the spawnDisk function it works fine. If I put a spawnBlue call in the removeOffscreenItems function it doesnt actually render the ball.
I’m not sure what I need to add to get it to work?
Any help would be gratefully received!
Gary
[import]uid: 7334 topic_id: 8941 reply_id: 308941[/import]