Featured image of post Lua example

Lua example

img

Alexandre Nakonechnyj (Grafik-Design) und Lua-Team (PostScript-Code) / Public domain

display.setStatusBar( display.HiddenStatusBar )

local physics = require( "physics" )
physics.start()

physics.setGravity( 0, 10 )
math.randomseed(os.time())


local shape_1 = { 1,-33, 27,-19, 31,14, 10,32, -17,29, -31,9, -28,-15, -13,-27 }
shape_1.density = 1; shape_1.friction = 0.3; shape_1.bounce = 0.2; 

local box = display.newImageRect( "images/ground.png", 400, 70 )
box.x = 167
box.y = 460
physics.addBody( box, "static", { density=1, friction=0.3, bounce=0.2 } )

scores = 0
texts = display.newText(scores, 20, 10, native.systemFont, 20)

local function f_ball(event)
    local ball = display.newImageRect( "images/ball.png", 70, 70 )
    if(math.random() >= 0.5)
    then
      x = 160 + math.random( 100 )
    else
      x = 160 - math.random( 100 )
    end
    
    ball.x = x
    ball.y = 250 - math.random( 100 )      
    physics.addBody( ball, 
        {density=shape_1.density, friction=shape_1.friction, bounce=shape_1.bounce, shape=shape_1}
    )
      
    local function onTouch()
        ball:removeSelf()
        ball = nil
        scores = scores + 5
        texts.text = scores
    end

    ball:addEventListener("touch", onTouch)    
end

local function f_box(event)
    local box = display.newImageRect( "images/crate.png", 70, 70 )
    box.x = 165
    box.y = 260
    physics.addBody( box, { density=1, friction=0.3, bounce=0.2 } )     
    
    local function onTouch()
        box:removeSelf()
        box = nil
        scores = scores + 10
        texts.text = scores
    end

    box:addEventListener("touch", onTouch)
end

--[[ for i=1,50 do
    produceBall()
    produceBall2()
end ]]

-- timer.performWithDelay(delay, function, times)
timer.performWithDelay( 1000, f_ball, -1 ) 
timer.performWithDelay( 1000, f_box, -1 )

 

Licensed under CC BY-NC-SA 4.0