-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoin.lua
More file actions
90 lines (61 loc) · 1.95 KB
/
coin.lua
File metadata and controls
90 lines (61 loc) · 1.95 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
81
82
83
84
85
86
87
88
89
module (..., package.seeall)
require "sprite"
local squirrel = require( "squirrel" )
local catapult = require( "catapult" )
local coinSound = audio.loadSound("coin.mp3")
-- coins layer
local layer
-- variables
local borderXright = 300
local randRightXMin = 1100
local randRightXMax = 1800
-- animation variables
local coinSheetData = require ("coinSheet")
local data1 = coinSheetData.getSpriteSheetData()
local spriteSheet = sprite.newSpriteSheetFromData( "coin.png", data1 )
local animationSpriteSet = sprite.newSpriteSet(spriteSheet, 1, 8)
sprite.add( animationSpriteSet, "flipping", 1, 8, 275, 0 )
function setLayer( _coinsLayer )
layer = _coinsLayer
end
function newCoin(_x, _y)
-- local variables
local counted = false
-- graphics and physics
local _coin = display.newGroup()
_coin:scale(2, 2)
_coin.x = _x
_coin.y = _y
physics.addBody( _coin, "kinematic", { isSensor=true, density = 1.0, friction = 0.3, bounce = 0.2, radius = 30 } )
local flippingSpriteSet = sprite.newSprite( animationSpriteSet )
flippingSpriteSet:prepare("flipping")
_coin:insert(flippingSpriteSet)
layer:insert(_coin)
flippingSpriteSet.x = 0
flippingSpriteSet.y = 0
flippingSpriteSet:play()
-- respawn when out of display
local function checkCoinOutOfWindow( event )
if squirrel.isLaunched() then
if _coin.x < squirrel.getSquirrelInstance().x - borderXright then
_coin.x = squirrel.getSquirrelInstance().x + math.random(randRightXMin, randRightXMax)
_coin.isVisible = true
-- restart variables
counted = false
end
else
counted = false
_coin.x = _x
_coin.y = _y
end
end
local function onLocalCollision( event )
if ( event.phase == "began" ) then
_coin.isVisible = false
_G.bonus = _G.bonus + _G.bonusEach;
audio.play( coinSound )
end
end
Runtime:addEventListener("enterFrame", checkCoinOutOfWindow)
_coin:addEventListener( "collision", onLocalCollision )
end