Escolar Documentos
Profissional Documentos
Cultura Documentos
PALABRAS RESERVADAS
and
break
for
or
do
else
function
repeat
if
return
elseif
in
then
end
local
true
false
nil
until
not
while
SIMBOLOS RESERVADOS
+
==
(
;
*
~=
)
:
/
<=
{
,
%
>=
}
.
^
<
[
..
#
>
]
...
TABLAS
t = {}
k = "x"
t[k] = 3.14
print( t[k] )
print( t["x"] )
print( t.x )
t[2] = "foo"
print( t[2] )
--create a table
--new table entry, with key="x" and value=3.14
--> 3.14
--> 3.14
--> 3.14
--new table entry, with key=2 and value="foo"
--> "foo"
Campos de tabla
t = { foo="hello" }
print( t.foo )
t.foo = "bye"
print( t.foo )
t.bar = 10
print( t.bar )
print( t["bar"] )
--local variable
OPERADORES ARITMETICOS
+
(addition)
* (multiplication)
% (modulo)
(subtraction)
/ (division)
^ (exponentiation)
OERADORES RELACIONALES
==
~=
<
>
<=
>=
(equal to)
(not equal to)
(less than)
(greater than)
(less than or equal to)
(greater than or equal to)
OPERADORES LOGICOS
and, or, not
LONGITUD DE OPERADOR
Anteponer (#)
+ sintaxis for
for i=1,#nombretabla
--instrucciones
end
do
FUNCIONES
Funcin local
local function f ()
--body
end
Funcin global
function f ()
--body
end
local f = function()
--body
end
f = function ()
--body
end
SINTAXIS IF
SINTAXIS SWITCH
if (condicion) then
--Instrucciones
else
--Instrucciones
if (condicion) then
--Instrucciones
elseif (condicion)
--Instrucciones
end
--Asi cuantos elseif se requieran
end
REFERENTE A CORONA
IMPRIMIR HELLO WORLD EN CONSOLA
print( "Hello World!" )
FISICA (bsica)
local physics = require( "physics" )
physics.start()
OBJETOS VISUALES
newImage: forma 1
SINTAXIS: display.newImage( "ruta",x,y )
local sky = display.newImage( "bkg_clouds.png" )
sky.x = 160; sky.y = 195 -- (propiedades del objeto para cambiar su posicion)
newImage: forma 2
local sky = display.newImage( "bkg_clouds.png",160,195 )
OPTIMIZACION
--LOCAL (RECOMMENDED)
local CCX = display.contentCenterX
--local variable
for i = 1,100 do
local image = display.newImage(
"myImage" )
image.x = CCX
end
--LOCAL (RECOMMENDED)
local function func2( y ) --'func2'
properly scoped above 'func1'
print( y )
end
local function func1()
func2( "myValue" )
end
func1()
func1()
local a = {}
local table_insert = table.insert
for i = 1,100 do
table_insert( a, i )
end
--TABLE SIZE METHOD (ACCEPTABLE)
local a = {}
for i = 1,100 do
a[#a+1] = i
end
local a = {}
for i = 1,100 do
a[i] = i
end
for i = 1,100 do
print( unpack(a) )
end
for i = 1,100 do
print( a[1],a[2],a[3],a[4] )
end
Evitar "ipairs()"
--ipairs() (DISCOURAGED)
local t1 = {}
local t2 = {}
local t3 = {}
local t4 = {}
local a = { t1, t2, t3, t4 }
--LUA
local
local
local
local
local
CONSTRUCT (RECOMMENDED)
t1 = {}
t2 = {}
t3 = {}
t4 = {}
a = { t1, t2, t3, t4 }
for i = 1,#a do
print( a[i] )
end
Mejoramiento de la matemtica
--DIVISION (DISCOURAGED)
x/2 ; y/8
--MULTIPLICATION BY DECIMAL
(RECOMMENDED)
x * 0.5 ; y * 0.125
--EXPONENTIATION (DISCOURAGED)
x^3
--MULTIPLICATION (RECOMMENDED)
x * x * x
local x = 1
end
end
end
end
ADMINISTRACION DE AUDIO
Para sonidos cortos usar loadSound
local soundTable = {
mySound1 = audio.loadSound(
mySound2 = audio.loadSound(
mySound3 = audio.loadSound(
mySound4 = audio.loadSound(
mySound5 = audio.loadSound(
mySound6 = audio.loadSound(
mySound7 = audio.loadSound(
mySound8 = audio.loadSound(
}
"a.wav"
"b.wav"
"c.wav"
"d.wav"
"e.wav"
"f.wav"
"g.wav"
"h.wav"
),
),
),
),
),
),
),
),
Ejemplo
display.newGroup()
myImage:removeSelf()
myImage = nil
--algunas propiedades (aplica para todos los objetos)
-- hide the image
myImage.isVisible = false
display.newImage( [parent,]
imageSheet, frameIndex )
display.newImageRect( [parentGroup,]
imageSheet, frameIndex, width,
height )
*
local myText = display.newText( "Hello World!", 100, 200,
native.systemFont, 16 )
*
-- primero creamos las opciones de hoja
local sheetOptions =
{
width = 512,
height = 256,
numFrames = 8
}
-- luego cargamos la imagen sprite
local sheet_runningCat =
graphics.newImageSheet( "sritepuma.png", sheetOptions )
* propiedades x1,x2,x3,x4
--ejemplo
local myRectangle = display.newRect( 0, 0, 150, 50 )
--aplicando propiedades y metodos
myRectangle.strokeWidth = 3
myRectangle:setFillColor( 0.5 )
myRectangle:setStrokeColor( 1, 0, 0 )
display.newRoundedRect( x, y, width,
height, cornerRadius )
display.newRoundedRect( parent, x, y,
width, height, cornerRadius )
display.newPolygon( x, y, vertices )
display.newPolygon( parent, x, y,
vertices )
display.newEmbossedText( [parentGroup,
] text, x, y, font, fontSize )
display.newEmbossedText( [parentGroup,
] text, x, y, [width, height,] font,
fontSize )
display.newContainer( [parent, ]
width, height )
-- Create a container
local container = display.newContainer( 128, 128 )
-- Create an image
local bkgd =
display.newImage( "aquariumbackgroundIPhone.jpg" )
-- Insert the image into the container
container:insert( bkgd, true )
-- Center the container in the display area
container:translate( display.contentWidth*0.5,
display.contentHeight*0.5 )
-- Transition (rotate) the container
transition.to( container, { rotation=360,
transition=easing.inOutExpo} )
display.newSnapshot( w, h )
display.newSnapshot( parent, w, h )
-- Add fish to the screen
for i=1,4 do
local fish = display.newImage( "fish.small.red.png" )
-- move to random position in a 200x200 region in the
middle of the screen
fish:translate( math.random( -100, 100 ),
math.random( -100, 100 ) )
METODOS
---- metodo contenToLocal
-- Rectangle that fills screen
local rect = display.newRect( 0, 0,
display.contentWidth, display.contentHeight )
rect.x = display.contentCenterX ; rect.y =
display.contentCenterY
rect.strokeWidth = 4 ; rect:setStrokeColor( 1,
0, 0 )
rect:setFillColor( 0, 0, 0 )
-- Create text objects to display content and
local coordinates
local contentText = display.newText( "", 0, 0,
display.nativeSystemFont, 16 )
local localText = display.newText( "", 0, 0,
display.nativeSystemFont, 16 )
localText.anchorX = 0 ; localText.anchorY = 0
contentText.anchorX = 0 ; contentText.anchorY =
0
--Mtodo localToContent
-- Create a square
local square = display.newRect( 100, 100, 40,
40 )
square:setFillColor( 1 )
-- Create another square on top and rotate it
20 degrees
local redSquare = display.newRect( 100, 100,
40, 40 )
redSquare:setFillColor( 1, 0, 0, 0.6 )
redSquare.rotation = 20
local sqCenterX, sqCenterY =
square:localToContent( 0, 0 )
print( "White square's center position in
screen coordinates: ", sqCenterX, sqCenterY )
-- Get the content position of the white
square's top-left corner
-- Using ( -20,-20 ) specifies the top left
xMin:
yMin:
xMax:
yMax:
100
100
150
150
-- propiedad parent ()
local g = display.newGroup()
-- Create and position image to be masked, and
insert into group
local image = display.newImageRect( g,
"image.png", 768, 1024 )
--rotacin sencilla
- crear cartas
-- transicin
local rect = display.newRect( 70, 150, 100, 150
)
rect:setFillColor( 1, 0, 0 )
--rect.rotation = -45
local reverse = 0 -- bit
-- funcion
local function rockRect()
if ( reverse == 0 ) then
reverse = 1
-- tiempo para la transicion
transition.to(rect, {time = 1000,
x=200})
else
reverse = 0
transition.to(rect, {time = 1000,
x=70})
end
end
--tiempo de inicio y duracion de cada ciclo de
la funcin
timer.performWithDelay( 2000, rockRect, 0 ) -Repite siempre
event.target:toFront()
end
return true
end
for i=1,5 do
--grupo de elementos de uan carta
local cardGroup = display.newGroup()
-- Card outline
local cardRect =
display.newRoundedRect(cardGroup, 100, 100,
125, 175, 12 )
cardRect.strokeWidth = 2
cardRect:setFillColor( 1 )
cardRect:setStrokeColor( 0, 0, 0, 0.3 )
cardGroup:insert(cardRect)
-- Card values
local cardValue =
display.newText( cardGroup, i,
cardRect.contentWidth - 72, 32,
native.systemFontBold, 24 )
cardValue:setFillColor( 1, 0, 0 )
local cardValue2 =
display.newText( cardGroup, i, 148,
cardRect.contentHeight - 8 ,
native.systemFontBold, 24 )
cardValue2:setFillColor( 1, 0, 0 )
cardGroup.x = (i * 25)
cardGroup.y = (i * 25)
if ( reverse == 0 ) then
reverse = 1
transition.to( rect, { rotation=-45,
time=500, transition=easing.inOutCubic } )
else
reverse = 0
transition.to( rect, { rotation=45,
time=500, transition=easing.inOutCubic } )
end
end
timer.performWithDelay( 600, rockRect, 0 ) -Repeat forever
-- propiedad x,y (esto ubica en el punto
donde le indicas)
-local rect = display.newRect( 0, 0, 50, 50 )
rect.x = 100
rect.y = 100
-- propiedad
objeto)
cards:insert( cardGroup )
-- agregando evento a un grupo
cardGroup:addEventListener( "touch",
sendToBack )
end
--Esto aumenta en x o y
local cuadrado = display.newRect( 100, 100,
50, 50 )
cuadrado:setFillColor(1, 0, 0, 1)
cuadrado:translate(100
,100)
Mtodo directo
redSquare2:setFillColor( 1, 0, 0 )
redSquare3:setFillColor( 1, 0, 0 )
--creamos los cuadrados
local whiteSquare1 = display.newRect( squares, 0, 0, 40, 40 )
local whiteSquare2 = display.newRect( squares, 0, 70, 40, 40 )
--os pinatmos de color balnco
whiteSquare1:setFillColor( 1 )
whiteSquare2:setFillColor( 1 )
squares:translate(100,50)
--ponemos solo los cuadrados rojos en la tabla
local redSquares = { redSquare1, redSquare2, redSquare3 }
--manipulamos la tabla usando un for
for i = 1, #redSquares do
--manipulate the square at the current index (i)
redSquares[i].x = redSquares[i].x + 100 -- los movemos 100 a la derecha
redSquares[i]:scale( 0.5, 0.5 ) -- los escalos a 50 %
end
Borrado directo
Borrando un grupo
display.remove ( myObject )
myObject = nil --set reference to nil!
display.remove( myGroup )
myGroup = nil
--OR
myObject:removeSelf()
myObject = nil --set reference to nil!
--OR
myGroup:removeSelf()
myGroup = nil
--OR
myGroup:remove( myObject )
myObject = nil --set reference to nil!