Backbutton Issues

I am trying to get my backbutton to work and no matter what words I change it will not work. What do I have incorrect in the code?

[lua]local backbutton = display.newImage (“backbutton.png”)
localGroup:insert(backbutton)
backbutton.x = 75
backbutton.y = 300
backbutton.xScale = .5
backbutton.yScale = .5

local gameboard = display.newImage (“gameboard.png”)
gameboard.x = 310
gameboard.y = 200
localGroup:insert(gameboard)

local function touched (event)
if (“ended” == event.phase) then
director:changeScene( “titlescreen”, “fade” )
end
end

background:addEventListener (“touch”, press touched)
–>MUST return a display.newGroup()



return localGroup
end
–> This is how we end every file except for director and main, as mentioned in my first comment[/lua] [import]uid: 72372 topic_id: 12420 reply_id: 312420[/import]

I am not getting the full picture of your problem from this code.

backbutton not working means what ? not appearing or you are not able to go back to another director file ?

if its not appearing, it is because gameboard is coming on top of the backbutton.

if the problem is that its not going to the previous scene, then I may require to see your touch event code for backbutton ?

is the function touch supposed to take you to the previous screen ? then try using the below code.

[lua]local function touched (event)
if (“ended” == event.phase) then
director:changeScene( “titlescreen”, “fade” )
end
end

backbutton:addEventListener (“touch”, touched)[/lua] [import]uid: 71210 topic_id: 12420 reply_id: 45306[/import]

The problem is that my backbutton will not take me to my previous screen. I tried the code you listed and it still didn’t work. what touch event code are you looking for? I am new to this but I thought what I posted is my touch event code for the backbutton. Sorry if that sounds crazy but I am only 2 weeks into this programming stuff. [import]uid: 72372 topic_id: 12420 reply_id: 45312[/import]

Technowand,

Also when I used the code you gave me, it would not let me transition back to the screen with the back button. What I have is a splashscreen that goes to my menu/titlescreen. On that screen I have buttons that transition to other screens with the gameboard screen being one of them, which is where the backbutton is located. When I used that code and tried to press the play button that would take me to the next screen it would not work anymore. I then modified back to what I had and it would let me get to the screen then but of course the backbutton still does not work:-( [import]uid: 72372 topic_id: 12420 reply_id: 45315[/import]

Is this in your main.lua file? [import]uid: 17138 topic_id: 12420 reply_id: 45318[/import]

Well I found your problem…
[lua]–Wrong
background:addEventListener (“touch”, press touched)

–It should say
backbutton:addEventListener (“touch”, touched) [import]uid: 17138 topic_id: 12420 reply_id: 45319[/import]

Khaodik,

it is not my main.lua file. it is my gameboard.lua file. i transition from my titlescreen to this screen and im trying to transition back. in regards to the code you suggested thats the same code technowand suggested and you can read my response above on what happen. it stopped me from even transitioning to the screen with the backbutton. [import]uid: 72372 topic_id: 12420 reply_id: 45320[/import]

try this…

[lua]module(…, package.seeall)

local localGroup = display.newGroup()

local gameboard = display.newImage (“gameboard.png”)
gameboard.x = 310
gameboard.y = 200
localGroup:insert(gameboard)

local backbutton = display.newImage (“backbutton.png”)
backbutton.x = 75
backbutton.y = 300
backbutton.xScale = .5
backbutton.yScale = .5
localGroup:insert(backbutton)

local function touched (event)
if (“ended” == event.phase) then
director:changeScene( “titlescreen”, “fade” )
end
end

backbutton:addEventListener (“touch”, touched)

–>MUST return a display.newGroup()

function new()
return localGroup
end[/lua]
if it still din’t work…send me your code outline…
I mean just the scene change codes (everything related to director) of all the below files ie
from main file -> titlescreen -> gameboard

just a quick check… does your main file display any images ??
there is a chance of that file still being shown as it will not be unloaded by director code…

[import]uid: 71210 topic_id: 12420 reply_id: 45321[/import]

Technowand,

My main screen does not display any images and the screen listed in it for changeScene is my loadtitlescreen file. I will place the code below in the order of the changes. Every page transitions like I want except the gameboard.

First file is my loadtitlescreen.lua and this is the code for that entire page since it’s so short

[lua]module(…, package.seeall)

– Main function - MUST return a display.newGroup()
function new()
local localGroup = display.newGroup()

local theTimer
local loadingImage

local showLoadingScreen = function()
loadingImage = display.newImageRect( “blackbackground.png”, 1600, 820)
localGroup:insert(loadingImage)

local goToLevel = function()
director:changeScene( “titlescreen” )
end

math.randomseed( os.time() )
theTimer = timer.performWithDelay( 1500, goToLevel, 1 )
end

local logo = display.newImage (“logo.png”)
logo.x = 240
logo.y = 150

showLoadingScreen()

unloadMe = function()
end

– MUST return a display.newGroup()
return localGroup
end[/lua]

This is the next page which is my titlescreenpage.lua, with just that section

[lua]local instructions = display.newImage (“instructions.png”)
instructions.x = 100
instructions.y = 240
instructions.xScale = .5
instructions.yScale = .5

–>This places the instructions button

local function touchedInstructions (event)
if (“ended” == event.phase) then
director:changeScene (“gameboard”, “fade”)
end
end

instructions:addEventListener (“touch”, touchedInstructions)[/lua]

This is my gameboard.lua file, just that section

[lua]local backbutton = display.newImage (“backbutton.png”)
localGroup:insert(backbutton)
backbutton.x = 75
backbutton.y = 300
backbutton.xScale = .5
backbutton.yScale = .5
local function touched (event)
if (“ended” == event.phase) then
director:changeScene( “titlescreen”, “fade” )
end
end

backbutton:addEventListener (“touch”, touch)

–>MUST return a display.newGroup()


return localGroup
end[/lua] [import]uid: 72372 topic_id: 12420 reply_id: 45395[/import]

the functions you have given is not complete. hope you are doing all the necessary steps…

make sure your main file have
[lua]director = require(“director”)
local mainGroup = display.newGroup()
mainGroup:insert(director.directorView)
director:changeScene(“loadtitlescreen”)[/lua]

files except main file have
[lua]module(…, package.seeall)
local localGroup = display.newGroup()
function new()
return localGroup
end[/lua]

also make sure that all your display object are local and is inserted into the group which is returned in the new function.

also change your gameboard code to
[lua]local function touched (event)
if (“ended” == event.phase) then
director:changeScene( “titlescreen”, “fade” )
end
end

backbutton:addEventListener (“touch”, touched)[/lua]

hope this will help… [import]uid: 71210 topic_id: 12420 reply_id: 45421[/import]

ok I will post the files…
I have removed all the graphics items and replaced it with text…but fundamental logic is still the same…
hope this will help…

main.lua
[lua]director = require(“director”)
local mainGroup = display.newGroup()
mainGroup:insert(director.directorView)
director:changeScene(“loadtitlescreen”)[/lua]

loadtitlescreen.lua
[lua]module(…, package.seeall)
function new()
print (“in load titlescreen file”)
local localGroup = display.newGroup()

local showLoadingScreen = function()

local goToLevel = function()
director:changeScene( “titlescreen” )
end

math.randomseed( os.time() )
theTimer = timer.performWithDelay( 1500, goToLevel, 1 )
end
showLoadingScreen()
– MUST return a display.newGroup()
return localGroup
end[/lua]
titlescreen.lua
[lua]module(…, package.seeall)
local localGroup = display.newGroup()
local instructions = display.newText (“instructions”,100,240,nil,25)
localGroup:insert(instructions)

local function touchedInstructions (event)
if (“ended” == event.phase) then
director:changeScene (“gameboard”, “fade”)
end
end

instructions:addEventListener (“touch”, touchedInstructions)

function new()
– MUST return a display.newGroup()
print(“title screen”)
return localGroup
end[/lua]

gameboard.lua
[lua]module(…, package.seeall)

function new()
local localGroup2 = display.newGroup()

–local backbutton = display.newImage (“backbutton.png”)
local backbutton = display.newText(“back”,75,300,nil,25)
localGroup2:insert(backbutton)
local function touched (event)
if (“ended” == event.phase) then
director:changeScene( “titlescreen”, “fade” )
end
end

backbutton:addEventListener (“touch”, touched)

return localGroup2
end[/lua]
[import]uid: 71210 topic_id: 12420 reply_id: 45428[/import]

Technowand,

Thanks for taking the time out to help me on this code. I’m entering it a little at a time just to see what changes. I’m updating the titlescreen page and when I put what you had it removed my button. This is the code for that entire page so you can see it. Did I place something in the wrong place? Remember I said this page was working fine without your code changes but now it’s not.

[lua]module(…, package.seeall)

function new()
local localGroup = display.newGroup()
–> This is how we start every single file or “screen” in our folder, except for main.lua
– and director.lua
–> director.lua is NEVER modified, while only one line in main.lua changes, described in that file


local physics = require (“physics”)
physics.start ()

borderBodyElement = { friction = 0.5, bounce=0.5 }

local borderLeft = display.newRect( 0, 20, 20, 460 )
borderLeft:setFillColor( 0, 0, 0, 0 )
physics.addBody( borderLeft, “static”, borderBodyElement )

local borderRight = display.newRect( 300, 20, 20, 460 )
borderLeft:setFillColor( 0, 0, 0, 0 )
physics.addBody( borderRight, “static”, borderBodyElement )

local background = display.newImage (“title2.png”)
background.x = 240
–> This sets the background

local instructions = display.newText (“instructions”, 100,240,nil,25)
localGroup:insert(instructions)
instructions.xScale = .5
instructions.yScale = .5

–>This places the instructions button

local function touchedInstructions (event)
if (“ended” == event.phase) then
director:changeScene (“gameboard”, “fade”)
end
end

instructions:addEventListener (“touch”, touchedInstructions)

function new()
–>MUST return a display.newGroup()
print(“titlescreen”)
return localGroup
end

local highscores = display.newImage (“highscores.png”)
highscores.x = 380
highscores.y = 240
highscores.xScale = .5
highscores.yScale = .5

local startbutton = display.newImage (“startbutton.png”)
startbutton.x = 240
startbutton.y = 200
startbutton.xScale = .5
startbutton.yScale = .5

local floor = display.newImage(“floor.png”)
floor.x = 230
floor.y = 340

physics.addBody( floor, “static”, { friction=0.5, bounce=0.3 } )

local greenblock = display.newImage( “greenblock.png” )
greenblock.x = 80; greenblock.y = 50; greenblock.rotation = 5
greenblock.xScale = 0; greenblock.yScale = 0

physics.addBody( greenblock, { density=1.0, friction=0.5, bounce= 0.4 } )

local purpleblock = display.newImage( “purpleblock.png” )
purpleblock.x = 150; purpleblock.y = 70; purpleblock.rotation = 10
purpleblock.xScale = 0; purpleblock.yScale = 0

physics.addBody( purpleblock, { density=2.0, friction=0.5, bounce= 0.4 } )

local orangeblock = display.newImage( “orangeblock.png” )
orangeblock.x = 370; orangeblock.y = 60; orangeblock.rotation = 10
orangeblock.xScale = 0; orangeblock.yScale = 0

physics.addBody( orangeblock, { density=4.0, friction=0.5, bounce= 0.4 } )

local yellowblock = display.newImage( “yellowblock.png” )
yellowblock.x = 400; yellowblock.y = 80; yellowblock.rotation = 10
yellowblock.xScale = 0; yellowblock.yScale = 0

physics.addBody( yellowblock, { density=3.0, friction=0.5, bounce= 0.4 } )

local redblock = display.newImage( “redblock.png” )
redblock.x = 300; redblock.y = 50; redblock.rotation = 5
redblock.xScale = 0; redblock.yScale = 0

physics.addBody( redblock, { density=2.0, friction=0.5, bounce= 0.4 } )

–This begins the redblock drag code
local redblockx0, redblocky0

local function startDrag( event )
local redblock = event.target
local phase = event.phase
if “began” == phase then

– Store initial position
x0 = event.x
y0 = event.y

redblockx0 = redblock.x
redblocky0 = redblock.y

print(x0, y0)

– Stop current motion, if any
redblock:setLinearVelocity( 0, 0 )
redblock.angularVelocity = 0

elseif “moved” == phase or “ended” == phase or “cancelled” == phase then
redblock.x = redblockx0 + event.x - x0
redblock.y = redblocky0 + event.y - y0
end
–>This ends the redblock drag code
–>Stop further propagtion of touch event!
return true
end

redblock:addEventListener( “touch”, startDrag );
end[/lua] [import]uid: 72372 topic_id: 12420 reply_id: 45525[/import]

I’m not sure if this will fix it, but one thing I noticed you are missing in the code above is.

[lua]–Add this before your last end
return localGroup

–Also you have to insert all your display objects into the display group.
localGroup:insert(myBody)

–Let me know if that fixed it… [import]uid: 17138 topic_id: 12420 reply_id: 45528[/import]

I have found some problems in your file

  1. You have 2 “new” function - you just need one
  2. your objects are not added to localGroup
  3. change the variable name floor - seems like it is a reserved keyword

resultant code will look like this…
try it and let me know how it works.

[lua]module(…, package.seeall)

local localGroup = display.newGroup()
–> This is how we start every single file or “screen” in our folder, except for main.lua
– and director.lua
–> director.lua is NEVER modified, while only one line in main.lua changes, described in that file


local physics = require (“physics”)
physics.start ()

borderBodyElement = { friction = 0.5, bounce=0.5 }

local borderLeft = display.newRect( 0, 20, 20, 460 )
borderLeft:setFillColor( 0, 0, 0, 0 )
physics.addBody( borderLeft, “static”, borderBodyElement )
localGroup:insert(borderLeft)

local borderRight = display.newRect( 300, 20, 20, 460 )
borderLeft:setFillColor( 0, 0, 0, 0 )
physics.addBody( borderRight, “static”, borderBodyElement )
localGroup:insert(borderRight)
local background = display.newImage (“title2.png”)
background.x = 240
localGroup:insert(background)
–> This sets the background

local instructions = display.newText (“instructions”, 100,240,nil,25)
localGroup:insert(instructions)
instructions.xScale = .5
instructions.yScale = .5
localGroup:insert(instructions)

–>This places the instructions button

local function touchedInstructions (event)
if (“ended” == event.phase) then
director:changeScene (“gameboard”, “fade”)
end
end

instructions:addEventListener (“touch”, touchedInstructions)

local highscores = display.newImage (“highscores.png”)
highscores.x = 380
highscores.y = 240
highscores.xScale = .5
highscores.yScale = .5
localGroup:insert(highscores)

local startbutton = display.newImage (“startbutton.png”)
startbutton.x = 240
startbutton.y = 200
startbutton.xScale = .5
startbutton.yScale = .5
localGroup:insert(startbutton)

local floorIMG = display.newImage(“floor.png”)
floorIMG.x = 230
floorIMG.y = 340
localGroup:insert(floorIMG)

physics.addBody( floor, “static”, { friction=0.5, bounce=0.3 } )

local greenblock = display.newImage( “greenblock.png” )
greenblock.x = 80; greenblock.y = 50; greenblock.rotation = 5
greenblock.xScale = 0; greenblock.yScale = 0
localGroup:insert(greenblock)
physics.addBody( greenblock, { density=1.0, friction=0.5, bounce= 0.4 } )

local purpleblock = display.newImage( “purpleblock.png” )
purpleblock.x = 150; purpleblock.y = 70; purpleblock.rotation = 10
purpleblock.xScale = 0; purpleblock.yScale = 0
localGroup:insert(purpleblock)
physics.addBody( purpleblock, { density=2.0, friction=0.5, bounce= 0.4 } )

local orangeblock = display.newImage( “orangeblock.png” )
orangeblock.x = 370; orangeblock.y = 60; orangeblock.rotation = 10
orangeblock.xScale = 0; orangeblock.yScale = 0
localGroup:insert(orangeblock)
physics.addBody( orangeblock, { density=4.0, friction=0.5, bounce= 0.4 } )

local yellowblock = display.newImage( “yellowblock.png” )
yellowblock.x = 400; yellowblock.y = 80; yellowblock.rotation = 10
yellowblock.xScale = 0; yellowblock.yScale = 0
localGroup:insert(yellowblock)
physics.addBody( yellowblock, { density=3.0, friction=0.5, bounce= 0.4 } )

local redblock = display.newImage( “redblock.png” )
redblock.x = 300; redblock.y = 50; redblock.rotation = 5
redblock.xScale = 0; redblock.yScale = 0
localGroup:insert(redblock)
physics.addBody( redblock, { density=2.0, friction=0.5, bounce= 0.4 } )

–This begins the redblock drag code
local redblockx0, redblocky0

local function startDrag( event )
local redblock = event.target
local phase = event.phase
if “began” == phase then

– Store initial position
x0 = event.x
y0 = event.y

redblockx0 = redblock.x
redblocky0 = redblock.y

print(x0, y0)

– Stop current motion, if any
redblock:setLinearVelocity( 0, 0 )
redblock.angularVelocity = 0

elseif “moved” == phase or “ended” == phase or “cancelled” == phase then
redblock.x = redblockx0 + event.x - x0
redblock.y = redblocky0 + event.y - y0
end
–>This ends the redblock drag code

–>Stop further propagtion of touch event!
return true
end

redblock:addEventListener( “touch”, startDrag );

function new()
–>MUST return a display.newGroup()
print(“titlescreen”)
return localGroup
end
[/lua] [import]uid: 71210 topic_id: 12420 reply_id: 45535[/import]

Technowand,

It doesn’t work if I put the code the way you have it. I will paste the code below the way I have it and it is working fine. I really need help on the second coding that is listed though. It’s my backbutton that is not working on the gameboard page. It will not let me change the touched to match. You will see that is different.

[lua]module(…, package.seeall)

function new()
local localGroup = display.newGroup()
–> This is how we start every single file or “screen” in our folder, except for main.lua
– and director.lua
–> director.lua is NEVER modified, while only one line in main.lua changes, described in that file


local physics = require (“physics”)
physics.start ()

borderBodyElement = { friction = 0.5, bounce=0.5 }

local borderBottom = display.newRect( 0, 320, 460, 20 )
borderBottom:setFillColor( 0, 0, 0, 0 )
physics.addBody( borderBottom, “static”, borderBodyElement )

local borderLeft = display.newRect( 0, 20, 20, 460 )
borderLeft:setFillColor( 0, 0, 0, 0 )
physics.addBody( borderLeft, “static”, borderBodyElement )
localGroup:insert(borderLeft)

local borderRight = display.newRect( 300, 20, 20, 460 )
borderLeft:setFillColor( 0, 0, 0, 0 )
physics.addBody( borderRight, “static”, borderBodyElement )
localGroup:insert(borderRight)

local background = display.newImage (“title2.png”)
background.x = 240
localGroup:insert(background)
–> This sets the background

local instructions = display.newImage (“instructions.png”)
instructions.x = 100
instructions.y = 240
instructions.xScale = .5
instructions.yScale = .5
localGroup:insert(instructions)

–>This places the instructions button

local function touchedInstructions (event)
if (“ended” == event.phase) then
director:changeScene (“gameboard”, “fade”)
end
end

instructions:addEventListener (“touch”, touchedInstructions)

function new()
–>MUST return a dislay.newGroup()
print(“titlescreen”)
return localGroup
end

local highscores = display.newImage (“highscores.png”)
highscores.x = 380
highscores.y = 240
highscores.xScale = .5
highscores.yScale = .5
localGroup:insert(highscores)

local startbutton = display.newImage (“startbutton.png”)
startbutton.x = 240
startbutton.y = 200
startbutton.xScale = .5
startbutton.yScale = .5
localGroup:insert(startbutton)

local greenblock = display.newImage( “greenblock.png” )
greenblock.x = 80; greenblock.y = 50; greenblock.rotation = 5
greenblock.xScale = 0; greenblock.yScale = 0
localGroup:insert(greenblock)
physics.addBody( greenblock, { density=1.0, friction=0.5, bounce= 0.4 } )

local purpleblock = display.newImage( “purpleblock.png” )
purpleblock.x = 150; purpleblock.y = 70; purpleblock.rotation = 10
purpleblock.xScale = 0; purpleblock.yScale = 0
localGroup:insert(purpleblock)
physics.addBody( purpleblock, { density=2.0, friction=0.5, bounce= 0.4 } )

local orangeblock = display.newImage( “orangeblock.png” )
orangeblock.x = 370; orangeblock.y = 60; orangeblock.rotation = 10
orangeblock.xScale = 0; orangeblock.yScale = 0
localGroup:insert(orangeblock)
physics.addBody( orangeblock, { density=4.0, friction=0.5, bounce= 0.4 } )

local yellowblock = display.newImage( “yellowblock.png” )
yellowblock.x = 400; yellowblock.y = 80; yellowblock.rotation = 10
yellowblock.xScale = 0; yellowblock.yScale = 0
localGroup:insert(yellowblock)
physics.addBody( yellowblock, { density=3.0, friction=0.5, bounce= 0.4 } )

local redblock = display.newImage( “redblock.png” )
redblock.x = 300; redblock.y = 50; redblock.rotation = 5
redblock.xScale = 0; redblock.yScale = 0
localGroup:insert(redblock)
physics.addBody( redblock, { density=2.0, friction=0.5, bounce= 0.4 } )

–This begins the redblock drag code
local redblockx0, redblocky0

local function startDrag( event )
local redblock = event.target
local phase = event.phase
if “began” == phase then

– Store initial position
x0 = event.x
y0 = event.y

redblockx0 = redblock.x
redblocky0 = redblock.y

print(x0, y0)

– Stop current motion, if any
redblock:setLinearVelocity( 0, 0 )
redblock.angularVelocity = 0

elseif “moved” == phase or “ended” == phase or “cancelled” == phase then
redblock.x = redblockx0 + event.x - x0
redblock.y = redblocky0 + event.y - y0
end
–>This ends the redblock drag code
–>Stop further propagtion of touch event!
return true
end

redblock:addEventListener( “touch”, startDrag );
end[/lua]

This is the page that has the backbutton that is not working. It’s the same thing I pasted above when we first started talking. I pasted the entire page this time. It’s long because I have coding for sixteen blocks. If you know a way to place the blocks on in shorten code let me know BUT the backbutton is what is not working.

[lua]module(…, package.seeall)

–>main function - MUST return a display.newGroup()
function new()
local localGroup = display.newGroup()
–> This is how we start every single file or “screen” in our folder, except for main.lua
– and director.lua
–> director.lua is NEVER modified, while only one line in main.lua changes, described in that file


local physics = require (“physics”)
physics.start ()

–>Background
local background = display.newImage (“blackbackground.png”)
localGroup:insert(background)
–> This sets the background

–>Gameboard
local gameboard = display.newImage (“gameboard.png”)
gameboard.x = 310
gameboard.y = 200
localGroup:insert(gameboard)
–>This sets the gameboard

local redblock = display.newImage (“redblock.png”)
localGroup:insert(redblock)
redblock.x = 70
redblock.y = 50

local redblockx0, redblocky0

local function startDrag( event )
local redblock = event.target
local phase = event.phase
if “began” == phase then

– Store initial position
x0 = event.x
y0 = event.y

redblockx0 = redblock.x
redblocky0 = redblock.y

print(x0, y0)

– Stop current motion, if any
redblock:setLinearVelocity( 0, 0 )
redblock.angularVelocity = 0

elseif “moved” == phase or “ended” == phase or “cancelled” == phase then
redblock.x = redblockx0 + event.x - x0
redblock.y = redblocky0 + event.y - y0
end
– Stop further propagtion of touch event!
return true
end

redblock:addEventListener( “touch”, startDrag );

–>This ends the redblock drag code

local redblock = display.newImage (“redblock.png”)
localGroup:insert(redblock)
redblock.x = 75
redblock.y = 50

local redblockx0, redblocky0

local function startDrag( event )
local redblock = event.target
local phase = event.phase
if “began” == phase then

– Store initial position
x0 = event.x
y0 = event.y

redblockx0 = redblock.x
redblocky0 = redblock.y

print(x0, y0)

– Stop current motion, if any
redblock:setLinearVelocity( 0, 0 )
redblock.angularVelocity = 0

elseif “moved” == phase or “ended” == phase or “cancelled” == phase then
redblock.x = redblockx0 + event.x - x0
redblock.y = redblocky0 + event.y - y0
end
– Stop further propagtion of touch event!
return true
end

redblock:addEventListener( “touch”, startDrag );
–>This ends the redblock drag code

local redblock = display.newImage (“redblock.png”)
localGroup:insert(redblock)
redblock.x = 80
redblock.y = 50

local redblockx0, redblocky0

local function startDrag( event )
local redblock = event.target
local phase = event.phase
if “began” == phase then

– Store initial position
x0 = event.x
y0 = event.y

redblockx0 = redblock.x
redblocky0 = redblock.y

print(x0, y0)

– Stop current motion, if any
redblock:setLinearVelocity( 0, 0 )
redblock.angularVelocity = 0

elseif “moved” == phase or “ended” == phase or “cancelled” == phase then
redblock.x = redblockx0 + event.x - x0
redblock.y = redblocky0 + event.y - y0
end
– Stop further propagtion of touch event!
return true
end

redblock:addEventListener( “touch”, startDrag );

–>This ends the redblock drag code

local redblock = display.newImage (“redblock.png”)
localGroup:insert(redblock)
redblock.x = 85
redblock.y = 50

local redblockx0, redblocky0

local function startDrag( event )
local redblock = event.target
local phase = event.phase
if “began” == phase then

– Store initial position
x0 = event.x
y0 = event.y

redblockx0 = redblock.x
redblocky0 = redblock.y

print(x0, y0)

– Stop current motion, if any
redblock:setLinearVelocity( 0, 0 )
redblock.angularVelocity = 0

elseif “moved” == phase or “ended” == phase or “cancelled” == phase then
redblock.x = redblockx0 + event.x - x0
redblock.y = redblocky0 + event.y - y0
end
– Stop further propagtion of touch event!
return true
end

redblock:addEventListener( “touch”, startDrag );

–>This ends the redblock drag code

–>This begins the purpleblock image insert and drag code
local purpleblock = display.newImage (“purpleblock.png”)
localGroup:insert(purpleblock)
purpleblock.x = 190
purpleblock.y = 50

local purpleblockx0, purpleblocky0

local function startDrag( event )
local purpleblock = event.target
local phase = event.phase
if “began” == phase then

– Store initial position
x0 = event.x
y0 = event.y

purpleblockx0 = purpleblock.x
purpleblocky0 = purpleblock.y

print(x0, y0)

– Stop current motion, if any
purpleblock:setLinearVelocity( 0, 0 )
purpleblock.angularVelocity = 0

elseif “moved” == phase or “ended” == phase or “cancelled” == phase then
purpleblock.x = purpleblockx0 + event.x - x0
purpleblock.y = purpleblocky0 + event.y - y0
end
– Stop further propagtion of touch event!
return true
end

purpleblock:addEventListener( “touch”, startDrag );

–>This ends the purpleblock drag code

–>This begins the purpleblock image insert and drag code
local purpleblock = display.newImage (“purpleblock.png”)
localGroup:insert(purpleblock)
purpleblock.x = 195
purpleblock.y = 50

local purpleblockx0, purpleblocky0

local function startDrag( event )
local purpleblock = event.target
local phase = event.phase
if “began” == phase then

– Store initial position
x0 = event.x
y0 = event.y

purpleblockx0 = purpleblock.x
purpleblocky0 = purpleblock.y

print(x0, y0)

– Stop current motion, if any
purpleblock:setLinearVelocity( 0, 0 )
purpleblock.angularVelocity = 0

elseif “moved” == phase or “ended” == phase or “cancelled” == phase then
purpleblock.x = purpleblockx0 + event.x - x0
purpleblock.y = purpleblocky0 + event.y - y0
end
– Stop further propagtion of touch event!
return true
end

purpleblock:addEventListener( “touch”, startDrag );

–>This ends the purpleblock drag code

–>This begins the purpleblock image insert and drag code
local purpleblock = display.newImage (“purpleblock.png”)
localGroup:insert(purpleblock)
purpleblock.x = 200
purpleblock.y = 50

local purpleblockx0, purpleblocky0

local function startDrag( event )
local purpleblock = event.target
local phase = event.phase
if “began” == phase then

– Store initial position
x0 = event.x
y0 = event.y

purpleblockx0 = purpleblock.x
purpleblocky0 = purpleblock.y

print(x0, y0)

– Stop current motion, if any
purpleblock:setLinearVelocity( 0, 0 )
purpleblock.angularVelocity = 0

elseif “moved” == phase or “ended” == phase or “cancelled” == phase then
purpleblock.x = purpleblockx0 + event.x - x0
purpleblock.y = purpleblocky0 + event.y - y0
end
– Stop further propagtion of touch event!
return true
end

purpleblock:addEventListener( “touch”, startDrag );

–>This ends the purpleblock drag code

–>This begins the yellowblock image insert and drag code
local yellowblock = display.newImage (“yellowblock.png”)
localGroup:insert(yellowblock)
yellowblock.x = 305
yellowblock.y = 50

local yellowblockx0, yellowblocky0

local function startDrag( event )
local yellowblock = event.target
local phase = event.phase
if “began” == phase then

– Store initial position
x0 = event.x
y0 = event.y

yellowblockx0 = yellowblock.x
yellowblocky0 = yellowblock.y

print(x0, y0)

– Stop current motion, if any
yellowblock:setLinearVelocity( 0, 0 )
yellowblock.angularVelocity = 0

elseif “moved” == phase or “ended” == phase or “cancelled” == phase then
yellowblock.x = yellowblockx0 + event.x - x0
yellowblock.y = yellowblocky0 + event.y - y0
end
– Stop further propagtion of touch event!
return true
end

yellowblock:addEventListener( “touch”, startDrag );

–>This ends the yellowblock drag code

–>This begins the yellowblock image insert and drag code
local yellowblock = display.newImage (“yellowblock.png”)
localGroup:insert(yellowblock)
yellowblock.x = 310
yellowblock.y = 50

local yellowblockx0, yellowblocky0

local function startDrag( event )
local yellowblock = event.target
local phase = event.phase
if “began” == phase then

– Store initial position
x0 = event.x
y0 = event.y

yellowblockx0 = yellowblock.x
yellowblocky0 = yellowblock.y

print(x0, y0)

– Stop current motion, if any
yellowblock:setLinearVelocity( 0, 0 )
yellowblock.angularVelocity = 0

elseif “moved” == phase or “ended” == phase or “cancelled” == phase then
yellowblock.x = yellowblockx0 + event.x - x0
yellowblock.y = yellowblocky0 + event.y - y0
end
– Stop further propagtion of touch event!
return true
end

yellowblock:addEventListener( “touch”, startDrag );

–>This ends the yellowblock drag code

–>This begins the yellowblock image insert and drag code

local yellowblock = display.newImage (“yellowblock.png”)
localGroup:insert(yellowblock)
yellowblock.x = 315
yellowblock.y = 50

local yellowblockx0, yellowblocky0

local function startDrag( event )
local yellowblock = event.target
local phase = event.phase
if “began” == phase then

– Store initial position
x0 = event.x
y0 = event.y

yellowblockx0 = yellowblock.x
yellowblocky0 = yellowblock.y

print(x0, y0)

– Stop current motion, if any
yellowblock:setLinearVelocity( 0, 0 )
yellowblock.angularVelocity = 0

elseif “moved” == phase or “ended” == phase or “cancelled” == phase then
yellowblock.x = yellowblockx0 + event.x - x0
yellowblock.y = yellowblocky0 + event.y - y0
end
– Stop further propagtion of touch event!
return true
end

yellowblock:addEventListener( “touch”, startDrag );

–>This ends the yellowblock drag code

–>This begins the orangeblock image insert and drag code

local orangeblock = display.newImage (“orangeblock.png”)
localGroup:insert(orangeblock)
orangeblock.x = 75
orangeblock.y = 140

local orangeblockx0, orangeblocky0

local function startDrag( event )
local orangeblock = event.target
local phase = event.phase
if “began” == phase then

– Store initial position
x0 = event.x
y0 = event.y

orangeblockx0 = orangeblock.x
orangeblocky0 = orangeblock.y

print(x0, y0)

– Stop current motion, if any
orangeblock:setLinearVelocity( 0, 0 )
orangeblock.angularVelocity = 0

elseif “moved” == phase or “ended” == phase or “cancelled” == phase then
orangeblock.x = orangeblockx0 + event.x - x0
orangeblock.y = orangeblocky0 + event.y - y0
end
– Stop further propagtion of touch event!
return true
end

orangeblock:addEventListener( “touch”, startDrag );

–>This ends the orangeblock drag code

–>This begins the orangeblock image insert and drag code

local orangeblock = display.newImage (“orangeblock.png”)
localGroup:insert(orangeblock)
orangeblock.x = 80
orangeblock.y = 140

local orangeblockx0, orangeblocky0

local function startDrag( event )
local orangeblock = event.target
local phase = event.phase
if “began” == phase then

– Store initial position
x0 = event.x
y0 = event.y

orangeblockx0 = orangeblock.x
orangeblocky0 = orangeblock.y

print(x0, y0)

– Stop current motion, if any
orangeblock:setLinearVelocity( 0, 0 )
orangeblock.angularVelocity = 0

elseif “moved” == phase or “ended” == phase or “cancelled” == phase then
orangeblock.x = orangeblockx0 + event.x - x0
orangeblock.y = orangeblocky0 + event.y - y0
end
– Stop further propagtion of touch event!
return true
end

orangeblock:addEventListener( “touch”, startDrag );

–>This ends the orangeblock drag code

–>This begins the orangeblock image insert and drag code

local orangeblock = display.newImage (“orangeblock.png”)
localGroup:insert(orangeblock)
orangeblock.x = 85
orangeblock.y = 140

local orangeblockx0, orangeblocky0

local function startDrag( event )
local orangeblock = event.target
local phase = event.phase
if “began” == phase then

– Store initial position
x0 = event.x
y0 = event.y

orangeblockx0 = orangeblock.x
orangeblocky0 = orangeblock.y

print(x0, y0)

– Stop current motion, if any
orangeblock:setLinearVelocity( 0, 0 )
orangeblock.angularVelocity = 0

elseif “moved” == phase or “ended” == phase or “cancelled” == phase then
orangeblock.x = orangeblockx0 + event.x - x0
orangeblock.y = orangeblocky0 + event.y - y0
end
– Stop further propagtion of touch event!
return true
end

orangeblock:addEventListener( “touch”, startDrag );

–>This ends the orangeblock drag code

–>This begins the greenblock image insert and drag code

local greenblock = display.newImage (“greenblock.png”)
localGroup:insert(greenblock)
greenblock.x = 75
greenblock.y = 230

local greenblockx0, greenblocky0

local function startDrag( event )
local greenblock = event.target
local phase = event.phase
if “began” == phase then

– Store initial position
x0 = event.x
y0 = event.y

greenblockx0 = greenblock.x
greenblocky0 = greenblock.y

print(x0, y0)

– Stop current motion, if any
greenblock:setLinearVelocity( 0, 0 )
greenblock.angularVelocity = 0

elseif “moved” == phase or “ended” == phase or “cancelled” == phase then
greenblock.x = greenblockx0 + event.x - x0
greenblock.y = greenblocky0 + event.y - y0
end
– Stop further propagtion of touch event!
return true
end

greenblock:addEventListener( “touch”, startDrag );

–>This ends the greenblock drag code

–>This begins the greenblock image insert and drag code

local greenblock = display.newImage (“greenblock.png”)
localGroup:insert(greenblock)
greenblock.x = 80
greenblock.y = 230

local greenblockx0, greenblocky0

local function startDrag( event )
local greenblock = event.target
local phase = event.phase
if “began” == phase then

– Store initial position
x0 = event.x
y0 = event.y

greenblockx0 = greenblock.x
greenblocky0 = greenblock.y

print(x0, y0)

– Stop current motion, if any
greenblock:setLinearVelocity( 0, 0 )
greenblock.angularVelocity = 0

elseif “moved” == phase or “ended” == phase or “cancelled” == phase then
greenblock.x = greenblockx0 + event.x - x0
greenblock.y = greenblocky0 + event.y - y0
end
– Stop further propagtion of touch event!
return true
end

greenblock:addEventListener( “touch”, startDrag );

–>This ends the greenblock drag code

–>This begins the greenblock image insert and drag code

local greenblock = display.newImage (“greenblock.png”)
localGroup:insert(greenblock)
greenblock.x = 85
greenblock.y = 230

local greenblockx0, greenblocky0

local function startDrag( event )
local greenblock = event.target
local phase = event.phase
if “began” == phase then

– Store initial position
x0 = event.x
y0 = event.y

greenblockx0 = greenblock.x
greenblocky0 = greenblock.y

print(x0, y0)

– Stop current motion, if any
greenblock:setLinearVelocity( 0, 0 )
greenblock.angularVelocity = 0

elseif “moved” == phase or “ended” == phase or “cancelled” == phase then
greenblock.x = greenblockx0 + event.x - x0
greenblock.y = greenblocky0 + event.y - y0
end
– Stop further propagtion of touch event!
return true
end

greenblock:addEventListener( “touch”, startDrag );

–>This ends the greenblock drag code

local backbutton = display.newImage (“backbutton.png”)
localGroup:insert(backbutton)
backbutton.x = 75
backbutton.y = 300
backbutton.xScale = .5
backbutton.yScale = .5
local function touched (event)
if (“ended” == event.phase) then
director:changeScene( “titlescreen”, “fade” )
end
end

backbutton:addEventListener (“touch”, touch)

–>MUST return a display.newGroup()


return localGroup
end
–> This is how we end every file except for director and main, as mentioned in my first comment[/lua]
[import]uid: 72372 topic_id: 12420 reply_id: 45838[/import]

If that is your only problem, then just change the function to look like this.
[lua]local function touched (event)
if (“ended” == event.phase) then
director:changeScene( “titlescreen”, “fade” )
end
end

backbutton:addEventListener (“touch”, touched) [import]uid: 17138 topic_id: 12420 reply_id: 45847[/import]

Thanks but I tried that code before and it won’t work. I know it should but it doesn’t. It works on one of my other pages but not that page which is driving me crazy. [import]uid: 72372 topic_id: 12420 reply_id: 45850[/import]

Try a tap function, that might work.
[lua]function button ( event )
director:changeScene( “titlescreen”, “fade” )
end

background:addEventListener( “tap”, button ) [import]uid: 17138 topic_id: 12420 reply_id: 45855[/import]

Khaodik,

The tap functions works great thank you. I still can’t figure out why I couldn’t get the back button to work like on other screens but in this case I left the backbutton although the person can tap the background to get back. They will hit the backbutton since it’s still shown on the page. I’ll worry later on why the backbutton code wouldn’t work.

THANKS A MILLION!! [import]uid: 72372 topic_id: 12420 reply_id: 45898[/import]

No problem, I’m glad it worked! [import]uid: 17138 topic_id: 12420 reply_id: 45940[/import]