Related
Im new to lua and gmod, and coding at all, i create a SENT, that when you press at entity you open the derma and can choose what you want, but problem is here, that all players on the server see the derma when somebody use entity. I really get stucked here
shared.lua
ENT.Type = "anim"
ENT.Base = "base_gmodentity"
ENT.PrintName = "Гроб Пидораса"
ENT.Spawnable = true
ENT.Category = "Кладбище"
int.lua
AddCSLuaFile("shared.lua")
include("shared.lua")
util.AddNetworkString("sendtomogila")
function ENT:Initialize()
self:SetModel("models/props_c17/gravestone_coffinpiece001a.mdl")
self:PhysicsInit( SOLID_VPHYSICS )
self:SetMoveType( MOVETYPE_VPHYSICS)
self:SetSolid(SOLID_VPHYSICS)
local phys = self:GetPhysicsObject()
if (phys:IsValid()) then phys:Wake() end
end
function ENT:Use(ply)
self:SetUseType(3)
if ply:IsPlayer() then
net.Start("sendtomogila")
net.WriteEntity(ply)
net.Broadcast()
end
end
cl_init.lua
include("shared.lua")
surface.CreateFont("bigHud", {
font = "Open Sans Bold",
size = 250,
weight = 800,
antialias = true
} )
surface.CreateFont("bigHud1", {
font = "Open Sans Bold",
size = 30,
weight = 800,
outline = true
} )
function ENT:Draw()
self:DrawModel()
cam.Start3D2D(self:GetPos() + Vector(0,0,50), Angle(0, RenderAngles().y - 90,90), 0.1)
draw.SimpleTextOutlined("Могила Пидора", "bigHud", 0, 0, Color(255,255,255), TEXT_ALIGN_CENTER, 0, 2, Color(0,0,0))
cam.End3D2D()
end
net.Receive("sendtomogila", function(len, ply)
local gav = net.ReadEntity()
if (!Frame) then
print(gav)
local Frame = vgui.Create( "DFrame" ) --Создаёт окно.
Frame:SetTitle( "ЭТО ОКНО ВИДЯТ ТОЛЬКО МУЖИКИ" ) --Устанавливает название окна.
Frame:SetPos( 100,100 ) --Устанавливает позицию окна.
Frame:SetSize( ScrW() * 300/1920, ScrH() * 200/1080 )
Frame:Center()
Frame:SetSizable(true) --/Если True, окно можно будет растянуть.
Frame:SetVisible( true ) --Если True, окно сразу включается при запуске функции, при false будет ждать команду на запуск.
Frame:SetDraggable( true ) --//Если True, окно можно перетаскивать.
Frame:ShowCloseButton( true ) --//Если True, у окна есть кнопка закрытия (не советую убирать на время тестов).
Frame:MakePopup(true) --//Если True, экран блокируется и появляется курсор.
local Text = vgui.Create("DLabel")
Text:SetParent( Frame ) --Устанавливает родительский элемент(то, на чём распологается кнопка).
Text:SetPos(20, 50)
Text:SetColor(Color(255,255,255,255)) --Устанавливает цвет текста
Text:SetFont("default") --Устанавливает шрифт текста. (можно использовать только те шрифты, которые доступны в игре)
Text:SetText("ТЫ ПИДОР?") --Устанавливает текст
Text:SizeToContents() --Автоматически подогнать размер (если размер будет меньше текста, он оборвётся многоточием)
local Knopka1 = vgui.Create( "DButton", Frame ) --//Создаёт кнопку.
local Knopka2 = vgui.Create( "DButton", Frame )
Knopka1:SetText( "НЕТ" ) --//Устанавливает текст на кнопке.
Knopka1:SetPos( 160, 130 )--Устанавливает позицию кнопка.
Knopka1:SetSize( ScrW() * 100/1920, ScrH() * 30/1080 ) --//Устанавливает размер кнопки.
Knopka1.DoClick = function () --//Функция, выполняющую кнопка.
Knopka2:SetEnabled(false)
RunConsoleCommand( "kill" )-- //То, что делает кнопка. В данном случае включает анимацию (насмешка/жест).
Knopka1:SetText( "ПИДОРА ОТВЕТ" )-- //То, что делает кнопка. В данном случае меняет текст на себе.
end --//Конец функции кнопки.
Knopka2:SetText( "ДА" ) --//Устанавливает текст на кнопке.
Knopka2:SetPos( 40, 130 )--Устанавливает позицию кнопка.
Knopka2:SetSize( ScrW() * 100/1920, ScrH() * 30/1080 ) --//Устанавливает размер кнопки.
Knopka2.DoClick = function () --//Функция, выполняющую кнопка.
Knopka1:SetEnabled(false)
RunConsoleCommand( "kill" )-- //То, что делает кнопка. В данном случае включает анимацию (насмешка/жест).
Knopka2:SetText( "ХУЙ В ЖОПУ НА" )-- //То, что делает кнопка. В данном случае меняет текст на себе.
end --//Конец функции кнопки.
elseif (Frame) then print("hi") end
end)
I tried to use if ply == ply then draw all derma, but it didnt help ;p
net.Broadcast() - Sends the currently built net message to all connected players.
you needed net.Send( Player ply ) - Sends the current message to the specified player, or to all players listed in the table.
I created 3 balloons(only need 3) and each of them contains a specific word and one of the words is the right answer. I shoot a ball from the cannon and hit the balloons. Assume the balloon 3 holds the balloonText3 which is the right answer, if i hit it the ball and the text disappears. For some reason, the ballCollision(event) doesn't run, i used print statement to confirm it. Whats wrong here?
local cannonBalls = display.newGroup()
...
local shot = function(event)
if(event.phase == 'ended') then
Runtime:removeEventListener('enterFrame', charge)
cannon.isVisible = true
cannon.rotation = 0
cannonBall = display.newImage('cannon ball.png', 84, 220)
physics.addBody(cannonBall, {density = 1, friction = 0, bounce = 0})
cannonBalls:insert(cannonBall)
print ('shot')
-- Shoot cannon ball
cannonBall:applyLinearImpulse(3, impulse, cannonBall.x, cannonBall.y )
--Collision listener
print('event listener')
cannonBall:addEventListener ('collision', ballCollision)
end
end
function ballCollision(event)
if (event.other.name =='balloon3') then
scene.updateScore()
print('Ball is colliding')
timer.performWithDelay(1, e.target.removeSelf, e.target)
timer.performWithDelay(1, e.other.removeSelf, e.target)
balloonText3.isVisible = false
audio.play(pop)
end
end
function scene:createScene(event)
local group = self.view
...
local balloon1 = display.newImage ('balloon_fat_red.png', 495, 60)
local balloon2 = display.newImage ('balloon_fat_red.png', 495, 115)
local balloon3 = display.newImage ('balloon_fat_red.png', 495, 160)
group:insert(balloon1)
group:insert(balloon2)
group:insert(balloon3)
physics.addBody(balloon1)
physics.addBody(balloon2)
physics.addBody(balloon3)
balloon1.bodyType = 'static'
balloon2.bodyType = 'static'
balloon3.bodyType = 'static'
table.insert(balloons, balloon1)
table.insert(balloons, balloon2)
table.insert(balloons, balloon3)
local balloonText1 = display.newText('\227\129\130', 495, 60)
balloonText1:setFillColor( 1,1, 0 )
local balloonText2 = display.newText('\227\129\132', 495, 115)
balloonText2:setFillColor( 1,1, 0 )
local balloonText3 = display.newText('\227\129\134', 495, 160)
balloonText3:setFillColor( 1,1, 0 )
group:insert(balloonText1)
group:insert(balloonText2)
group:insert(balloonText3)
end
I'm going to assume that you put the print statement in the if block of ballCollision, because I can't see anything else wrong with the listener registration. Also, I don't see anywhere in the code where you set the name attribute of the balloon object. So either
add balloon3.name = "balloon3" (and similarly for 1 and 2) after setting bodyType,
or (probably simpler)
use if event.other == balloons[3] then ... in the collision handler. You don't need parentheses around the condition in Lua.
The code below removes a balloon when a bullet hits it. I'm stuck on how to make the specific balloonTexts disappear when they are hit by the bullet. For example, I wanted to make the balloon that has 'balloonText2' disappear when the bullet hits it. How do I detect its the second balloon i hit?
function createBalloons(a, b)
for i = 1, a do
for j = 1, b do
local balloon = display.newImage ('balloon_fat_red.png', 465+ (i * 30), 80 + (j * 50))
balloonText1 = display.newText(hiragana_array[x+1], 495, 125)
balloonText2 = display.newText(hiragana_array[x+2], 495, 175)
balloonText3 = display.newText(hiragana_array[x+3], 495, 225)
balloonText1:setFillColor( 1,1, 0 )
balloonText2:setFillColor( 1,1, 0 )
balloonText3:setFillColor( 1,1, 0 )
balloon.name = 'balloon'
physics.addBody(balloon)
balloon.bodyType = 'static'
table.insert(balloons, balloon)
end
end
target.text = #balloons
end
--ball collides with balloon
function ballCollision(e)
if (e.other.name == 'balloon') then
e.target:removeSelf()
e.other:removeSelf()
audio.play(pop)
score.text = score.text + 50
score.anchorX = 0
score.anchorY = 0
score.x = 200
score.y = 50
target.text = target.text - 1
end
You need to keep track of the balloonText's. What I would consider doing is adding the display objects for the text the balloon object:
local balloon = display.newImage ('balloon_fat_red.png', 465+ (i * 30), 80 + (j * 50))
balloon.balloonText1 = display.newText(hiragana_array[x+1], 495, 125)
balloon.balloonText2 = display.newText(hiragana_array[x+2], 495, 175)
balloon.balloonText3 = display.newText(hiragana_array[x+3], 495, 225)
Then in your collision handler:
e.target:removeSelf()
e.other.balloonText1:removeSelf()
e.other.balloonText2:removeSelf()
e.other.balloonText3:removeSelf()
e.other:removeSelf()
or something like that.
Can you assign name property of the text object. Then print e.other.name in collision function and see what's the output. I think e.other is getting nil, that's why it's not removing from the display group.
I ran this code and it gave me an error attempt to index global 'self' (a nil value), in this scene i'm creating the Question1 of the game, inside which includes creating cannon, balloons and other game elements. I checked but i'm not sure whats wrong here.
function scene.createScene()
local group = self.view ---line 27 where i got the error
scoreText = display.newText( "0", 0, 0, globals.font.bold, 32 )
scoreText.x = display.contentCenterX
scoreText.y = 32
group:insert( scoreText )
background = display.newImage( "bkg_clouds.png")
group:insert(background)
background.x = 240
background.y = 195
questionText = display.newText('a', display.contentCenterX, display.contentWidth/4, native.systemFont, 40)
group:insert(questionText)
infoBar = display.newImage ("infoBar.png")
group:insert(infoBar)
background.x = 200
background.y = 100
restartBtn = display.newImage ("restartBtn.png")
group:insert(restartBtn)
background.x = 470
background.y = 300
cannon = display.newImage ("cannon.png")
group:insert(cannon)
background.x = 10
background.y = 270
cannon.anchorX = 0.5
cannon.anchorY = 0.5
restartBtn.isVisible = true
function createBalloons(a, b)
for i = 1, a do
for j = 1, b do
local balloon = display.newImage ('balloon_fat_red.png', 465+ (i * 30), 80 + (j * 50))
balloon.balloonText1 = display.newText(hiragana_array[x+1], 495, 125)
balloon.balloonText2 = display.newText(hiragana_array[x+2], 495, 175)
balloon.balloonText3 = display.newText(hiragana_array[x+3], 495, 225)
balloon.balloonText1:setFillColor( 1,1, 0 )
balloon.balloonText2:setFillColor( 1,1, 0 )
balloon.balloonText3:setFillColor( 1,1, 0 )
balloon.name = 'balloon'
physics.addBody(balloon)
balloon.bodyType = 'static'
table.insert(balloons, balloon)
end
end
target.text = #balloons
end
function cannonCharge:touch(e)
if(e.phase == 'began') then
impulse = 0
cannon.isVisible = true
Runtime:addEventListener('enterFrame', charge)
end
end
function charge()
local degreesPerFrame = 1
cannon.rotation = cannon.rotation - degreesPerFrame
impulse=impulse-0.2
if(cannon.rotation < -46) then
cannon.rotation = -46
impulse = -3.2
end
end
function shot:touch(e)
if(e.phase == 'ended') then
Runtime:removeEventListener('enterFrame', charge)
cannon.isVisible = false
cannon.rotation = 0
local cannonBall = display.newImage('cannon ball.png', 84, 220)
physics.addBody(cannonBall, {density = 1, friction = 0, bounce = 0})
group:insert(cannonBall)
-- Shoot cannon ball
cannonBall:applyLinearImpulse(dir, impulse, cannonBall.x, cannonBall.y )
--Collision listener
cannonBall:addEventListener ('collision', ballCollision)
end
end
function ballCollision(e)
if (e.other.name == 'balloon') then
scene.updateScore()
e.target:removeSelf()
print ('remove balloon text')
e.other:removeSelf()
audio.play(pop)
end
end
cannonBall:applyLinearImpulse(dir, impulse, cannonBall.x, cannonBall.y )
--Collision listener
cannonBall:addEventListener ('collision', ballCollision)
scene.view:insert( ballCollision )
end
You probably need function scene:createScene(). Note the colon instead of the dot in your original code.
Your function should be like this.
function scene:createScene( event )
local group = self.view
-----------------------------------------------------------------------------
-- CREATE display objects and add them to 'group' here.
-- Example use-case: Restore 'group' from previously saved state.
-----------------------------------------------------------------------------
end
Here is the reference:
http://docs.coronalabs.com/api/library/storyboard/
I'm trying to make a scoring system that displays an all time score across every scene, when a user completes a scene the score from that scene is added to the all time score.
I get 1 error in the compiler: Attempt to perform "arithmetic" on field "score"
code from main lua: contains no errors but not sure if correct.
-- 1 Clue 1 Word
display.setStatusBar (display.HiddenStatusBar)
local storyboard = require ( "storyboard" )
storyboard.gotoScene("menu")
storyboard.state = {}
storyboard.state.score = 0
storyboard.state.score = display.newText(storyboard.state.score, 250, 20, "ARIAL", 16)
<code>
The error appears in the line: storyboard.state.score = storyboard.state.score + 1
<code>
if(correct == #L1) then
--alert()
print ("CORRECT "..#L1)
line.isVisible = false
storyboard.gotoScene( "scene2", "slideLeft", 500)
storyboard.state.score = storyboard.state.score + 1
storyboard.removeScene( "scene1" )
end
end
end
Why storyboard.state.score assigned twice?
storyboard.state.score = 0
storyboard.state.score = display.newText(storyboard.state.score, 250, 20, "ARIAL", 16)
UPD :
storyboard.state = {score = 0}
score = display.newText(storyboard.state.score, 250, 20, "ARIAL", 16)
storyboard.gotoScene( "scene2", "slideLeft", 500)
storyboard.state.score = storyboard.state.score + 1
storyboard.removeScene( "scene1" )