Having multiple balls move on screen using parameters

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

  1. See my next post first, because I do want you to know.  This was a great post… :slight_smile:

  2. That actually looks like a clone of one of the 111% games.  I think a lot games are cloning is his zany ideas lately.  

IMHO referring to ‘Swipe Brick Breaker’ (by Monthly 23 who is clearly a cloner with some effort to model and improve) as if it were the ‘original’ is bad practice.    

I feel it is always better to give credit to the initial variant of a game if you can find it.  I know its hard however with so much clonig going on.

  1. You said this is a school project? - Please clarify.  Is this for a programming class where this is the graded assignment / final/ etc.?

Please be specific, give us the title of the class and the university/school.

  1. I would like to help, but I have to know the answer to #2 first.

I should say, the post itself was great!  Well thought out and well formatted.  It even includes a video and that is AWESOME.

I just want to know where we stand on the specific title and topic of the class, so I (and others) can give the appropriate level of assistance.

While you consider your reply I’ll give you a couple hints:

  1. As you probably figured out by now, there are many ways to approach this and solve it.  You can do this with and without using physics.  With and without enterFrame, etc.

  2. The first idea that pops into my head would involve: 

  • A single enterFrame listener
  • Physics - Could be an issue with multiple 100’s of balls… would need to test this.
  • Defered spawning of ball 2, 3, 4, 5 based on the position of the prior ball.
  • Some vector math to get the correct spawn position.
  • … and some glue code to fit it all together.

PS - The game looks great so far, excluding the issue you’re trying to resolve so you did great with the rest.

Your basic mistake is that every time you spawn a new ball, you assign it to the global variable ‘ball’. This overwrites the previous ball and means your enterFrame listener cannot access that ball anymore.

The simple solution is to put every new ‘ball’ into a balls table, and iterate through that table within your enterFrame listener.

I would also urge you to research scope, particularly the different between local and global variables in Corona and Lua in general, as all your objects and functions appear to be global and could cause you all sorts of issues going forward.

  1. Thank you!  :smiley:

  2. Sorry about that, there was a stage where lots of people at school was playing the game and then I thought: Heyyyy I’ll try and make this for my SDD major work. (didn’t think of researching if the game was a clone or not)

  3. Yes it’s for my SDD (Software Design and Developement) major work (a graded assignment 17.5% of my total course mark). This is for year 12 and will affect what university I’ll be able to go to. I’m not planning to sell/distribute the game it’s just for assessment purposes. 

  4. I hope you can still help but I think [member=‘nick sherman’] has solved my issue. Just woke up so I’ll start working on it to see if it fixes.

Ok I’ve read the concept of scopes, I’m working to changing my global functions to local ones and I’ve taken your advice in putting every new ball into a table then iterating through it with my enterFrame listener.

EDIT: It works I just needed to replace value with ball in my table iteration. 

EDIT2: literally just needed to add ball.velocityX and ball.velocityY

I’ve now realised that newballvelocityX and newballvelocityY is shared across all newly spawned balls so all my balls move in the same way. So is there a way for each ball to have custom properties so each ball can have it’s own velocity (like how each ball have ball.x and ball.y for positions I would like to have something like ball.vx and ball.vy) - SOLVED

[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

–using array to loop through have many balls to spawn
for ballcount = 1, 1 do --currentballcount – ie this is 4 balls total
ballarray[ballcount] = {}
end

local ball
local currentballcount
local newballvelocityX
local newballvelocityY
local ballTable = {}

– 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) – spawn 2nd ball
timer.performWithDelay(1000, spawnnewballs) – spawn 3rd ball

end
end

function spawnnewballs()

local ball = display.newImage(“images/bullet.png”)
ball.x = startx
ball.y = starty
ball.name = “ball”
physics.addBody(ball, “dynamic”, {density = 1, friction = 0, bounce = 0})
ballGroup.insert(ballGroup, ball)
table.insert(ballTable, ball) – insert ball into table “ballTable”
end

– ball movement
function updatenewbullet(ball)

for key,ball in pairs(ballTable) do – iterate through table?

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
end [/lua]

If I do ball = display.newImage(“images/bullet.png”) rather than local ball = display.newImage(“images/bullet.png”) I have my original problem but now every new ball spawned increases in speed… (eg 2nd ball is moving fine, 3rd ball is spawned and is moving faster than the 2nd ball which has stopped moving after the 3rd ball was spawned)

 [/spoiler]

  1. See my next post first, because I do want you to know.  This was a great post… :slight_smile:

  2. That actually looks like a clone of one of the 111% games.  I think a lot games are cloning is his zany ideas lately.  

IMHO referring to ‘Swipe Brick Breaker’ (by Monthly 23 who is clearly a cloner with some effort to model and improve) as if it were the ‘original’ is bad practice.    

I feel it is always better to give credit to the initial variant of a game if you can find it.  I know its hard however with so much clonig going on.

  1. You said this is a school project? - Please clarify.  Is this for a programming class where this is the graded assignment / final/ etc.?

Please be specific, give us the title of the class and the university/school.

  1. I would like to help, but I have to know the answer to #2 first.

I should say, the post itself was great!  Well thought out and well formatted.  It even includes a video and that is AWESOME.

I just want to know where we stand on the specific title and topic of the class, so I (and others) can give the appropriate level of assistance.

While you consider your reply I’ll give you a couple hints:

  1. As you probably figured out by now, there are many ways to approach this and solve it.  You can do this with and without using physics.  With and without enterFrame, etc.

  2. The first idea that pops into my head would involve: 

  • A single enterFrame listener
  • Physics - Could be an issue with multiple 100’s of balls… would need to test this.
  • Defered spawning of ball 2, 3, 4, 5 based on the position of the prior ball.
  • Some vector math to get the correct spawn position.
  • … and some glue code to fit it all together.

PS - The game looks great so far, excluding the issue you’re trying to resolve so you did great with the rest.

Your basic mistake is that every time you spawn a new ball, you assign it to the global variable ‘ball’. This overwrites the previous ball and means your enterFrame listener cannot access that ball anymore.

The simple solution is to put every new ‘ball’ into a balls table, and iterate through that table within your enterFrame listener.

I would also urge you to research scope, particularly the different between local and global variables in Corona and Lua in general, as all your objects and functions appear to be global and could cause you all sorts of issues going forward.

  1. Thank you!  :smiley:

  2. Sorry about that, there was a stage where lots of people at school was playing the game and then I thought: Heyyyy I’ll try and make this for my SDD major work. (didn’t think of researching if the game was a clone or not)

  3. Yes it’s for my SDD (Software Design and Developement) major work (a graded assignment 17.5% of my total course mark). This is for year 12 and will affect what university I’ll be able to go to. I’m not planning to sell/distribute the game it’s just for assessment purposes. 

  4. I hope you can still help but I think [member=‘nick sherman’] has solved my issue. Just woke up so I’ll start working on it to see if it fixes.

Ok I’ve read the concept of scopes, I’m working to changing my global functions to local ones and I’ve taken your advice in putting every new ball into a table then iterating through it with my enterFrame listener.

EDIT: It works I just needed to replace value with ball in my table iteration. 

EDIT2: literally just needed to add ball.velocityX and ball.velocityY

I’ve now realised that newballvelocityX and newballvelocityY is shared across all newly spawned balls so all my balls move in the same way. So is there a way for each ball to have custom properties so each ball can have it’s own velocity (like how each ball have ball.x and ball.y for positions I would like to have something like ball.vx and ball.vy) - SOLVED

[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

–using array to loop through have many balls to spawn
for ballcount = 1, 1 do --currentballcount – ie this is 4 balls total
ballarray[ballcount] = {}
end

local ball
local currentballcount
local newballvelocityX
local newballvelocityY
local ballTable = {}

– 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) – spawn 2nd ball
timer.performWithDelay(1000, spawnnewballs) – spawn 3rd ball

end
end

function spawnnewballs()

local ball = display.newImage(“images/bullet.png”)
ball.x = startx
ball.y = starty
ball.name = “ball”
physics.addBody(ball, “dynamic”, {density = 1, friction = 0, bounce = 0})
ballGroup.insert(ballGroup, ball)
table.insert(ballTable, ball) – insert ball into table “ballTable”
end

– ball movement
function updatenewbullet(ball)

for key,ball in pairs(ballTable) do – iterate through table?

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
end [/lua]

If I do ball = display.newImage(“images/bullet.png”) rather than local ball = display.newImage(“images/bullet.png”) I have my original problem but now every new ball spawned increases in speed… (eg 2nd ball is moving fine, 3rd ball is spawned and is moving faster than the 2nd ball which has stopped moving after the 3rd ball was spawned)

 [/spoiler]