在 ActionScript 3.0 中访问 "global" var - AIR 应用程序
Access to "global" var in ActionScript 3.0 - AIR application
在 AIR Mobile 应用程序中,我有以下代码:
import character.*;
var player_1 = new characterObject("Player 1");
var player_2 = new characterObject("Player 2");
在包中 class:
package character {
public class characterObject extends MovieClip {
public var characterName: Number;
public var playerCounter: Number = 0;
public function characterObject(myName: String) {
characterName = myName;
playerCounter++;
}
}
我可以在 player_2 对象距离内访问 player_1.playerCounter 属性 吗?
仅当 total_player 时,我才需要增加玩家的价值(我想创建的 var 作为 player_1.playerCounter 的总和+ player_2.playerCounter + player_n.playerCounter ...) < of x .
我会创建一个向量来保存 characterObject class 之外的字符实例。然后你可以参考当前金额的向量长度。
var characters:Vector.<characterObject> = new Vector.<characterObject>();
characters.push( new characterObject("Player 1") );
characters.push( new characterObject("Player 2") );
trace( characters.length ); // 2
坏主意。
你可以尝试下一步
package character {
public class CharactersModel extends Object{
private var _chars : Array = [];
public function addCharacter(char : characterObject) {
_chars.push(char);
}
public function getChars() : Array {
return _chars;
}
public function getCharByName(name : String ) : characterObject {
//select from array and return
//use it instead of null
return null;
}
}
}
并像这样更新 characterObject
private var _model : CharactersModel;
public function characterObject(model : CharactersModel ,myName: String) {
_model = model;
characterName = myName;
playerCounter++;
model.addCharacter(this);
}
public function getOtherChar(name : String) {
return _model.getCharByName(name);
}
最后
import character.*;
var model : CharactersModel = new CharactersModel();
var player_1 = new characterObject(model ,"Player 1");
var player_2 = new characterObject(model ,"Player 2");
在 AIR Mobile 应用程序中,我有以下代码:
import character.*;
var player_1 = new characterObject("Player 1");
var player_2 = new characterObject("Player 2");
在包中 class:
package character {
public class characterObject extends MovieClip {
public var characterName: Number;
public var playerCounter: Number = 0;
public function characterObject(myName: String) {
characterName = myName;
playerCounter++;
}
}
我可以在 player_2 对象距离内访问 player_1.playerCounter 属性 吗? 仅当 total_player 时,我才需要增加玩家的价值(我想创建的 var 作为 player_1.playerCounter 的总和+ player_2.playerCounter + player_n.playerCounter ...) < of x .
我会创建一个向量来保存 characterObject class 之外的字符实例。然后你可以参考当前金额的向量长度。
var characters:Vector.<characterObject> = new Vector.<characterObject>();
characters.push( new characterObject("Player 1") );
characters.push( new characterObject("Player 2") );
trace( characters.length ); // 2
坏主意。
你可以尝试下一步
package character {
public class CharactersModel extends Object{
private var _chars : Array = [];
public function addCharacter(char : characterObject) {
_chars.push(char);
}
public function getChars() : Array {
return _chars;
}
public function getCharByName(name : String ) : characterObject {
//select from array and return
//use it instead of null
return null;
}
}
}
并像这样更新 characterObject
private var _model : CharactersModel;
public function characterObject(model : CharactersModel ,myName: String) {
_model = model;
characterName = myName;
playerCounter++;
model.addCharacter(this);
}
public function getOtherChar(name : String) {
return _model.getCharByName(name);
}
最后
import character.*;
var model : CharactersModel = new CharactersModel();
var player_1 = new characterObject(model ,"Player 1");
var player_2 = new characterObject(model ,"Player 2");