-
Notifications
You must be signed in to change notification settings - Fork 7
/
XCButtonNode.coffee
52 lines (45 loc) · 1.69 KB
/
XCButtonNode.coffee
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
#########################################
# XCButtonNode is an image based button
# it automatically listens for taps and
# responds to those which start and end
# within its boundaries.
#########################################
class XCButtonNode extends XCSpriteNode
#create a new button with an image
constructor: (imageName) ->
super(imageName)
#automatically listen for tap events
xc.addEventListener('tapDown', this)
xc.addEventListener('tapUp', this)
xc.addEventListener('tapMoved', this)
# when a tap is detected
tapDown: (event) ->
# is the tap within the button's boundary?
if xc.rectContainsPoint(this.rect(), event.point)
#if so, a tap is starting
@tapStarted = true
#call the function onTapStart. This is useful
# for visual changes to the button. For example,
# growing or changing color.
this.onTapStart(event.point)
#when a tap move is detected
tapMoved: (event) ->
#is the tap still within the button's boundaries?
if not xc.rectContainsPoint(this.rect(), event.point)
#if not, call the onTapLeave method. Again,
# this is useful for visual changes to notify
# the user that the tap left the button
this.onTapLeave(event.point)
#when a tap is lifted
tapUp: (event) ->
#did the tap start in our boundaries and is it still there?
if @tapStarted and xc.rectContainsPoint(this.rect(), event.point)
#if so, call the onHit function
this.onHit(event.point)
#since we just handled the tap, there should be no more.
@tapStarted = false
#by default, the tapstart and tapleave methods don't do anything
onTapStart: (point) ->
onTapLeave: (point) ->
#by default, the onHit method doesn't do anything.
onHit: (point) ->