【发布时间】:2014-08-21 09:22:24
【问题描述】:
我有一个 RPG 主 HTML 元素,定义如下。我在 index.html 文件的主体中实例化它。
<link rel="import" href="packages/polymer/polymer.html">
<link rel="import" href="gridlayout.html">
<link rel="import" href="gridtile.html">
<polymer-element name="rpg-main" attributes="">
<template>
<style>
grid-tile {
background: #FF00FF;
width: 50px;
height: 50px
}
:host {
position: absolute;
display: block;
width: 500px;
height: 500px;
}
</style>
<grid-layout rows = "2" cols = "2" spacing = "50px">
<grid-tile>
</grid-tile>
<grid-tile>
</grid-tile>
<grid-tile>
</grid-tile>
<grid-tile>
</grid-tile>
</grid-layout>
</template>
<script type="application/dart" src="rpgmain.dart"></script>
</polymer-element>
正如预期的那样,我还定义了网格布局和网格平铺元素。在网格布局的构造函数中,我试图引用它的子元素。但是该元素在上面的rpg-main类中实例化时认为它有0个孩子。
import 'package:polymer/polymer.dart';
import 'dart:html';
@CustomTag('grid-layout')
class GridLayout extends PolymerElement {
@published int rows;
@published int cols;
@published int spacing;
String _px = "px";
String _perc = "%";
GridLayout.created() : super.created() {
print("NUM CHILDREN " + this.children.length.toString());
/** for (int i = 0; i <= this.rows + this.cols - 2; i++) {
Element child = this.children[i];
this.children[i].style.margin = this._parseNum(spacing);
child.style.float = "right";
if (i % this.rows == this.rows) {
child.style.clear = "right";
}
}**/
}
num _parseString(String s) {
print(s.toString());
return num.parse(s.split(_px)[0]);
}
String _parseNum(num n) {
return n.toString() + _px;
}
}
***以上代码打印“NUM CHILDREN: 0”
它只在我的 rpg-main 元素中被实例化,所以我很惊讶它不会将网格图块识别为子元素。可能是因为 grid-layout 元素是在 rpg-main 自定义元素的模板标签中实例化的吗? (所以也许 grid-layout 不认为它的孩子在'light dom'中?)这将是一种耻辱,但如果是这样,解决方法是什么?
【问题讨论】:
-
这有点老了,在 JS 而不是 Dart 中,但它可能会对你有所帮助:github.com/cletusw/rpg
-
嘿,这很酷。一般来说,您是如何为这类事情找到 Polymer 的?认为它可能适合游戏,因为它比以前的 html5 更像是面向对象的编程。
-
到目前为止一切都很好。我认为如果我尝试扩展到真实游戏的规模和复杂性,就会出现性能问题。
标签: html dart polymer dart-polymer shadow-dom