Replicating Code

I have a splashscreen that opens and then transitions to my menu screen. On that screen I have three buttons. I want to have all those buttons transition to another screen. I got the Instructions button working but when I tried to duplicate the same code for the Start button and the Highscores button it would not work. I am posting the code below on what is working. Can you show me what I need to do to make the start button transition to the play screen? I am posting my code below and I’m trying to do it with Lua tags this time so hopefully it shows up correctly.

[lua]module(…, package.seeall)

function new()
local Group = 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.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)

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: 12241 reply_id: 312241[/import]

Where are you setting the eventListener for the Start/HighScores buttons??
cheers,

?:slight_smile: [import]uid: 3826 topic_id: 12241 reply_id: 44629[/import]

I would take line 33 - 41 and place it on line 48. I would then change the word instructions to highscores. Is that right? That’s what I did and it didn’t work. Should the listener go someplace else? [import]uid: 72372 topic_id: 12241 reply_id: 44635[/import]

Can somebody answer my last question? [import]uid: 72372 topic_id: 12241 reply_id: 44831[/import]