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
--********************************************

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.

worked perfectly!!!!

Thanks very much.

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..

I don't understand the question. Maybe I could help if you rephrase the question.

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!!!

"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.

1
2
3
4
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

Amazing!!!
so simple, so good.

Thanks a lot.

In future please post your code in < lua > tags ;)

Part of the forum rules.

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

Thanks.

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;

1
2
3
local function neatCode (event)
print "Posting my code, all neat and tidy!"
end

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

views:1553 update:2011/10/5 8:48:05
corona forums © 2003-2011