Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]

How do I set an event listener to every object that is created in an array?
Started by naumannad Nov 16 2018 08:50 AM

1 reply to this topic
array objects event
This topic has been archived. This means that you cannot reply to this topic.
[TOPIC: post.html]


[GLOBAL: userInfoPane.html]
  • Observer

  • 1 posts
  • Corona SDK

Hey guys, 


I am creating an app in which the user has many balls that spawn every second. 10 blue balls = 1 green ball, 10 green = 1 yellow, 10 yellow = 1 orange, etc. These balls are kind of like currency as well because if you buy something in the upgrade store, it takes it from your ball collection.


I am working on a level system in which you use your balls to battle other people's balls. What I am trying to figure out is how I can use a collision listener to make it that when two balls collide, they affect damage on each other. I also want to figure out how to assign an attack value for every ball that spawns. (Just to let you all know, the balls are created in an array).


Essentially, I want the attack scene to by autonomous. So like if a blue ball collided with an enemy ball, the blue ball would affect one damage to the enemy and the enemy would affect 1 to the blue ball.  When an enemy or a bue ball has a health of <= 0, it would remove itself from the scene.

blue = {}
blueHealth = {}
blues = display.newGroup()

function initBlue()
	for i = 1, blueOnes do
		blue[i] = display.newImage("blueball.png")
		blue[i].x = math.random(100, 200)
		blue[i].y = math.random(-400, -100)
		blue[i].width = 40
		blue[i].height = 40
		physics.addBody(blue[i], "dynamic", { bounce = 0.5,
		friction = 0.2, radius = 20})
		blueHealth[i] = 1

		blue[i].id = "good"


blueCPU = {}
blueHealthCPU = {}
bluesCPU = display.newGroup()

function initBlueCPU()
	for i = 1, blueOnesCPU do
		blueCPU[i] = display.newImage("blueball.png")
		blueCPU[i].x = math.random(800, 900)
		blueCPU[i].y = math.random(-400, -100)
		blueCPU[i].width = 40
		blueCPU[i].height = 40
		physics.addBody(blueCPU[i], "dynamic", { bounce = 0.5,
		friction = 0.2, radius = 20})
		blueHealthCPU[i] = 1

		blueCPU[i].id = "bad"


[TOPIC: post.html]


[GLOBAL: userInfoPane.html]
  • Corona Geek

  • 1,960 posts
  • Corona SDK

Ideally you would have one function that creates all the different coloured balls for both players.

Store data about each type of ball in a table, and use the appropriate data depending on the ball being created.

A ball knows who it belongs to, what its HP and attack power etc is, when balls collide the collision listener gets the data from each ball and calculates the outcome. You can add any of these stats just like position, width and height are set in your example above.