How to switch between 2 shapes when touched?

hi,

This sounds simple, but i can’t seem to figure it out.

I’m trying to switch between 2 shapes every time they are touched. I managed to get it to change from Shape01 to Shape02 when touched, but it wont change back to Shape01 when touched the 2nd time.

This as far as i could get with the code. Any help would be greatly appreciated.

thanks

[code]

local Shape01
local Shape02
Shape01= display.newCircle( 40, 25, 20 )
Shape01:setFillColor( 210, 38, 42 )
Shape01.alpha = 1
Shape01.myName = “Shape01”
local changeShape = function(event)

local phase = event.phase
local t = event.target

if “began” == phase then

if t.myName == “Shape01” then

Shape02= display.newRect( Shape01.x - 20, Shape01.y - 20, 40, 40 )
Shape02:setFillColor( 0, 0, 0 )
Shape02.myName = “Shape02”
Shape01:removeSelf() – destroy object

end

if t.myName == “Shape02” then
Shape01= display.newCircle( Shape02.x + 20, Shape02.y + 20, 20 )
Shape01:setFillColor( 210, 38, 42 )
Shape01.alpha = 1
Shape01.myName = “Shape01”
Shape02:removeSelf() – destroy object

end

end
end

Shape01:addEventListener( “touch”, changeShape )
Shape02:addEventListener( “touch”, changeShape )

[/code] [import]uid: 10542 topic_id: 3661 reply_id: 303661[/import]

You should use the debugger (included in the Corona app directory, simply start it and you will see debug output in a terminal while running your app)…then you can see the error instantly. Shape02 is not initialized before you add an event listener and therefore the event is never fired / the app stops at that point. [import]uid: 11219 topic_id: 3661 reply_id: 11161[/import]

Yes, you are destroying Shape1 on touch “began” but the event listener for Shape1 still exists and fires again on the touch “ended” phase causing a runtime error.

See the fixed code below.

Note because you have to refer to the changeShape function while inside the function itself, you have to forward declare the function name as below.

[code]

local Shape01
local Shape02

Shape01= display.newCircle( 40, 25, 20 )
Shape01:setFillColor( 210, 38, 42 )
Shape01.alpha = 1
Shape01.myName = “Shape01”
local changeShape

changeShape = function(event)

local phase = event.phase
local t = event.target

if “began” == phase then

if t.myName == “Shape01” then

Shape02= display.newRect( Shape01.x - 20, Shape01.y - 20, 40, 40 )
Shape02:setFillColor( 0, 0, 0 )
Shape02.myName = “Shape02”
Shape01:removeEventListener( “touch”, changeShape )
Shape01:removeSelf() – destroy object
Shape02:addEventListener( “touch”, changeShape )
end

if t.myName == “Shape02” then
Shape01= display.newCircle( Shape02.x + 20, Shape02.y + 20, 20 )
Shape01:setFillColor( 210, 38, 42 )
Shape01.alpha = 1
Shape01.myName = “Shape01”
Shape02:removeEventListener( “touch”, changeShape )
Shape02:removeSelf() – destroy object
Shape01:addEventListener( “touch”, changeShape )
end

end
end

Shape01:addEventListener( “touch”, changeShape )
[import]uid: 9064 topic_id: 3661 reply_id: 11162[/import]

can you please help by showing it in code. Still not working

thanks [import]uid: 10542 topic_id: 3661 reply_id: 11163[/import]

The code posted above works but you do set the fill color of Shape2 to be black when you create it so it won’t be visible on a black screen. Try Shape02:setFillColor(255,255,255) [import]uid: 9064 topic_id: 3661 reply_id: 11164[/import]

it only changes once from Shape01 to Shape02, but it wont change back from Shape02 to Shape01

i added the following code at the beginning so the background is white.

  
local background = display.newRect( 0, 0, 320, 480 )  
background:setFillColor( 255, 255, 255 )  
  

The rest is the same.

still stuck [import]uid: 10542 topic_id: 3661 reply_id: 11171[/import]

Did you include the changes I made in the changeShape function? Are you running the Simulator by launching Terminal and are you getting any error messages in the terminal window?

It works fine for me so I don’t know what’s going wrong with your version. [import]uid: 9064 topic_id: 3661 reply_id: 11174[/import]

It’s working now. :slight_smile:

thanks. you’ve been a great help [import]uid: 10542 topic_id: 3661 reply_id: 11206[/import]