• Announcement: Lua.org now officially recommends this forum as a meeting place for the Lua community

Help with adding a button (1 Viewer)

adamstc222

Newcomer
Joined
Apr 21, 2021
Messages
1
Reaction score
0
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
 

dinsdale247

Moderator
Staff member
Community Patron
Creator of WinLua
Joined
Nov 17, 2020
Messages
91
Reaction score
32
Location
Victoria BC
Website
winlua.net
Hi @adamstc222,

I appreciate your enthusiasm and I really enjoy helping people but what you've asked is sort of impossible. You see, you don't even know how much you don't know.

Rather than try to reverse engineer this, I would recommend you spend $25 on the Programming In Lua book and download my WinLua - Lua Only installer. Then you can start at the beginning and learn the language before you jump into the middle of something very complicated.

I'd be happy to help if you go and put a little effort into your project. However, I have no interest in teaching 80 year old's to program.

Cheers,
d
 
Top