I am having problems with my Lua. I an making a MOTD for my server using HTML that I am going to host with my website.
I have my frames set up and the according tabs, as seen
But this is my problem
As you exit the MOTD by pressing 'accept', the page set in the code stays up as seen and invades your screen like hell. My code is as follows:
function Welcome ()
MainMenu = vgui.Create( "DFrame" )
MainMenu:SetPos( 350, 100 )
MainMenu:SetSize( 1200, 900 )
MainMenu:SetTitle( "Welcome Menu" )
MainMenu:SetBackgroundBlur( true )
MainMenu:SetVisible( true )
MainMenu:SetDraggable( true )
MainMenu:ShowCloseButton( false )
MainMenu:MakePopup()
PropertySheet = vgui.Create( "DPropertySheet")
PropertySheet:SetParent( MainMenu )
PropertySheet:SetPos( 5, 30 )
PropertySheet:SetSize( 1190, 820 )
local DermaButton = vgui.Create( "DButton", DermaPanel )
DermaButton:SetText( "Agree" )
DermaButton:SetSize( 165, 30 )
DermaButton:SetPos( 590, 860 )
DermaButton:SetParent( MainMenu )
DermaButton.DoClick = function()
MainMenu:Close()
RunConsoleCommand( "say", "I have read and agree with the rules")
end
local DermaButton = vgui.Create( "DButton", DermaPanel )
DermaButton:SetText( "Disagree" )
DermaButton:SetSize( 165, 30 )
DermaButton:SetPos( 420, 860 )
DermaButton:SetParent( MainMenu )
DermaButton.DoClick = function()
MainMenu:Close()
surface.PlaySound( "buttons/button8.wav" )
RunConsoleCommand( "say", "I disagree with the rules, I will now be disconnected.")
RunConsoleCommand( "disconnect" )
end
local TabOne = vgui.Create( "DPanel" )
TabOne:SetVisible( true )
local HTML = vgui.Create("HTML")
HTML:SetPos(50,50)
HTML:SetSize(ScrW() - 100, ScrH() - 100)
HTML:OpenURL("http://www.garrysmod.com")
local TabThree = vgui.Create( "DPanel" )
TabThree:SetVisible( true )
local HTML = vgui.Create("HTML")
HTML:SetPos(50,50)
HTML:SetSize(ScrW() - 100, ScrH() - 100)
HTML:OpenURL("http://www.garrysmod.com")
local TabTwo = vgui.Create( "DPanel" )
TabTwo:SetVisible( true )
local HTML = vgui.Create("HTML")
HTML:SetPos(50,50)
HTML:SetSize(ScrW() - 100, ScrH() - 100)
HTML:OpenURL("http://www.garrysmod.com")
local TabFour = vgui.Create( "DPanel" )
TabThree:SetVisible( true )
local HTML = vgui.Create("HTML")
HTML:SetPos(50,50)
HTML:SetSize(ScrW() - 100, ScrH() - 100)
HTML:OpenURL("http://www.garrysmod.com")
local TabFive = vgui.Create( "DPanel" )
TabThree:SetVisible( true )
local HTML = vgui.Create("HTML")
HTML:SetPos(50,50)
HTML:SetSize(ScrW() - 100, ScrH() - 100)
HTML:OpenURL("http://www.garrysmod.com")
PropertySheet:AddSheet( "Home", TabOne, "gui/silkicons/heart",
false, false, "Welcome to the server!" )
PropertySheet:AddSheet( "Rules", TabTwo, "gui/silkicons/script",
false, false, "Server rules!" )
PropertySheet:AddSheet( "Group", TabThree, "gui/silkicons/group",
false, false, "Group page!" )
PropertySheet:AddSheet( "Ranks", TabFour, "gui/silkicons/flag_blue",
false, false, "Ranks and donations!" )
PropertySheet:AddSheet( "Admins", TabFive, "gui/silkicons/shield",
false, false, "Your admin squad!" )
end
concommand.Add("OpenMotd", Welcome )
If you need to contact me fast, I can be found on Steam, under steamcommunity.com/id/PonyTerrance
This is happening, because you are not closing the HTML object anywhere in the code.
Also remember, that you are creating multiple HTML objects with the same variable name - Either choose an unique variable name for each one or simply create one HTML object and remove the other code passages which are creating HTML objects.
Related
function Team_Swap()
local Backg = vgui.Create( "DFrame" )
Backg:SetSize( ScrW() / 2, ScrH() / 2 )
Backg:SetPos ( ScrW() / 2, ScrH() / 2 )
Backg:SetTitle( "Swap Teams" )
Backg:SetVisible ( true )
Backg:SetDraggable ( true )
Backg:ShowCloseButton ( true )
Backg:MakePopup()
local DColorButton = vgui.Create ( "DColorButton", Backg )
DColorButton:SetPos( 40, 40 )
DColorButton:Paint( 100, 40 )
DColorButton:SetSize( 100, 40 )
DColorButton:SetText( "Join Red Team", Color( 221,78,76 ) )
DColorButton:SetColor( Color( 221,78,76 )
function DColorButton:DoClick(player)
player:Kill()
player:SetTeam(1)
player:Spawn()
end
end
concommand.Add( "set_team", Team_Swap )
This code runs fine... until you do its only purpose CLICK THE BUTTON
the button when clicked returns the following text in console
newrecycle]set_team
[ERROR] gamemodes/capturetheflag/gamemode/cl_init.lua:32: attempt to index local 'player' (a nil value)
1. DoClick - gamemodes/capturetheflag/gamemode/cl_init.lua:32
2. unknown - lua/vgui/dlabel.lua:218
[n3wr3cycl3|20|STEAM_0:1:59994487] Lua Error:
[ERROR] gamemodes/capturetheflag/gamemode/cl_init.lua:32: attempt to index local 'player' (a nil value)
1. DoClick - gamemodes/capturetheflag/gamemode/cl_init.lua:32
2. unknown - lua/vgui/dlabel.lua:218
please help!
First of all: You are mixing Clientside (vgui) and Serverside (Player:SetTeam()) Stuff.
(Explanation why you are running in this Error is in the Clientside Script)
My suggestion:
Clientside Script:
function Team_Select()
local Backg = vgui.Create( "DFrame" )
Backg:SetSize( ScrW() / 2, ScrH() / 2 )
Backg:SetPos ( ScrW() / 2, ScrH() / 2 )
Backg:SetTitle( "Swap Teams" )
Backg:SetVisible ( true )
Backg:SetDraggable ( true )
Backg:ShowCloseButton ( true )
Backg:MakePopup()
local TeamRedButton = vgui.Create ( "DColorButton", Backg )
TeamRedButton:SetPos( 40, 40 )
TeamRedButton:Paint( 100, 40 )
TeamRedButton:SetSize( 100, 40 )
TeamRedButton:SetText( "Join Red Team", Color( 221,78,76 ) )
TeamRedButton:SetColor( Color( 221,78,76 )
function TeamRedButton:DoClick() -- DoClick has 0 Params
RunConsoleCommand("switchteam", "red")
end
end
concommand.Add( "chooseteam", Team_Select )
And Serverside Script:
function Team_Switch( --[[ Player ]] player, --[[ String ]] cmd, --[[ Table ]] args)
-- Get the Parameter out of the Table (in my example "red" for the red team)
-- and move the player to the choosen Team
end
concommand.Add("switchteam", Team_Switch)
The Clientside Script is just the GUI for the user. The actual Teamswitch is handled by the server and can be initialized from the Client console as well.
The user is able to execute switchteam red directly
Source: My Experience and the GMod Wiki
I attempt to get data (in a global variable) from user through a textbox from the user in functions, and use these variables to insert into a sqlite table.
But, when I attempt to do so, I get an error like "attempt to call global locationGlobal (a nil value)" in the INSERT INTO line, after I click the submit button.
NOTE : I input values in the textbox by using Bluestacks android simulator as inputting the text is not possible in windows Corona SDK.
Here is my code :
local widget = require("widget")
require "sqlite3"
local path = system.pathForFile("testUser.db", system.DocumentsDirectory)
db = sqlite3.open( path )
--local location,area,arrivalTime,departTime,eventAttended
local function onSystemEvent( event )
if( event.type == "applicationExit" ) then
db:close()
end
end
--local tablesetup = [[CREATE TABLE IF NOT EXISTS visitPlace (id INTEGER PRIMARY KEY autoincrement, location, area, arrivalTime, departTime, eventAttended);]]
local tablesetup = [[CREATE TABLE place (id INTEGER PRIMARY KEY autoincrement, location);]]
print(tablesetup)
db:exec( tablesetup )
_W = display.viewableContentWidth
_H = display.viewableContentHeight
local background = display.newRect(0,0,_W,_H)
background:setFillColor(0,0,0)
local function textListenerLocation( event )
if ( event.phase == "began" ) then
-- user begins editing defaultField
event.target.text = ''
print(location)
print( event.text )
elseif ( event.phase == "ended" ) then
-- do something with defaultField text
locationGlobal = tostring( event.target.text)
elseif ( event.phase == "editing" ) then
print( event.newCharacters )
print( event.oldText )
print( event.startPosition )
print( event.text )
elseif ( event.phase == "submitted" ) then
locationGlobal =tostring( event.target.text)
--local label = display.newText( location, 180, 30, native.systemFontBold, 20 )
-- label:setFillColor( 190/255, 190/255, 1 )
end
end
local function SubmitEvent( event )
--local label = display.newText( location, 180, 30, native.systemFontBold, 20 )
--label:setFillColor( 190/255, 190/255, 1 )
local insertionTable = [[INSERT INTO visitPlace VALUES(NULL,']]..locationGlobal..[[) ]]
db:exec(insertionTable)
for row in db:nrows("SELECT * FROM visitPlace") do
local text = row.location
local t = display.newText(text, 450, 120*row.id, native.systemFont, 40)
t:setFillColor(1,0,1)
end
local label1 = display.newText( "Submitted", 180, 30, native.systemFontBold, 20 )
label1:setFillColor( 190/255, 190/255, 1 )
end
function background:tap( event )
native.setKeyboardFocus(nil)
end
local locationTxtbox = native.newTextField(180,140,280,100)
locationTxtbox.size = 34
locationTxtbox:addEventListener("textListenerLocation",locationTxtbox)
local submitButton = widget.newButton
{
label = "Submit",
onEvent = SubmitEvent,
shape = "roundedRect",
width = 100,
height = 30,
cornerRadius = 2
}
submitButton.x = display.contentCenterX + (display.contentCenterX/2)
submitButton.Y = display.contentCenterY + (display.contentCenterY/2)
background:addEventListener("tap",background)
Runtime:addEventListener( "system", onSystemEvent )
--defaultField = native.newTextField( 150, 150, 180, 30 )
--defaultField:addEventListener( "userInput", textListener )
currently your textlistenerlocation function is not being called. So when you hit the submit button, locationGlobal has not yet been defined.
The following looks suspicious:
locationTxtbox:addEventListener("textListenerLocation",locationTxtbox)
the addEventListener should take an event string and then a function to call when the event happens. textListenerLocation is your event handling function, it is not the event string.
locationTxtbox:addEventListener("userInput", textListenerLocation)
I'm bundling storyboard and i want user to move to next scene after filling fields and then press a button
I have a problem with the btn ,---------------------------------------------------------------------------------
-- scence2.lua
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
-- BEGINNING OF YOUR IMPLEMENTATION
local widget = require( "widget" )
require("hijacks")
local sysFonts = native.getFontNames()
local tHeight -- forward reference
local roundedRect = display.newRoundedRect( 40, 120, 200, 40, 8 )
roundedRect:setFillColor( 0, 0, 0, 170 )
local t = display.newText( "Waiting for button event...", 0, 0, "AmericanTypewriter-Bold", 18 )
t.x, t.y = display.contentCenterX, 70
local function fieldHandler( textField )
return function( event )
if ( "began" == event.phase ) then
-- This is the "keyboard has appeared" event
-- In some cases you may want to adjust the interface when the keyboard appears.
elseif ( "ended" == event.phase ) then
-- This event is called when the user stops editing a field: for example, when they touch a different field
elseif ( "editing" == event.phase ) then
elseif ( "submitted" == event.phase ) then
-- This event occurs when the user presses the "return" key (if available) on the onscreen keyboard
print( textField().text )
-- Hide keyboard
native.setKeyboardFocus( nil )
end
end
end
-- Predefine local objects for use later
local nameField, phoneField
local fields = display.newGroup()
-- Note: currently this feature works in device builds or Xcode simulator builds only (also works on Corona Mac Simulator)
local isAndroid = "Android" == system.getInfo("platformName")
local inputFontSize = 18
local inputFontHeight = 30
tHeight = 30
if isAndroid then
-- Android text fields have more chrome. It's either make them bigger, or make the font smaller.
-- We'll do both
inputFontSize = 14
inputFontHeight = 42
tHeight = 40
end
nameField = native.newTextField( 40, 120, 200, tHeight )
nameField.font = native.newFont( native.systemFontBold, inputFontSize )
nameField:addEventListener( "userInput", fieldHandler( function() return nameField end ) )
phoneField = native.newTextField( 40, 160, 200, tHeight )
phoneField.font = native.newFont( native.systemFontBold, inputFontSize )
phoneField.inputType = "phone"
phoneField:addEventListener( "userInput", fieldHandler( function() return phoneField end ) )
-- Add fields to our new group
fields:insert(nameField)
fields:insert(phoneField)
-- * Add field labels *
local defaultLabel = display.newText( "الاسم", 250, 120, native.systemFont, 18 )
defaultLabel:setTextColor( 255, 0, 0 )
local defaultLabel = display.newText( "رقم الجوال", 250, 160, native.systemFont, 18 )
defaultLabel:setTextColor( 255, 0, 0 )
-- These are the functions triggered by the buttons
local button1Press = function( event )
t.text = "Button 1 pressed"
end
local button1Release = function( event )
t.text = "Button 1 released"
end
local buttonHandler = function( event )
t.text = "id = " .. event.target.id .. ", phase = " .. event.phase
end
-- This button has individual press and release functions
-- (The label font defaults to native.systemFontBold if no font is specified)
local button1 = widget.newButton{
default = "buttonRed.png",
over = "buttonRedOver.png",
onPress = button1Press,
onRelease = button1Release,
label = "موافق",
emboss = true
}
local button2 = widget.newButton{
default = "buttonBlue.png",
over = "buttonBlue.png",
onEvent = buttonHandler,
onPress = button1Press,
onRelease = button1Release,
label = "ok",
emboss = true;
labelColor = { default = { 51, 51, 51, 255 } },
fontSize = 32,
}
button1.x = 160; button1.y = 320
-- These are the functions triggered by the buttons
local button1Press = function( event )
nameField.text = "Button 1 pressed"
nameField.text = "hello"
storyboard.gotoScene( true, "scene3", "fade", 800 )
end
-- local button1Release = function( event )
-- nameField.text = "Button 1 released"
-- end
local funcction buttonHandler = function( event )
-- nameField.text = "hello"
-- storyboard.gotoScene( true, "scene3", "fade", 800 )
local background = display.newImage("buttonBlue.png", true) -- flag overrides large image downscaling
background.x = display.contentWidth / 2
background.y = display.contentHeight / 2
end
local image, text1, text2, text3
local function onSceneTouch( self, event )
if event.phase == "began" then
-- first argument means show native activity indicator while transitioning
--storyboard.gotoScene( true, "scene3", "fade", 800 )
return true
end
end
-- Called when the scene's view does not exist:
function scene:createScene( event )
local screenGroup = self.view
image = display.newImage( "bg2.jpg" )
screenGroup:insert( image )
image.touch = onSceneTouch
text1 = display.newText( "Scene 2", 0, 0, native.systemFontBold, 24 )
text1:setTextColor( 255 )
text1:setReferencePoint( display.CenterReferencePoint )
text1.x, text1.y = display.contentWidth * 0.5, 270
screenGroup:insert( text1 )
text2 = display.newText( "MemUsage: ", 0, 0, native.systemFont, 16 )
text2:setTextColor( 255 )
text2:setReferencePoint( display.CenterReferencePoint )
text2.x, text2.y = display.contentWidth * 0.5, display.contentHeight * 0.5
screenGroup:insert( text2 )
text3 = display.newText( "Touch to continue.", 0, 0, native.systemFontBold, 18 )
text3:setTextColor( 255 ); text3.isVisible = false
text3:setReferencePoint( display.CenterReferencePoint )
text3.x, text3.y = display.contentWidth * 0.5, display.contentHeight - 100
screenGroup:insert( text3 )
print( "\n2: createScene event" )
end
-- Called immediately after scene has moved onscreen:
function scene:enterScene( event )
print( "2: enterScene event" )
-- remove previous scene's view
storyboard.purgeScene( "scene1" )
-- Update Lua memory text display
local showMem = function()
image:addEventListener( "touch", image )
text3.isVisible = true
text2.text = text2.text .. collectgarbage("count")/1000 .. "MB"
text2.x = display.contentWidth * 0.5
end
local memTimer = timer.performWithDelay( 1000, showMem, 1 )
end
-- Called when scene is about to move offscreen:
function scene:exitScene()
print( "2: exitScene event" )
-- remove touch listener for image
image:removeEventListener( "touch", image )
-- reset label text
text2.text = "MemUsage: "
end
-- Called prior to the removal of scene's "view" (display group)
function scene:destroyScene( event )
print( "((destroying scene 2's view))" )
end
-- END OF YOUR IMPLEMENTATION
-- "createScene" event is dispatched if scene's view does not exist
scene:addEventListener( "createScene", scene )
-- "enterScene" event is dispatched whenever scene transition has finished
scene:addEventListener( "enterScene", scene )
-- "exitScene" event is dispatched before next scene's transition begins
scene:addEventListener( "exitScene", scene )
-- "destroyScene" event is dispatched before view is unloaded, which can be
-- automatically unloaded in low memory situations, or explicitly via a call to
-- storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( "destroyScene", scene )
return scene it is not move to next scene..
the action works fine, it show testing message..
enter code here
Trying to get a sprite (instance1) to jump on touch, but it won't work.
Here is my code:
physics.addBody( instance1, { density=1.0, friction=0.3, bounce=0.3} )
local function jump( event )
if(event.numTaps == 2) then
instance1:applyForce( 350, -2000, instance1.x, instance1.y )
end
end
instance1:addEventListener("tap", jump)
I will add, that if I do this, the sprite instance will jump once, but never again:
local function jump( event )
if(event.numTaps == 2) then
physics.addBody( instance1, { density=1.0, friction=0.3, bounce=0.3} )
instance1:applyForce( 350, -2000, instance1.x, instance1.y )
end
end
instance1:addEventListener("tap", jump)
Instance info:
local sheet1 = sprite.newSpriteSheet( "character.png", 75, 105 )
local spriteSet1 = sprite.newSpriteSet(sheet1, 1, 16)
sprite.add( spriteSet1, "character", 1, 12, 700, 1 ) -- play 12 frames every 700 ms
local instance1 = sprite.newSprite( spriteSet1 )
instance1.x = display.contentWidth/2
instance1.y = 240
still cant reproduce your error,
i answered your question to paste the code i tested:
--main.lua
local sprite=require("sprite")
local physics=require("physics")
local sheet1 = sprite.newSpriteSheet( "character.png", 75, 105 )
local spriteSet1 = sprite.newSpriteSet(sheet1, 1, 16)
sprite.add( spriteSet1, "character", 1, 12, 700, 1 ) -- play 12 frames every 700 ms
local instance1 = sprite.newSprite( spriteSet1 )
instance1.x = display.contentWidth/2
instance1.y = 240
physics.start()
physics.setGravity(0,1)
physics.addBody( instance1, { density=1.0, friction=0.3, bounce=0.3} )
local function jump( event )
if(event.numTaps == 2) then
instance1:applyForce( 35, -200, instance1.x, instance1.y )
end
end
instance1:addEventListener("tap", jump)
First time post and I sincerely apologize if this has been answered in any other post but I have not been able to find a resolution for the problem I'm facing on this or any other site. I'm a new programmer self-teaching with web tutorials and any other resource I have found. I am trying to create code which will spawn characters and allow you to call them. I've had trouble assigning an index value to the individual instances I have created with a for function. I have tried to establish the instance as both a table and a group display object. If anyone is able to point me in the direction of any resources to get a more indepth understanding of tables and group display objects for the Corona SDK implementation of Lua I'm sure that my problem is that I don't have a thorough enough understanding of these particular functionalities and how they work. Here is the code I've written so far.
-- Character Game
require "sprite"
require "ui"
local background = display.newImage("Background Placeholder.png")
halfW = display.viewableContentWidth / 2
halfH = display.viewableContentHeight / 2
local numCharacters = 20
local roundedRect = display.newRoundedRect( 365, 20, 110, 40, 8 )
roundedRect:setFillColor( 0, 255, 0, 80 )
score = 0
t = ui.newLabel{ bounds = { 370, 30, 100, 40 },
text = "Score " .. score,
textColor = { 255, 0, 20, 255 },
size = 18,
align = "center"
}
local scoreboard = function ( event )
t:setText( "Score " .. score )
end
Runtime:addEventListener( "enterFrame", scoreboard )
local group = display.newGroup()
local character = sprite.newSpriteSheetFromData( "Character Placeholder.png", require("Character Placeholder").getSpriteSheetData() )
local characterSet1 = sprite.newSpriteSet(character,1,8)
sprite.add(characterSet1,"character",1,8,1500,0)
local characterplay = function( event )
score = score + group.points
group[i]:removeSelf()
end
do
for i=1, numCharacters do
group:insert(sprite.newSprite(characterSet1))
group[i].xScale = .2
group[i].yScale = .2
group.points = 50
group[i]:setReferencePoint ( display.BottomCenterReferencePoint )
group[i]:translate( halfW + math.random( -100, 100 ), halfH + math.random( -130, -110 ) )
end
timer.performWithDelay( 500, charactermovie )
for i=1, 21 do
timer.performWithDelay( math.random( 500, 5000 ) , charactermove )
charactermove = function(event )
transition.to( group[i], { time=10000, y = 580 } )
transition.to( group[i], { time=8000, x = math.random( 0, 480 ) } )
transition.to( group[i], { time=7000, xScale = 1.5} )
transition.to( group[i], { time=7000, yScale = 1.5} )
group[i]:prepare("character")
group[i]:play()
end
group[i]:addEventListener( "tap", characterplay )
end
end
charactermovie = function( event )
group[i]:prepare("character")
group[i]:play()
end
local function spriteListener( event )
print( "Sprite event: ", event.sprite, event.sprite.sequence, event.phase )
end
for i, group in pairs(group) do print (group, i, v) end
I'm currently getting an "attempt to index field '?' at the line containing this code "group[i]:addEventListener( "tap", zombieplay )" upon launch and a "nil key supplied for property lookup" error at the "group[i]:removeSelf()" line of the "zombieplay" function. I've tried moving the offending code to a variety of locations to see if this is a scoping issue but I largely run into the same error and believe I may not properly understand indexes and keys... I've found that the app functions as intended but I have to call index keys 1 through 21 to get them all to move even though I am only calling for 20 characters and the removeSelf line throwing errors is not removing the individual characters. I'm going to try writing a module for the characters and see if that helps clear any of this up. I will post my results shortly.
Your characterplay and charactermovie functions are trying to use the variable i, this is outside the functions scope.
There is a property of event called target, which is used to get the event callee. You want to do something like this:
local characterplay = function( event )
score = score + group.points
event.target:removeSelf()
end