-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
80 lines (54 loc) · 2.29 KB
/
main.lua
File metadata and controls
80 lines (54 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
-----------------------------------------------------------------------------------------
-- main.lua
-- by Alex Diker
-----------------------------------------------------------------------------------------
display.setStatusBar( display.HiddenStatusBar )
--- Add Physics
local physics = require( "physics" )
physics.start()
local sky = display.newImage( "bkg_clouds.png", display.contentWidth, display.contentHeight)
sky.x = 160; sky.y = 195
local ground = display.newImage ("grass_long.png", display.contentWidth, display.contentHeight)
ground.x = 160; ground.y = 520
local trees = display.newImage( "tree.png" )
trees.x = 250; trees.y = 340;
physics.addBody( ground, "static", { density = 0.3, friction = 0.2, bounce = 0.2 } )
--- Create Player
local player = display.newImage( "player.png" )
player.x = 160
player.y = 400
physics.addBody(player)
--- OnTouch Event for player
local function onTouch(event)
if(event.phase == "began") then
if(event.x < player.x) then
--- jump left
player:setLinearVelocity(-20, -200)
else
--- jump right
player:setLinearVelocity(20, -200)
end
end
end
Runtime:addEventListener("touch", onTouch)
local coconut = display.newImageRect( "coconut.png", 30, 30)
coconut.x = 85; coconut.y = 200; coconut.rotation = 12
physics.addBody (coconut, { friction = 0.1 , bounce=0.1, density=0.1} )
--- Add a score label
local score = 0
local scoreText = display.newText ("Score:", 20, 100, display.contentWidth, display.contentHeight * 0.5, native.systemFontBold, 18)
local scoreLabel = display.newText ( score, 100, 100, display.contentWidth, display.contentHeight * 0.5, native.systemFontBold, 18)
scoreLabel.x = display.viewableContentWidth
scoreLabel.y = display.viewableContentHeight - 400
scoreText.x = display.viewableContentWidth - 60
scoreText.y = display.viewableContentHeight - 400
scoreLabel:setTextColor( 0, 0, 0, 10)
scoreText:setTextColor( 0, 0, 0, 10)
--- add text
---local author = display.newText ( "Alex Diker", 320, 100, display.contentWidth, display.contentHeight * 0.5, native.systemFontBold, 14 )
--- author:setFillColor( 0, 0, 0 )
--- Make Coconut Bounce
local function push()
coconut:applyLinearImpulse(1, 3, coconut.x, coconut.y)
end
coconut:addEventListener("tap", push)