Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
crazyflirt
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Shawn Wang
crazyflirt
Commits
022672e7
Commit
022672e7
authored
May 06, 2018
by
Shawn Wang
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'develop' of cd.i.strikingly.com:shawn.wang/crazyflirt into develop
parents
6e9763d0
86015ab2
Show whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
277 additions
and
41 deletions
+277
-41
doc.rb
be/doc.rb
+1
-0
Controller.ts
fe/src/Controller.ts
+27
-1
Main.ts
fe/src/Main.ts
+8
-5
Map.ts
fe/src/Map.ts
+32
-5
MapGrid.ts
fe/src/MapGrid.ts
+14
-2
Runner.ts
fe/src/Runner.ts
+13
-5
SceneManager.ts
fe/src/SceneManager.ts
+10
-5
SoundManager.ts
fe/src/SoundManager.ts
+45
-0
Store.ts
fe/src/Store.ts
+25
-7
GameScene.ts
fe/src/scenes/GameScene.ts
+3
-11
EventBus.ts
fe/src/utils/EventBus.ts
+99
-0
No files found.
be/doc.rb
View file @
022672e7
...
@@ -21,6 +21,7 @@
...
@@ -21,6 +21,7 @@
role:
'ghost'
,
# runner
role:
'ghost'
,
# runner
x:
1
,
x:
1
,
y:
3
y:
3
t_cid
}
}
}
}
...
...
fe/src/Controller.ts
View file @
022672e7
class
Controller
extends
egret
.
Sprite
{
class
Controller
extends
egret
.
Sprite
{
private
myPosition
=
{}
public
constructor
()
{
public
constructor
()
{
super
();
super
();
this
.
init
()
this
.
init
()
...
@@ -61,6 +62,17 @@ class Controller extends egret.Sprite {
...
@@ -61,6 +62,17 @@ class Controller extends egret.Sprite {
controlContainer
.
addChild
(
this
.
down
)
controlContainer
.
addChild
(
this
.
down
)
controlContainer
.
addChild
(
this
.
left
)
controlContainer
.
addChild
(
this
.
left
)
controlContainer
.
addChild
(
this
.
right
)
controlContainer
.
addChild
(
this
.
right
)
const
me
=
Store
.
getState
().
runners
.
find
(
runner
=>
runner
.
t_cid
===
Store
.
getState
().
cid
)
this
.
myPosition
.
x
=
me
.
x
this
.
myPosition
.
y
=
me
.
y
EventBus
.
addEventListener
(
'moving'
,
(
payload
)
=>
{
if
(
payload
.
target
.
payload
.
t_cid
===
Store
.
getState
().
cid
)
{
this
.
myPosition
.
x
=
payload
.
target
.
payload
.
x
this
.
myPosition
.
y
=
payload
.
target
.
payload
.
y
}
})
}
}
private
controlTouchBegin
(
e
:
egret
.
TouchEvent
){
private
controlTouchBegin
(
e
:
egret
.
TouchEvent
){
...
@@ -75,7 +87,21 @@ class Controller extends egret.Sprite {
...
@@ -75,7 +87,21 @@ class Controller extends egret.Sprite {
delta
.
x
+=
1
delta
.
x
+=
1
}
}
this
.
dispatchEventWith
(
"position_change"
,
false
,
delta
)
platform
.
sendSocketMessage
({
data
:
JSON
.
stringify
({
type
:
'moving'
,
room_id
:
'default_room'
,
cid
:
Store
.
getState
().
cid
,
payload
:
{
x
:
this
.
myPosition
.
x
+
delta
.
x
,
y
:
this
.
myPosition
.
y
+
delta
.
y
,
avatar_url
:
Store
.
getState
().
userInfo
.
avatarUrl
,
nick_name
:
Store
.
getState
().
userInfo
.
nickName
}
}),
})
}
}
public
release
()
{
public
release
()
{
...
...
fe/src/Main.ts
View file @
022672e7
...
@@ -90,7 +90,7 @@ class Main extends egret.DisplayObjectContainer {
...
@@ -90,7 +90,7 @@ class Main extends egret.DisplayObjectContainer {
platform
.
sendSocketMessage
({
platform
.
sendSocketMessage
({
data
:
JSON
.
stringify
({
data
:
JSON
.
stringify
({
type
:
'entry'
,
type
:
'entry'
,
rid
:
'default_room'
,
r
oom_
id
:
'default_room'
,
payload
:
{
payload
:
{
avatar_url
:
Store
.
getState
().
userInfo
.
avatarUrl
,
avatar_url
:
Store
.
getState
().
userInfo
.
avatarUrl
,
nick_name
:
Store
.
getState
().
userInfo
.
nickName
nick_name
:
Store
.
getState
().
userInfo
.
nickName
...
@@ -108,13 +108,15 @@ class Main extends egret.DisplayObjectContainer {
...
@@ -108,13 +108,15 @@ class Main extends egret.DisplayObjectContainer {
})
})
})
})
this
.
createGameScene
();
this
.
createGameScene
();
platform
.
onSocketMessage
(
function
(
res
)
{
platform
.
onSocketMessage
(
function
(
res
)
{
console
.
log
(
'收到服务器内容:'
+
res
.
data
)
console
.
log
(
'收到服务器内容:'
+
res
.
data
)
const
state
=
Store
.
onMessage
(
res
.
data
)
let
data
=
res
.
data
let
data
=
res
.
data
if
(
typeof
res
.
data
===
'string'
)
{
if
(
typeof
res
.
data
===
'string'
)
{
data
=
JSON
.
parse
(
res
.
data
)
data
=
JSON
.
parse
(
res
.
data
)
}
// data = { "type": "game_ready", "rid": "default_room", "payload": [{ "nick_name": "syfee", "avatar_url": "https://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTIibqBbRLAe0AI9NvnzPdLEBx9mT0ZKXicbibYWLAJiaPs0EZtmDiazJ7kytEj4tzWiberUptkR1yg0Q28g/132", "cid": "1525524303u0my3", "role": "ghost", "t_cid": "1525524303u0my3", x: 1, y: 1 }, { "nick_name": "syfee1", "avatar_url": "https://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTIibqBbRLAe0AI9NvnzPdLEBx9mT0ZKXicbibYWLAJiaPs0EZtmDiazJ7kytEj4tzWiberUptkR1yg0Q28g/132", "cid": "1525524303u0my4", "role": "runner", "t_cid": "1525524303u0my4", x: 1, y: 1 }], "cid": "1525524303u0my4" }
const
state
=
Store
.
onMessage
(
data
)
EventBus
.
dispatch
(
data
.
type
,
data
)
switch
(
data
.
type
)
{
switch
(
data
.
type
)
{
case
'entry'
:
case
'entry'
:
Store
.
onMessage
({
Store
.
onMessage
({
...
@@ -126,7 +128,6 @@ class Main extends egret.DisplayObjectContainer {
...
@@ -126,7 +128,6 @@ class Main extends egret.DisplayObjectContainer {
case
'game_ready'
:
case
'game_ready'
:
SceneManager
.
getInstance
().
roomScene
.
update
(
data
)
SceneManager
.
getInstance
().
roomScene
.
update
(
data
)
}
}
}
})
})
platform
.
onSocketError
((
e
)
=>
{
platform
.
onSocketError
((
e
)
=>
{
console
.
error
(
"onSocketError"
)
console
.
error
(
"onSocketError"
)
...
@@ -161,6 +162,8 @@ class Main extends egret.DisplayObjectContainer {
...
@@ -161,6 +162,8 @@ class Main extends egret.DisplayObjectContainer {
*/
*/
private
createGameScene
()
{
private
createGameScene
()
{
this
.
addChild
(
SceneManager
.
getInstance
());
this
.
addChild
(
SceneManager
.
getInstance
());
// SoundManager.getInstance().play({soundName: 'bgm_mp3', loops: -1})
// SoundManager.getInstance().playCatched(1)
}
}
/**
/**
...
...
fe/src/Map.ts
View file @
022672e7
...
@@ -5,7 +5,8 @@ const gridsInY = 10
...
@@ -5,7 +5,8 @@ const gridsInY = 10
class
Map
extends
egret
.
Sprite
{
class
Map
extends
egret
.
Sprite
{
private
grids
:
any
=
[]
private
grids
:
any
=
[]
private
touchArea
:
egret
.
Bitmap
private
touchArea
:
egret
.
Bitmap
private
isShowingTouchArea
public
isShowingTouchArea
private
runnerMap
:
any
=
{}
public
constructor
()
{
public
constructor
()
{
super
();
super
();
this
.
init
()
this
.
init
()
...
@@ -28,6 +29,24 @@ class Map extends egret.Sprite {
...
@@ -28,6 +29,24 @@ class Map extends egret.Sprite {
this
.
touchArea
.
texture
=
RES
.
getRes
(
'mask_jpg'
)
this
.
touchArea
.
texture
=
RES
.
getRes
(
'mask_jpg'
)
this
.
touchArea
.
alpha
=
0
this
.
touchArea
.
alpha
=
0
this
.
addChild
(
this
.
touchArea
)
this
.
addChild
(
this
.
touchArea
)
Store
.
getState
().
runners
.
forEach
(
runnerData
=>
{
const
runner
=
new
Runner
(
runnerData
)
this
.
addChild
(
runner
)
this
.
runnerMap
[
runnerData
.
t_cid
]
=
runner
})
EventBus
.
addEventListener
(
'moving'
,
(
payload
)
=>
{
this
.
runnerMap
[
payload
.
target
.
payload
.
t_cid
].
x
=
payload
.
target
.
payload
.
x
*
gridWidth
this
.
runnerMap
[
payload
.
target
.
payload
.
t_cid
].
y
=
payload
.
target
.
payload
.
y
*
gridHeight
})
EventBus
.
addEventListener
(
'clicking'
,
(
payload
)
=>
{
const
x
=
payload
.
target
.
payload
.
x
*
gridWidth
const
y
=
payload
.
target
.
payload
.
y
*
gridHeight
if
(
Store
.
isCatcher
())
{
this
.
showSurroundedGrids
(
x
,
y
)
}
else
{
this
.
showClickedGrid
(
x
,
y
)
}
})
}
}
public
getSurroundedGrids
(
x
,
y
)
{
public
getSurroundedGrids
(
x
,
y
)
{
...
@@ -44,13 +63,21 @@ class Map extends egret.Sprite {
...
@@ -44,13 +63,21 @@ class Map extends egret.Sprite {
return
grids
return
grids
}
}
public
showSurroundedGrids
(
x
,
y
,
centerGrid
)
{
public
showClickedGrid
(
x
,
y
)
{
if
(
this
.
isShowingTouchArea
)
{
const
surroundedGrids
=
this
.
getSurroundedGrids
(
x
,
y
)
return
const
clickedGrid
=
surroundedGrids
.
find
(
grid
=>
grid
.
x
===
x
&&
grid
.
y
===
y
)
clickedGrid
.
bg
.
texture
=
RES
.
getRes
(
'grid_select_jpg'
)
setTimeout
(()
=>
{
clickedGrid
.
bg
.
texture
=
RES
.
getRes
(
'grid_jpg'
)
},
1000
);
}
}
public
showSurroundedGrids
(
x
,
y
)
{
this
.
isShowingTouchArea
=
true
this
.
isShowingTouchArea
=
true
centerGrid
.
bg
.
texture
=
RES
.
getRes
(
'grid_select_jpg'
)
const
surroundedGrids
=
this
.
getSurroundedGrids
(
x
,
y
)
const
surroundedGrids
=
this
.
getSurroundedGrids
(
x
,
y
)
const
centerGrid
=
surroundedGrids
.
find
(
grid
=>
grid
.
x
===
x
&&
grid
.
y
===
y
)
centerGrid
.
bg
.
texture
=
RES
.
getRes
(
'grid_select_jpg'
)
surroundedGrids
.
forEach
(
grid
=>
{
surroundedGrids
.
forEach
(
grid
=>
{
grid
.
alpha
=
1
grid
.
alpha
=
1
})
})
...
...
fe/src/MapGrid.ts
View file @
022672e7
...
@@ -17,8 +17,20 @@ class MapGrid extends egret.Sprite {
...
@@ -17,8 +17,20 @@ class MapGrid extends egret.Sprite {
}
}
private
onTouchTap
()
{
private
onTouchTap
()
{
if
(
Store
.
isCatcher
())
{
if
(
Store
.
isCatcher
()
&&
!
this
.
parent
.
isShowingTouchArea
)
{
this
.
parent
.
showSurroundedGrids
(
this
.
x
,
this
.
y
,
this
)
platform
.
sendSocketMessage
({
data
:
JSON
.
stringify
({
type
:
'clicking'
,
cid
:
Store
.
getState
().
cid
,
room_id
:
'default_room'
,
payload
:
{
x
:
this
.
x
/
72
,
y
:
this
.
y
/
72
,
avatar_url
:
Store
.
getMyInfo
().
avatarUrl
,
nick_name
:
Store
.
getMyInfo
().
nickName
}
}),
})
}
}
}
}
...
...
fe/src/
Guy
.ts
→
fe/src/
Runner
.ts
View file @
022672e7
class
Guy
extends
egret
.
Sprite
{
class
Runner
extends
egret
.
Sprite
{
public
constructor
()
{
public
constructor
(
data
)
{
super
()
super
()
this
.
init
()
this
.
init
(
data
)
}
}
private
avatar
=
new
egret
.
Bitmap
()
private
avatar
=
new
egret
.
Bitmap
()
private
imageLoader
=
new
egret
.
ImageLoader
()
private
imageLoader
=
new
egret
.
ImageLoader
()
private
init
()
{
private
init
(
data
)
{
this
.
avatar
.
width
=
72
this
.
avatar
.
width
=
72
this
.
avatar
.
height
=
72
this
.
avatar
.
height
=
72
this
.
x
=
data
.
x
*
this
.
avatar
.
width
this
.
y
=
data
.
y
*
this
.
avatar
.
height
this
.
imageLoader
.
addEventListener
(
egret
.
Event
.
COMPLETE
,
this
.
loadCompleteHandler
,
this
)
this
.
imageLoader
.
addEventListener
(
egret
.
Event
.
COMPLETE
,
this
.
loadCompleteHandler
,
this
)
this
.
imageLoader
.
load
(
"http://www.insajderi.com/wp-content/uploads/2018/01/asdja.jpg"
)
this
.
imageLoader
.
load
(
data
.
avatar_url
)
this
.
addChild
(
this
.
avatar
)
this
.
addChild
(
this
.
avatar
)
}
}
...
@@ -39,4 +41,9 @@ class Guy extends egret.Sprite {
...
@@ -39,4 +41,9 @@ class Guy extends egret.Sprite {
this
.
x
=
72
*
13
+
10
this
.
x
=
72
*
13
+
10
}
}
}
}
public
moveTo
(
x
,
y
)
{
this
.
x
=
x
this
.
y
=
y
}
}
}
\ No newline at end of file
fe/src/SceneManager.ts
View file @
022672e7
...
@@ -5,6 +5,10 @@ class SceneManager extends egret.DisplayObjectContainer {
...
@@ -5,6 +5,10 @@ class SceneManager extends egret.DisplayObjectContainer {
public
roomScene
:
RoomScene
;
public
roomScene
:
RoomScene
;
// 游戏场景
// 游戏场景
public
gameScene
:
GameScene
;
public
gameScene
:
GameScene
;
private
sceneMap
:
any
=
{
roomScene
:
RoomScene
,
gameScene
:
GameScene
}
public
constructor
()
{
public
constructor
()
{
super
();
super
();
...
@@ -13,9 +17,9 @@ class SceneManager extends egret.DisplayObjectContainer {
...
@@ -13,9 +17,9 @@ class SceneManager extends egret.DisplayObjectContainer {
private
init
()
{
private
init
()
{
// 实例化两个场景
// 实例化两个场景
this
.
roomScene
=
new
RoomScene
();
this
.
roomScene
=
new
RoomScene
();
this
.
gameScene
=
new
GameScene
();
//
this.gameScene = new GameScene();
// 默认添加开始场景
// 默认添加开始场景
this
.
addChild
(
this
.
game
Scene
);
this
.
addChild
(
this
.
room
Scene
);
}
}
// 实例化单例获取方法
// 实例化单例获取方法
public
static
getInstance
():
SceneManager
{
public
static
getInstance
():
SceneManager
{
...
@@ -26,10 +30,11 @@ class SceneManager extends egret.DisplayObjectContainer {
...
@@ -26,10 +30,11 @@ class SceneManager extends egret.DisplayObjectContainer {
}
}
// 切换场景
// 切换场景
public
changeScene
(
type
)
{
public
changeScene
(
type
)
{
// 释放资源
if
(
!
this
[
type
])
{
if
(
this
[
type
]
&&
typeof
this
[
type
].
release
===
'function'
)
{
const
Scene
=
this
.
sceneMap
[
type
]
this
[
type
]
.
release
()
this
[
type
]
=
new
Scene
()
}
}
// 移除所有显示列表中的对象
// 移除所有显示列表中的对象
this
.
removeChildren
();
this
.
removeChildren
();
// 添加下一个场景
// 添加下一个场景
...
...
fe/src/SoundManager.ts
0 → 100644
View file @
022672e7
class
SoundManager
{
private
static
instance
:
SoundManager
;
private
sounds
:
any
public
constructor
()
{
this
.
init
()
}
private
init
()
{
this
.
sounds
=
{}
const
soundNames
=
[
'catched_0_mp3'
,
'catched_1_mp3'
,
'catched_2_mp3'
,
'flirt_mp3'
,
'bgm_mp3'
,
]
soundNames
.
forEach
(
soundName
=>
{
this
.
sounds
[
soundName
]
=
RES
.
getRes
(
soundName
)
})
}
// 实例化单例获取方法
public
static
getInstance
():
SoundManager
{
if
(
!
SoundManager
.
instance
)
{
SoundManager
.
instance
=
new
SoundManager
();
}
return
SoundManager
.
instance
;
}
public
play
({
soundName
,
startTime
=
0
,
loops
=
1
})
{
this
.
sounds
[
soundName
].
play
(
startTime
,
loops
)
}
// 随机播放被抓音频
public
playCatched
(
num
?)
{
const
soundName
=
[
'catched_0_mp3'
,
'catched_2_mp3'
,
][
num
||
this
.
rnd
(
0
,
1
)]
this
.
play
({
soundName
})
}
rnd
(
start
,
end
)
{
return
Math
.
floor
(
Math
.
random
()
*
(
end
-
start
)
+
start
)
}
}
\ No newline at end of file
fe/src/Store.ts
View file @
022672e7
...
@@ -2,25 +2,30 @@ let _state: any = {
...
@@ -2,25 +2,30 @@ let _state: any = {
role
:
null
,
role
:
null
,
cid
:
null
,
cid
:
null
,
roomId
:
null
,
roomId
:
null
,
userInfo
:
{},
runners
:
[],
catcher
:
{}
}
}
const
listenners
=
[]
class
Store
{
class
Store
{
static
getState
()
{
static
getState
()
{
return
_state
return
_state
}
}
static
onMessage
(
message
)
{
static
onMessage
(
message
)
{
const
{
payload
}
=
message
const
{
payload
,
room_id
}
=
message
switch
(
message
.
type
)
{
switch
(
message
.
type
)
{
case
'game_ready'
:
case
'game_ready'
:
{
const
{
cid
,
room_id
,
payload
:
{
role
}
}
=
payload
const
runners
=
payload
.
filter
(
runner
=>
runner
.
role
!==
'ghost'
)
_state
=
{
_state
=
{
...
_state
,
...
_state
,
cid
,
role
,
roomId
:
room_id
,
roomId
:
room_id
,
runners
,
}
}
break
;
break
;
}
case
'setUserInfo'
:
case
'setUserInfo'
:
_state
=
{
_state
=
{
...
_state
,
...
_state
,
...
@@ -32,6 +37,7 @@ class Store {
...
@@ -32,6 +37,7 @@ class Store {
...
_state
,
...
_state
,
cid
:
payload
.
cid
cid
:
payload
.
cid
}
}
break
;
break
;
default
:
default
:
break
;
break
;
...
@@ -39,8 +45,19 @@ class Store {
...
@@ -39,8 +45,19 @@ class Store {
return
_state
return
_state
}
}
static
subscribe
(
fn
)
{
}
static
getMyInfo
()
{
return
_state
.
userInfo
}
static
getMyPosition
static
isCatcher
()
{
static
isCatcher
()
{
return
true
console
.
log
(
'isCatcher_____'
,
_state
.
runners
,
_state
.
cid
)
// return _state.role === 'ghost'
return
!
_state
.
runners
.
some
(
runner
=>
runner
.
t_cid
===
_state
.
cid
)
}
}
}
}
window
.
Store
=
Store
\ No newline at end of file
fe/src/scenes/GameScene.ts
View file @
022672e7
...
@@ -7,8 +7,6 @@ class GameScene extends egret.Sprite {
...
@@ -7,8 +7,6 @@ class GameScene extends egret.Sprite {
this
.
init
()
this
.
init
()
}
}
private
me
=
new
Guy
()
// 初始化(给开始按钮绑定点击事件)
// 初始化(给开始按钮绑定点击事件)
private
init
()
{
private
init
()
{
// put display object to this scene
// put display object to this scene
...
@@ -18,21 +16,15 @@ class GameScene extends egret.Sprite {
...
@@ -18,21 +16,15 @@ class GameScene extends egret.Sprite {
bg
.
graphics
.
drawRect
(
0
,
0
,
1334
,
750
)
bg
.
graphics
.
drawRect
(
0
,
0
,
1334
,
750
)
bg
.
graphics
.
endFill
()
bg
.
graphics
.
endFill
()
this
.
addChild
(
bg
)
this
.
addChild
(
bg
)
if
(
!
Store
.
isCatcher
())
{
this
.
controller
=
new
Controller
()
this
.
controller
=
new
Controller
()
this
.
addChild
(
this
.
controller
)
this
.
addChild
(
this
.
controller
)
this
.
controller
.
addEventListener
(
"position_change"
,
this
.
onPositionChange
,
this
)
}
this
.
map
=
new
Map
()
this
.
map
=
new
Map
()
this
.
map
.
y
=
15
this
.
map
.
y
=
15
this
.
map
.
x
=
10
this
.
map
.
x
=
10
this
.
me
.
y
=
15
this
.
me
.
x
=
10
this
.
addChild
(
this
.
map
)
this
.
addChild
(
this
.
map
)
this
.
map
.
addChild
(
this
.
me
)
}
private
onPositionChange
(
delta
){
this
.
me
.
applyDelta
(
delta
.
data
)
}
}
public
release
()
{
public
release
()
{
...
...
fe/src/utils/EventBus.ts
0 → 100644
View file @
022672e7
class
EventBusClass
{
private
listeners
:
any
=
{}
public
addEventListener
(
type
,
callback
,
scope
?)
{
var
args
=
[];
var
numOfArgs
=
arguments
.
length
;
for
(
var
i
=
0
;
i
<
numOfArgs
;
i
++
){
args
.
push
(
arguments
[
i
]);
}
args
=
args
.
length
>
3
?
args
.
splice
(
3
,
args
.
length
-
1
)
:
[];
if
(
typeof
this
.
listeners
[
type
]
!=
"undefined"
)
{
this
.
listeners
[
type
].
push
({
scope
:
scope
,
callback
:
callback
,
args
:
args
});
}
else
{
this
.
listeners
[
type
]
=
[{
scope
:
scope
,
callback
:
callback
,
args
:
args
}];
}
}
public
removeEventListener
(
type
,
callback
,
scope
)
{
if
(
typeof
this
.
listeners
[
type
]
!=
"undefined"
)
{
var
numOfCallbacks
=
this
.
listeners
[
type
].
length
;
var
newArray
=
[];
for
(
var
i
=
0
;
i
<
numOfCallbacks
;
i
++
)
{
var
listener
=
this
.
listeners
[
type
][
i
];
if
(
listener
.
scope
==
scope
&&
listener
.
callback
==
callback
)
{
}
else
{
newArray
.
push
(
listener
);
}
}
this
.
listeners
[
type
]
=
newArray
;
}
}
public
hasEventListener
(
type
,
callback
,
scope
)
{
if
(
typeof
this
.
listeners
[
type
]
!=
"undefined"
)
{
var
numOfCallbacks
=
this
.
listeners
[
type
].
length
;
if
(
callback
===
undefined
&&
scope
===
undefined
){
return
numOfCallbacks
>
0
;
}
for
(
var
i
=
0
;
i
<
numOfCallbacks
;
i
++
)
{
var
listener
=
this
.
listeners
[
type
][
i
];
if
((
scope
?
listener
.
scope
==
scope
:
true
)
&&
listener
.
callback
==
callback
)
{
return
true
;
}
}
}
return
false
;
}
public
dispatch
(
type
,
target
)
{
var
event
=
{
type
:
type
,
target
:
target
};
var
args
=
[];
var
numOfArgs
=
arguments
.
length
;
for
(
var
i
=
0
;
i
<
numOfArgs
;
i
++
){
args
.
push
(
arguments
[
i
]);
};
args
=
args
.
length
>
2
?
args
.
splice
(
2
,
args
.
length
-
1
)
:
[];
args
=
[
event
].
concat
(
args
);
if
(
typeof
this
.
listeners
[
type
]
!=
"undefined"
)
{
var
listeners
=
this
.
listeners
[
type
].
slice
();
var
numOfCallbacks
=
listeners
.
length
;
for
(
var
i
=
0
;
i
<
numOfCallbacks
;
i
++
)
{
var
listener
=
listeners
[
i
];
if
(
listener
&&
listener
.
callback
)
{
var
concatArgs
=
args
.
concat
(
listener
.
args
);
listener
.
callback
.
apply
(
listener
.
scope
,
concatArgs
);
}
}
}
}
public
getEvents
()
{
var
str
=
""
;
for
(
var
type
in
this
.
listeners
)
{
var
numOfCallbacks
=
this
.
listeners
[
type
].
length
;
for
(
var
i
=
0
;
i
<
numOfCallbacks
;
i
++
)
{
var
listener
=
this
.
listeners
[
type
][
i
];
str
+=
listener
.
scope
&&
listener
.
scope
.
className
?
listener
.
scope
.
className
:
"anonymous"
;
str
+=
" listen for '"
+
type
+
"'
\
n"
;
}
}
return
str
;
}
}
if
(
!
window
.
EventBus
)
{
window
.
EventBus
=
new
EventBusClass
()
}
declare
let
EventBus
:
EventBusClass
;
declare
interface
Window
{
EventBus
:
EventBusClass
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment