【发布时间】:2014-01-26 06:10:20
【问题描述】:
我正在尝试在画布中实现我的游戏到角度指令,但我不知道如何实现我的类“原型”
我的代码在 coffeeScript 中,但 javascript 不是问题 :)
我的指令:
angular.module("tetris", []).directive("tetris", ($timeout) ->
restrict: "E"
transclude: true
scope: {}
link: ($scope, $element, $log) ->
game = new Game($element.children()[0],$element.children()[1],$element.children()[2])
game.initGame()
document.onkeydown=(e)->
game.keyDown(e);
document.onkeyup=(e)->
game.keyUp(e);
return
template:"<div class=\"row relative\" id=\"canvas\">"+
"<canvas class=\"absolute\" width=\"120\" height=\"250\" style=\"z-index:1\">"+
"Your browser is not supporting canvas"+
"</canvas>"+
"<canvas class=\"absolute\" width=\"120\" height=\"250\" style=\"z-index:2\"></canvas>"+
"<canvas class=\"absolute\" width=\"120\" height=\"250\" style=\"z-index:3\"></canvas>"+
"</div>"
replace: true
)
这是我的 eventHandler 类,我想将其实现为指令以将变量和事件提升到范围。这个类是从 Game 类中调用的。
基本上我想将变量从游戏原型提升到指令的父范围
class EventHandler
constructor:->
@scoreElm = document.getElementById('score')
@levelElm = document.getElementById('level')
@bombElm = document.getElementById('bomb')
@nextElm = document.getElementById('next')
updateScore:(score)->
@scoreElm.innerHTML=score
setBombCountDown:(time)->
@bombElm.style.display.block
@bombElm.innerHTML=time
hideBombCountDown:->
@bombElm.style.display.none
//编辑:
我想出了如何去做,但我觉得这不是正确的方法。你有什么建议可以做得更好吗?
angular.module("tetris", []).directive("tetris", ($timeout) ->
restrict: "E"
transclude: true
scope: false
link: ($scope, $element, $log) ->
class EventHandler
updateScore:(score)->
$scope.score = score
$scope.$apply()
setBombCountDown:(time)->
$scope.bombTime=time
$scope.$apply()
hideBombCountDown:->
$scope.bombTime=null
$scope.$apply()
game = new Game($element.children()[0],$element.children()[1],$element.children()[2],EventHandler)
game.initGame()
document.onkeydown=(e)->
game.keyDown(e);
document.onkeyup=(e)->
game.keyUp(e);
return
template:"<div class=\"row relative\" id=\"canvas\">"+
"<canvas class=\"absolute\" width=\"120\" height=\"250\" style=\"z-index:1\">"+
"Your browser is not supporting canvas"+
"</canvas>"+
"<canvas class=\"absolute\" width=\"120\" height=\"250\" style=\"z-index:2\"></canvas>"+
"<canvas class=\"absolute\" width=\"120\" height=\"250\" style=\"z-index:3\"></canvas>"+
"</div>"
replace: true
)
【问题讨论】:
标签: angularjs coffeescript angularjs-directive prototype