您真正需要做的就是给这两个实例的引用相同的阵列,或者给Perso一个参考Jeau。静态变量是一个really bad idea,即使这种情况没有固有的东西可以阻止它们为你工作。
这是一个使用Dependency Injection解决方案将是什么样子:
package model {
public class Jeau extends EventDispatcher {
protected var _tMap1:Array = new Array();
protected var _tMap2:Array = new Array();
protected var _tMap3:Array = new Array();
//consider using more descriptive variable names
//or an array of arrays (one map in each index)
public function get tMap1():Array {
return _tMap1;
}
public function set tMap1(value:Array):void {
if (value != _tMap1) {
_tMap1 = value;
dispatchEvent(new Event('tMap1Changed'));
}
}
public function get tMap2():Array {
return _tMap2;
}
public function set tMap2(value:Array):void {
if (value != _tMap2) {
_tMap2 = value;
dispatchEvent(new Event('tMap2Changed'));
}
}
public function get tMap3():Array {
return _tMap3;
}
public function set tMap3(value:Array):void {
if (value != _tMap3) {
_tMap3 = value;
dispatchEvent(new Event('tMap3Changed'));
}
}
protected function somethingThatChangesMap1(index:int, value:String):void {
_tMap1[index] = value;
dispatchEvent(new Event('tMap1Changed'));
}
}
}
我认为这是一个视图类 - 你有没有考虑到许多细节。您可以监听模型类中的事件,然后根据这些数组中的任何内容更新视图。通过获取整个实例,您有能力倾听这些事件。否则,您必须使用其他一些机制来传达更改(例如Ro enter link description here botLegs中使用的事件总线)。
package view {
class Perso extends MovieClip {
protected var jeau:Jeau;
public function get jeau():Jeau {
return _jeau;
}
public function set jeau(value:Jeau):void {
if (value != _jeau) {
_jeau = value;
_jeau.addEventListener('map1Changed', doMap1Stuff);
_jeau.addEventListener('map2Changed', doMap2Stuff);
_jeau.addEventListener('map3Changed', doMap3Stuff);
doMap1Stuff();
doMap2Stuff();
doMap3Stuff();
}
}
protected function doMap1Stuff(e:Event=null) {
//do actions depending on the state of map1 here
}
protected function doMap2Stuff(e:Event=null) {
//do actions depending on the state of map2 here
}
protected function doMap3Stuff(e:Event=null) {
//do actions depending on the state of map3 here
}
}
}
这只是如何使用第三个类来组合前两个的例子。我不一定这样做:
package control {
public class MainGame {
protected var jeau:Jeau;
protected function perso:Perso;
public function MainGame() {
jeau = new Jeau();
//jeau setup
perso = new Perso();
perso.jeau = jeau;
}
}
}
难以理解的问题的语言。如果你可以专注于清理你的问题,这将会有很大的帮助。当你实例化Perso.as传递给你的Jeu.as类的引用时。 – BadFeelingAboutThis
(对不起,我英语不好...)简而言之,我想将t_map值从我的jeu.as带到perso.as中的其他变量 – Janick
你确定静态变量不能工作吗?即使't_map1'发生变化,访问'Jeu.t_map1 [0]'(例如)将会给你当前的值。 –