I wanted to know if we can know the objects involved in collision. Like if we have a scenario where there are many objects on the screen and all of them are allowed to collide with each other. But certain actions should be triggered only when certain two objects collide. For this case we need know the two objects which have collided. Can anyone guide me in this regard. Please… [import]uid: 183447 topic_id: 33282 reply_id: 333282[/import]
Yes, it’s possible. You can assign all of your objects a custom property to represent their type or their unique ID. Then, in the collision handler function, you can access these properties of the objects involved in the collision (as you would access any other properties, like their x,y coordinates) to identify them and trigger actions only in certain combinations.
- Andrew [import]uid: 109711 topic_id: 33282 reply_id: 132232[/import]
Can you guide me any tutorials or materials regarding this so I can read and get better hold on the above mentioned solution [import]uid: 183447 topic_id: 33282 reply_id: 132233[/import]
Hi there,
Sure, see this page: http://developer.coronalabs.com/content/game-edition-collision-detection. In the sections called “Global collision listeners” and “Local collision listeners”, you can see that the code creates a custom string property called myName, which is then used in the collision handler to identify the object.
- Andrew [import]uid: 109711 topic_id: 33282 reply_id: 132234[/import]
I have another doubt over here, the custom string property is working incase when an image is loaded normally like display.newImage(“Image.png”). But when I use the below code, I get error "attempt to index field ‘?’ "
Code
[lua]local diskGfx = { “puck_yellow.png”, “puck_green.png”, “puck_red.png” }
local allDisks = {} – empty table for storing objects
local function spawnDisk( event )
local phase = event.phase
if “ended” == phase then
audio.play( popSound )
myLabel.isVisible = false
randImage = diskGfx[math.random( 1, 3 )]
allDisks[#allDisks + 1] = display.newImage( randImage )
local disk = allDisks[#allDisks]
physics.addBody( disk, { density=0.3, friction=0.6, radius=66.0 } )
diskGfx[1].myName = “voilet”
end
end[/lua]
Code above is picked from Corona’s Multi Puck, and I have mentioned truncated code. Without the line "diskGfx[1].myName = “voilet” " every thing is fine but when I write this line that is I try to assign custom string I get the error.
Please explain how to assign custom string for this case. [import]uid: 183447 topic_id: 33282 reply_id: 132311[/import]
Hi there,
In your line 16, the code [lua]diskGfx[1][/lua] refers to the string [lua]“puck_yellow.png”[/lua] (assigned in line 1). You can only add custom properties, such as myName, to tables, not to strings.
In line 16, I think what you mean to have written is [lua]disk.myName = “violet”[/lua], since [lua]disk[/lua] is a reference to the display object that was just created.
- Andrew [import]uid: 109711 topic_id: 33282 reply_id: 132336[/import]
Yes, it’s possible. You can assign all of your objects a custom property to represent their type or their unique ID. Then, in the collision handler function, you can access these properties of the objects involved in the collision (as you would access any other properties, like their x,y coordinates) to identify them and trigger actions only in certain combinations.
- Andrew [import]uid: 109711 topic_id: 33282 reply_id: 132232[/import]
Can you guide me any tutorials or materials regarding this so I can read and get better hold on the above mentioned solution [import]uid: 183447 topic_id: 33282 reply_id: 132233[/import]
Hi there,
Sure, see this page: http://developer.coronalabs.com/content/game-edition-collision-detection. In the sections called “Global collision listeners” and “Local collision listeners”, you can see that the code creates a custom string property called myName, which is then used in the collision handler to identify the object.
- Andrew [import]uid: 109711 topic_id: 33282 reply_id: 132234[/import]
I have another doubt over here, the custom string property is working incase when an image is loaded normally like display.newImage(“Image.png”). But when I use the below code, I get error "attempt to index field ‘?’ "
Code
[lua]local diskGfx = { “puck_yellow.png”, “puck_green.png”, “puck_red.png” }
local allDisks = {} – empty table for storing objects
local function spawnDisk( event )
local phase = event.phase
if “ended” == phase then
audio.play( popSound )
myLabel.isVisible = false
randImage = diskGfx[math.random( 1, 3 )]
allDisks[#allDisks + 1] = display.newImage( randImage )
local disk = allDisks[#allDisks]
physics.addBody( disk, { density=0.3, friction=0.6, radius=66.0 } )
diskGfx[1].myName = “voilet”
end
end[/lua]
Code above is picked from Corona’s Multi Puck, and I have mentioned truncated code. Without the line "diskGfx[1].myName = “voilet” " every thing is fine but when I write this line that is I try to assign custom string I get the error.
Please explain how to assign custom string for this case. [import]uid: 183447 topic_id: 33282 reply_id: 132311[/import]
Hi there,
In your line 16, the code [lua]diskGfx[1][/lua] refers to the string [lua]“puck_yellow.png”[/lua] (assigned in line 1). You can only add custom properties, such as myName, to tables, not to strings.
In line 16, I think what you mean to have written is [lua]disk.myName = “violet”[/lua], since [lua]disk[/lua] is a reference to the display object that was just created.
- Andrew [import]uid: 109711 topic_id: 33282 reply_id: 132336[/import]
Thank You Sooo Much
[import]uid: 183447 topic_id: 33282 reply_id: 132566[/import]
Thank You Sooo Much
[import]uid: 183447 topic_id: 33282 reply_id: 132566[/import]