adamstc222
New Coder
Hello I need help adding a button to my game I just want it to be a button that shows instructions for my game when it is pressed please can someone help. Here is my code:
--displays title screen
local titleBg
local playBtn
local creditsBtn
local titleView
--Author: Adam St Clair
--App: Corona Destroyer
--Date: March 2021
--Define the variables
rotateAmt = 5
rotateMin = -90
rotateMax = 40
cannonRotation = 0
cannonForce = 1200
playerPoints = 0
targetHit = false
timeLeft = 1000
--Define the functions
function closeDownApp(event)
os.exit ( )
end
--This adds the widget library to our project
local widget = require "widget"
--This is what is run when we press our button
local myButtonEvent = function (event )
if event.phase == "release" then
print( "You pressed and released the "..event.target.id.." button!" )
end
end
--This will create a button using images
local myButton=widget.newButton{
--The id can be used to tell you what button was pressed in your button event
id = "mainMenuButton",
--this is the default button image
default="helpbutton.png",
--this is the image to use when the button is pressed
over="helpbutton.png",
--this tells it what function to call when you press the button
onEvent = myButtonEvent
}
--This will create a button using text
local myButton=widget.newButton{
--The id can be used to tell you what button was pressed in your button event
id = "myTextButton",
--This is the text label to put on the button
label = "My Button",
--This is the start x coordinate of the Top Left Corner
left=150,
--This is the start y coordinate of the Top Left Corner
top=150,
--Emboss can be true or false, tells it to make the label look embossed/inset
emboss=true,
--The border around the outside of the button
strokeWidth=4,
--How round to make the corners
cornerRadius=8,
--this tells it what function to call when you press the button
onEvent = myButtonEvent
}
function update( )
--decrease the time counter
timeLeft = timeLeft - 1
scoreDisplay.text = 'Successful Vaccines ' .. playerPoints .. ' Time: ' .. timeLeft
--check if the time has run out
if (timeLeft <= 0) then
--display the final score
scoreDisplay.text = 'Successful Vaccines: ' .. playerPoints
--remove all of the screen objects
display.remove(cannonBase)
display.remove(cannonBarrel)
display.remove(target)
display.remove(upButton)
display.remove(downButton)
display.remove(fireButton)
--display the 'game over' sign
gameOver = display.newImage('gameover.png', 150, 75)
gameOver:addEventListener('touch', closeDownApp)
end
--check if the target has been hit
if (targetHit == true) then
targetHit = false
playerPoints = playerPoints + 1
Hit = display.newImage('Hit.png', target.x, target.y)
--replace the target with the hit picture
transition.dissolve(Hit, target, 1000, 0)
display.remove(bullet)
--put the target back to a random position
target.x = math.random(200, 450 )
target.y = math.random(100, 290 )
end
end
function onCollide(event)
targetHit=true
end
function fire(event)
--only fire at the beginning of a touch event
if (event.phase == 'began') then
media.playSound('bang.wav')
bullet = display.newImage('vaccine.png')
--move the image
bullet.x, bullet.y = cannonBarrel:localToContent(70, 0)
bullet.rotation = cannonRotation
--apply physics to the vaccine
physics.addBody( bullet, { density=2.0, friction=0.2, radius=15 } )
--determine the appropriate ratio of horizontal to vertical force
force_x = math.cos(math.rad(cannonRotation)) * cannonForce
force_y = math.sin(math.rad(cannonRotation)) * cannonForce
--fire the vaccine
bullet:applyForce( force_x, force_y, bullet.x, bullet.y )
end
end
function moveDown(event)
--only move the barrel if the touch event started
if (event.phase == 'began') then
cannonRotation = cannonRotation + rotateAmt
if (cannonRotation >= rotateMax) then
cannonRotation = rotateMax
end
cannonBarrel.rotation = cannonRotation
fireButton.rotation = cannonRotation
end
end
function moveUp(event)
--only move the barrel if the touch event started
if (event.phase == 'began') then
cannonRotation = cannonRotation - rotateAmt
if (cannonRotation <= rotateMin) then
cannonRotation = rotateMin
end
cannonBarrel.rotation = cannonRotation
fireButton.rotation = cannonRotation
end
end
function makeTarget( )
target = display.newImage('target.png')
target.x = math.random(200, 450)
target.y = math.random(100, 290)
physics.addBody(target,{density=1.0, friction=0.5, bounce=0.05, radius=15})
target.bodyType = 'static'
target:addEventListener('collision', onCollide)
end
function makeInterface( )
--up button
upButton = display.newImage('up_button.png')
upButton:translate(10, 70)
upButton:addEventListener('touch', moveUp)
--down button
downButton = display.newImage('down_button.png')
downButton:translate(10, 195)
downButton:addEventListener('touch', moveDown)
--fire button
fireButton = display.newImage('fire_button.png')
fireButton:translate(4, 145)
fireButton:addEventListener('touch', fire)
--display cannon parts
cannonBarrel = display.newImage('cannon_barrel.png')
cannonBarrel:translate(75, 220)
cannonBase = display.newImage('cannon_base.png')
cannonBase:translate(70, 220)
--display score
scoreDisplay = display.newText( ('Points: ' .. playerPoints), 0, 0, native.systemFont, 20 )
scoreDisplay:setFillColor( 255,0,0 )
scoreDisplay:translate(display.contentHeight /2, 30)
end
--Define control structure
function init( )
display.setStatusBar(display.HiddenStatusBar)
background = display.newImage('bg.png')
physics = require('physics')
physics.setDrawMode('normal')
physics.start( )
makeInterface( )
makeTarget( )
Runtime:addEventListener('enterFrame', update)
end
--Call the code
init( )
I have no knowledge about lua code all of this is copied from online so can someone explain like there explaining to an 80 year old
--displays title screen
local titleBg
local playBtn
local creditsBtn
local titleView
--Author: Adam St Clair
--App: Corona Destroyer
--Date: March 2021
--Define the variables
rotateAmt = 5
rotateMin = -90
rotateMax = 40
cannonRotation = 0
cannonForce = 1200
playerPoints = 0
targetHit = false
timeLeft = 1000
--Define the functions
function closeDownApp(event)
os.exit ( )
end
--This adds the widget library to our project
local widget = require "widget"
--This is what is run when we press our button
local myButtonEvent = function (event )
if event.phase == "release" then
print( "You pressed and released the "..event.target.id.." button!" )
end
end
--This will create a button using images
local myButton=widget.newButton{
--The id can be used to tell you what button was pressed in your button event
id = "mainMenuButton",
--this is the default button image
default="helpbutton.png",
--this is the image to use when the button is pressed
over="helpbutton.png",
--this tells it what function to call when you press the button
onEvent = myButtonEvent
}
--This will create a button using text
local myButton=widget.newButton{
--The id can be used to tell you what button was pressed in your button event
id = "myTextButton",
--This is the text label to put on the button
label = "My Button",
--This is the start x coordinate of the Top Left Corner
left=150,
--This is the start y coordinate of the Top Left Corner
top=150,
--Emboss can be true or false, tells it to make the label look embossed/inset
emboss=true,
--The border around the outside of the button
strokeWidth=4,
--How round to make the corners
cornerRadius=8,
--this tells it what function to call when you press the button
onEvent = myButtonEvent
}
function update( )
--decrease the time counter
timeLeft = timeLeft - 1
scoreDisplay.text = 'Successful Vaccines ' .. playerPoints .. ' Time: ' .. timeLeft
--check if the time has run out
if (timeLeft <= 0) then
--display the final score
scoreDisplay.text = 'Successful Vaccines: ' .. playerPoints
--remove all of the screen objects
display.remove(cannonBase)
display.remove(cannonBarrel)
display.remove(target)
display.remove(upButton)
display.remove(downButton)
display.remove(fireButton)
--display the 'game over' sign
gameOver = display.newImage('gameover.png', 150, 75)
gameOver:addEventListener('touch', closeDownApp)
end
--check if the target has been hit
if (targetHit == true) then
targetHit = false
playerPoints = playerPoints + 1
Hit = display.newImage('Hit.png', target.x, target.y)
--replace the target with the hit picture
transition.dissolve(Hit, target, 1000, 0)
display.remove(bullet)
--put the target back to a random position
target.x = math.random(200, 450 )
target.y = math.random(100, 290 )
end
end
function onCollide(event)
targetHit=true
end
function fire(event)
--only fire at the beginning of a touch event
if (event.phase == 'began') then
media.playSound('bang.wav')
bullet = display.newImage('vaccine.png')
--move the image
bullet.x, bullet.y = cannonBarrel:localToContent(70, 0)
bullet.rotation = cannonRotation
--apply physics to the vaccine
physics.addBody( bullet, { density=2.0, friction=0.2, radius=15 } )
--determine the appropriate ratio of horizontal to vertical force
force_x = math.cos(math.rad(cannonRotation)) * cannonForce
force_y = math.sin(math.rad(cannonRotation)) * cannonForce
--fire the vaccine
bullet:applyForce( force_x, force_y, bullet.x, bullet.y )
end
end
function moveDown(event)
--only move the barrel if the touch event started
if (event.phase == 'began') then
cannonRotation = cannonRotation + rotateAmt
if (cannonRotation >= rotateMax) then
cannonRotation = rotateMax
end
cannonBarrel.rotation = cannonRotation
fireButton.rotation = cannonRotation
end
end
function moveUp(event)
--only move the barrel if the touch event started
if (event.phase == 'began') then
cannonRotation = cannonRotation - rotateAmt
if (cannonRotation <= rotateMin) then
cannonRotation = rotateMin
end
cannonBarrel.rotation = cannonRotation
fireButton.rotation = cannonRotation
end
end
function makeTarget( )
target = display.newImage('target.png')
target.x = math.random(200, 450)
target.y = math.random(100, 290)
physics.addBody(target,{density=1.0, friction=0.5, bounce=0.05, radius=15})
target.bodyType = 'static'
target:addEventListener('collision', onCollide)
end
function makeInterface( )
--up button
upButton = display.newImage('up_button.png')
upButton:translate(10, 70)
upButton:addEventListener('touch', moveUp)
--down button
downButton = display.newImage('down_button.png')
downButton:translate(10, 195)
downButton:addEventListener('touch', moveDown)
--fire button
fireButton = display.newImage('fire_button.png')
fireButton:translate(4, 145)
fireButton:addEventListener('touch', fire)
--display cannon parts
cannonBarrel = display.newImage('cannon_barrel.png')
cannonBarrel:translate(75, 220)
cannonBase = display.newImage('cannon_base.png')
cannonBase:translate(70, 220)
--display score
scoreDisplay = display.newText( ('Points: ' .. playerPoints), 0, 0, native.systemFont, 20 )
scoreDisplay:setFillColor( 255,0,0 )
scoreDisplay:translate(display.contentHeight /2, 30)
end
--Define control structure
function init( )
display.setStatusBar(display.HiddenStatusBar)
background = display.newImage('bg.png')
physics = require('physics')
physics.setDrawMode('normal')
physics.start( )
makeInterface( )
makeTarget( )
Runtime:addEventListener('enterFrame', update)
end
--Call the code
init( )
I have no knowledge about lua code all of this is copied from online so can someone explain like there explaining to an 80 year old