How to avoid images appear when the touch is over a image and not over the background?

Hi guys!!, maybe a simple problem, but is breaking my head! haha.
I’m making appear images (blue square) when the background is touched, but I also have a green square that I can drag. The problem is that when I try to drag de green square, a blue square appear over the green square because I did a touch. How can I avoid to make appear the blue square over the green square when I touch the screen to drag the green square?.

the code is this:

local physics = require(“physics”)
local gameUI = require(“gameUI”)
local easingx = require(“easingx”)
local movieclip = require( “movieclip” )
local ui = require( “ui” )

–***VARS************
physics.start()
system.activate( “multitouch” )
physics = require(“physics”)
physics.setGravity(0,9.8)
local baseline = 290
_H = display.contentHeight
_W = display.contentWidth
mRand = math.random
–*************************

–************BACKGROUND************
local bkg = display.newImage( “background.jpg”, true )
bkg.x = display.contentWidth/2
bkg.y = display.contentHeight/2
–*********************************

–********GREEN SQUARE*********************
local object2 = display.newRect(0, 0, 50, 50) --green
object2.x = _W / 2
object2.y = _H / 3
object2:setFillColor(0, 255, 0)
physics.addBody(object2, “kinematic”, {density = 1.0, friction = 1, bounce = 0.2})
object2.myName = “object2”
–********************************************

–*****GREEN SQUARE DRAGGABLE***********
local function dragObject2 (event)
object2.x = event.x
object2.y = event.y
end
object2:addEventListener (“touch”, dragObject2)
–********************************************

–********APPEAR BLUE SQUARE WHEN THE BACKGROUND IS TOUCHED*********
local function spawnDisk( event )
local phase = event.phase

if “began” == phase then
audio.play( popSound )
myLabel.isVisible = false
local disk = display.newRect(0, 0, 50, 50)
disk.x = event.x
disk.y = event.y
disk:setFillColor(0, 0, 255)

transition.to(disk, { time = 500, xScale = 1.0, yScale = 1.0, transition = easingx.easeOutElastic }) – “pop” animation

physics.addBody(disk, “kinematic”, {density = 1.0, friction = 1, bounce = 0.2})
disk.linearDamping = 0.4
disk.angularDamping = 0.6

end
end
bkg:addEventListener( “touch”, spawnDisk ) – touch the screen to create disks
–******************************************** [import]uid: 54055 topic_id: 14092 reply_id: 314092[/import]

Put “return true” to the end of the “dragObject2” function. Also your drag code is a little simplistic, please search forum for better drag implementation. [import]uid: 46529 topic_id: 14092 reply_id: 51889[/import]

worked perfectly!!!

Thanks very much. [import]uid: 54055 topic_id: 14092 reply_id: 51890[/import]

Hi Culutas!!!

Maybe you can help me with another question…

How I can close the area where the blue squares can appear when the screen is touched

Thanks in advance… [import]uid: 54055 topic_id: 14092 reply_id: 51891[/import]

I don’t understand the question. Maybe I could help if you rephrase the question. [import]uid: 46529 topic_id: 14092 reply_id: 51897[/import]

Sorry, here I’m going again.

Every time I touch the screen, a image appear on the spot touched. So, I don’t want this images appear everywhere I touch, I just want to this images appear on the spot touched if I do the touch into a certain area of the screen.

Thanks!!! [import]uid: 54055 topic_id: 14092 reply_id: 51899[/import]

“IF” I understand you right, following is what you need. Put it into a touch listener and it will print only if touch occurs “in” defined area.

if (event.x \> 100) and (event.x \< 200) and (event.y \>300) and (event.y \< 400) then print("X:" .. event.x) print("Y:" .. event.y) end [import]uid: 46529 topic_id: 14092 reply_id: 51919[/import]

Amazing!!!
so simple, so good.

Thanks a lot. [import]uid: 54055 topic_id: 14092 reply_id: 51920[/import]

In future please post your code in < lua > tags :wink:

Part of the forum rules. [import]uid: 52491 topic_id: 14092 reply_id: 51982[/import]

Sorry about that.
So, how can I do that?, I don’t know how to post the code into < lua> format.

Thanks. [import]uid: 54055 topic_id: 14092 reply_id: 52031[/import]

No worries; put < lua > before your code and < / lua > at the end of it.

(Without the spaces of course.)

Then it will post like this;

[lua]local function neatCode (event)
print “Posting my code, all neat and tidy!”
end[/lua]

Peach :wink: [import]uid: 52491 topic_id: 14092 reply_id: 52076[/import]

Hi!!!
thanks for your help, the advice works very good.

Could you help me with another problem?, maybe it is easy to fix, but even in a forum I couldn’t to find the answer.

how to make appear a sprite.sheet right after another sprite.sheet?

Thanks [import]uid: 54055 topic_id: 14092 reply_id: 52791[/import]