I’m currently trying to replicate “Swipe Brick Breaker” for a school project
https://www.youtube.com/watch?v=NrjuN6Vx7KM <------ (this is what I’m trying to make)
I’m up to the stage of implementing multiple balls moving on screen but I can’t figure how to have multiple balls moving using the “enterframe” event listener. Help would be greatly appreciated.
Here’s what I have for my original single ball
[spoiler]
[lua]
– Bullet properties
function updatebullet()
– Movement
bullet.x = bullet.x + velocityX
bullet.y = bullet.y + velocityY
– If bullet hits the ceiling or left or right wall, bounce off of it
if bullet.x < 0 or bullet.x + bullet.width > display.contentWidth then
velocityX = -velocityX
end
– Bounce off top wall
if bullet.y < -30 then
velocityY = -velocityY
end
– stop if bullet reaches bottem wall
if bullet.y > display.contentHeight + 20 then
velocityY = 0
velocityX = 0
end
if spawnCheck == true then
removeArray()
spawning()
spawnCheck = false
end
end
[/lua]
then I have Runtime:addEventListener(“enterFrame”, updatebullet) so that the function constantly updating
[/spoiler]
I’ve created new functions which creates new balls and now every time a new ball is spawned the old one stops moving…
(eg I spawn the 2nd ball and it will move fine but when the 3rd ball spawns the 2nd ball stops moving and the 3rd ball moves fine)
Here’s the code:
[spoiler]
[lua]
function gameListeners(event)
if event == “add” then
Runtime:addEventListener(“enterFrame”, updatebullet)
Runtime:addEventListener(“enterFrame”, function() updatenewbullet(ball) end)
Runtime:addEventListener( “touch”, onScreenTouch) – listener is always active
– Remove listeners when not needed to free up memory
elseif event == “remove” then
Runtime:removeEventListener(“enterFrame”, updatebullet)
end
end
– use function when new balls need to be spawned
function releaseballs()
startx = bullet.x – record original ball’s x and y position
starty = bullet.y
newballvelocityX = velocityX – record original ball’s x and y velocity
newballvelocityY = velocityY –
ballGroup:removeSelf()
ballGroup = display.newGroup()
for ballcount = 1, 1 do
timer.performWithDelay(500, spawnnewballs)
timer.performWithDelay(1000, spawnnewballs)
end
end
function spawnnewballs()
ball = display.newImage(“images/bullet.png”)
ball.name = “ball”
ball.x = startx
ball.y = starty
physics.addBody(ball, “dynamic”, {density = 1, friction = 0, bounce = 0})
ballGroup.insert(ballGroup, ball)
end
– ball movement
function updatenewbullet(ball)
if ball ~= nil then
– Movement
ball.x = ball.x + newballvelocityX
ball.y = ball.y + newballvelocityY
– If ball hits the ceiling or left or right wall, bounce off of it
if ball.x < 0 or ball.x + ball.width > display.contentWidth then
newballvelocityX = -newballvelocityX
end
– Bounce off top wall
if ball.y < -30 then
newballvelocityY = -newballvelocityY
end
– stop if ball reaches bottem wall
if ball.y > display.contentHeight + 20 then
newballvelocityY = 0
newballvelocityX = 0
ball:removeSelf()
end
end
end
[/lua]
[/spoiler]
I thought the issue would be fixed if I used a parameter in my updatenewbullet() turning it into updatenewbullet(ball) but that’s not doing any good