Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Increasing Touch Detection Area using Containers
Started by augustino.fp May 17 2018 04:17 PM

4 replies to this topic
container touch listener display
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

augustino.fp

[GLOBAL: userInfoPane.html]
augustino.fp
  • Observer

  • 2 posts
  • Corona SDK

Hi all,

 

first time poster!

 

I have a question regarding Containers in Corona and was hoping someone would be able to help out!

 

I am developing a game in which I have several display objects (Images) that respond to user touch via touch listeners. It works great, the only issue is that I would like to increase the area in which the touch listener responds, making it greater than the bounds of the image. 

 

I figured I would do this with containers, making a separate container for each image with bounds exceeding those of the image, please see the below code for clarification.

 

First I initialize both the image display object and its associated container, notice that I increase the bounds when creating the container:

 

self.image = display.newImageRect(image, width, height) --this is the display object that will appear on screenself.container = display.newContainer( level_parent:get_group(), width + 100, height + 100 ) --create container to extend accuracy of bounds

 

I then add the necessary touch listener to the container:

 

self.container:addEventListener("touch", touchListener) -- touch event listeners

 

Here is the external code which calls this creation, as well as inserting the image as a child of the container and setting the necessary transforms:

 

block_obj = block.new(level_init, image, data[i][j], x, y,theta, i, j, r, rotation, is_block, width, height) -- create new block objectblock_container = block_obj:get_container()block_image = block_obj:get_image()block_container.x = x; block_container.y = yblock_container.rotation = rotationblock_container:insert(block_image, true)level_group:insert(block_container)

 

When I run the code, these changes are not reflected; the user touch still only works within the bounds of the display image.

 

Thank you for any information you can provide.

 

Regards,

 

Augustino

 

 



[TOPIC: post.html]
#2

augustino.fp

[GLOBAL: userInfoPane.html]
augustino.fp
  • Observer

  • 2 posts
  • Corona SDK

It appears when I pasted the code, it did not show up very well lol here is clearer code:

 

self.image = display.newImageRect(image, width, height) --this is the display object that will appear on screens

 

self.container = display.newContainer( level_parent:get_group(), width + 100, height + 100 ) --create container to extend accuracy of bounds

 

self.container:addEventListener("touch", touchListener) -- touch event listeners

 

block_obj = block.new(level_init, image, data[i][j], x, y,theta, i, j, r, rotation, is_block, width, height) -- create new block object

 

block_container = block_obj:get_container()

 

block_image = block_obj:get_image()

 

block_container.x = x; block_container.y = y

 

block_container.rotation = rotation

 

block_container:insert(block_image, true)

 

 

level_group:insert(block_container)



[TOPIC: post.html]
#3

sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
  • Contributor

  • 500 posts
  • Corona SDK

You will need to place another object in the container.  Place an object in the container that is the size you need and set its alpha to zero.  Because the object is zero alpha you will need to change its .isHitTestable parameter to true.  

object.alpha = 0
object.isHitTestable = true

You might need to place the event listener on the invisible object - try it both ways.



[TOPIC: post.html]
#4

SGS

[GLOBAL: userInfoPane.html]
SGS
  • Corona Geek

  • 2,110 posts
  • Corona SDK

Or simply use a larger png with a larger transparency area.



[TOPIC: post.html]
#5

sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
  • Contributor

  • 500 posts
  • Corona SDK

In general, I avoid active transparencies in graphics files (for hit/touch purposes) because I prefer to make size/aspect ratio alterations with numbers and code rather than restructuring them in graphics programs.




[topic_controls]
[/topic_controls]