我想在Flex中制作类似于Shake& Fidget的“游戏”。我将所有用户数据保存在mysql数据库中,并使用ZendFramework(PHP)检索数据。 我想把我需要的所有用户和字符信息保存到AS类中,这样我就可以在每个视图中使用这些数据。
从数据库中收集的数据是正确的。我能够在我的应用程序labbels中加载它,但每次我改变视图时都必须再次询问它,所以我想要创建这些类,以便只询问一次信息。
我会在这里发布文件,以便更清楚。
ViewNavigatorAplication.mxml
<s:ViewNavigatorApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
firstView="views.RotrHomeView"
persistNavigatorState="true">
<fx:Script>
<![CDATA[
import flash.net.registerClassAlias;
import valueObjects.Character;
registerClassAlias("Character", valueObjects.Character);
]]>
</fx:Script>
第一个View进入登录界面,工作正常。所以我将转到加载和“尝试”显示字符数据的视图。
char_panel.mxml
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:usersservice1="services.usersservice1.*"
title="Character Sheet"
viewActivate="char_panel_viewActivateHandler()">
<fx:Script>
<![CDATA[
import mx.binding.utils.*;
import mx.events.FlexEvent;
import mx.rpc.events.ResultEvent;
import valueObjects.Character;
//[Bindable]protected var character:Character = new Character();
public function updateStats():void{
var str:int = parseInt(getGlobalStatsResult.lastResult.globalSTR) + parseInt(getCharacterStatsResult.lastResult.str);
var dex:int = parseInt(getGlobalStatsResult.lastResult.globalDEX) + parseInt(getCharacterStatsResult.lastResult.dex);
var intel:int = parseInt(getGlobalStatsResult.lastResult.globalINT) + parseInt(getCharacterStatsResult.lastResult.intel);
var cha:int = parseInt(getGlobalStatsResult.lastResult.globalCHA) + parseInt(getCharacterStatsResult.lastResult.cha);
var sta:int = parseInt(getGlobalStatsResult.lastResult.global_VIT) + parseInt(getCharacterStatsResult.lastResult.vit);
data.modStats(str,intel,cha,sta,dex)
data.showStats(lb_show_str,lb_show_dex,lb_show_int,lb_show_cha,lb_show_vit);
//character.showStats(lb_show_str,lb_show_dex,lb_show_int,lb_show_cha,lb_show_vit);
}
public function char_panel_viewActivateHandler():void{
if(!data){
data = new Character();
}
getCharacterStatsResult.token = usersService1.getCharacterStats("user01");
getGearListResult.addEventListener(ResultEvent.RESULT,onResult);
getGearListResult.token = usersService1.getGearList();
}
public function onStatsResult(event:ResultEvent):void{
if(getGlobalStatsResult.lastResult.globalSTR != null){
updateStats();
}
}
public function onResult(event:ResultEvent):void{
if(getGearListResult.lastResult[0].itemName != null){
getGlobalStatsResult.addEventListener(ResultEvent.RESULT, onStatsResult);
getGlobalStatsResult.token = usersService1.getGlobalStats("user01");
currentState = "Character";
}
}
]]>
</fx:Script>
<s:states>
<s:State name="Loading"/>
<s:State name="Character"/>
</s:states>
<fx:Declarations>
<s:CallResponder id="getCharacterStatsResult"/>
<s:CallResponder id="getGearListResult"/>
<s:CallResponder id="getGlobalStatsResult"/>
<usersservice1:UsersService1 id="usersService1"/>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:Label id="lb_show_str" includeIn="Character" x="119" y="46" text=""/>
<s:BusyIndicator includeIn="Loading" left="172" right="171" top="148" bottom="424"
horizontalCenter="0" verticalCenter="-138"/>
<s:Label id="lb_show_int" includeIn="Character" x="119" y="90"/>
<s:Label id="lb_show_cha" includeIn="Character" x="119" y="139"/>
<s:Label id="lb_show_vit" includeIn="Character" x="119" y="191"/>
<s:Label id="lb_show_dex" includeIn="Character" x="119" y="243"/>
至于AS类:
Character.as
package valueObjects{
import mx.data.ChangedItems;
import mx.messaging.channels.StreamingAMFChannel;
import spark.components.Label;
import spark.skins.spark.StackedFormHeadingSkin;
[Bindable]
public class Character
{
private var _name:String;
private var _stats:FinalStats;
private var _gear:GearList;
public function Character()
{
this._stats = new FinalStats();
this._gear = new GearList();
}
public function modStats(str:int,intel:int,cha:int,sta:int,dex:int):void{
this._stats.modStr(str);
this._stats.modInt(intel);
this._stats.modCha(cha);
this._stats.modVit(sta);
this._stats.modDex(dex);
}
public function getStats():Array{
var aStats:Array;
aStats["str"]=this._stats.getStr();
aStats["int"]=this._stats.getInt();
aStats["cha"]=this._stats.getCha();
aStats["sta"]=this._stats.getVit();
aStats["dex"]=this._stats.getDex();
return aStats;
}
public function setName(charName:String):void{
this._name = charName;
}
public function getName():String{
return this._name;
}
public function showStats(lbSTR:Label, lbDEX:Label, lbINT:Label, lbCHA:Label, lbVIT:Label):void{
lbSTR.text = "" + this._stats.getStr();
lbDEX.text = "" + this._stats.getDex();
lbINT.text = "" + this._stats.getInt();
lbCHA.text = "" + this._stats.getCha();
lbVIT.text = "" + this._stats.getVit();
}
}}
FinalStats.as
package valueObjects{
public class FinalStats
{
private var str:int = 0;
private var intel:int = 0;
private var cha:int = 0;
private var sta:int = 0;
private var dex:int = 0;
public function FinalStats()
{
}
public function getStr():int{
return this.str;
}
public function modStr(x:int):void{
this.str+=x;
}
public function getDex():int{
return this.dex;
}
public function modDex(x:int):void{
this.dex+=x;
}
public function getInt():int{
return this.intel;
}
public function modInt(x:int):void{
this.intel+=x;
}
public function getCha():int{
return this.cha;
}
public function modCha(x:int):void{
this.cha+=x;
}
public function getVit():int{
return this.sta;
}
public function modVit(x:int):void{
this.sta+=x;
}
}
}
最后一个GearList.as
package valueObjects{
import mx.data.ChangedItems;
import mx.messaging.channels.StreamingAMFChannel;
import spark.components.Label;
import spark.skins.spark.StackedFormHeadingSkin;
public class GearList
{
private var _headID:String;
private var _shoulderID:String;
private var _chestID:String;
private var _bracersID:String;
private var _glovesID:String;
private var _pantsID:String;
private var _bootsID:String;
private var _main_handID:String;
private var _off_handID:String;
public function GearList()
{
}
public function showStats(lbHead:Label, lbShoulder:Label, lbChest:Label, lbBracer:Label, lbGlove:Label, lbPants:Label, lbBoots:Label, lbMainHand:Label, lbOffHand:Label):void{
lbHead.text = ""+this._headID;
lbShoulder.text = ""+this._shoulderID;
lbChest.text = ""+this._chestID;
lbBracer.text = ""+this._bracersID;
lbGlove.text = ""+this._glovesID;
lbPants.text = ""+this._pantsID;
lbBoots.text = ""+this._bootsID;
lbMainHand.text = ""+this._main_handID;
lbOffHand.text = ""+this._off_handID;
}
public function getOff_handID():String
{
return _off_handID;
}
public function setOff_handID(value:String):void
{
_off_handID = value;
}
public function getMain_handID():String
{
return _main_handID;
}
public function setMain_handID(value:String):void
{
_main_handID = value;
}
public function getBootsID():String
{
return _bootsID;
}
public function setBootsID(value:String):void
{
_bootsID = value;
}
public function getPantsID():String
{
return _pantsID;
}
public function setPantsID(value:String):void
{
_pantsID = value;
}
public function getGlovesID():String
{
return _glovesID;
}
public function setGlovesID(value:String):void
{
_glovesID = value;
}
public function getBracersID():String
{
return _bracersID;
}
public function setBracersID(value:String):void
{
_bracersID = value;
}
public function getChestID():String
{
return _chestID;
}
public function setChestID(value:String):void
{
_chestID = value;
}
public function getShoulderID():String
{
return _shoulderID;
}
public function setShoulderID(value:String):void
{
_shoulderID = value;
}
public function getHeadID():String
{
return _headID;
}
public function setHeadID(value:String):void
{
_headID = value;
}
}}
如果你还在这里,你会得到我的尊重:D 当我尝试它时,我得到以下错误。我在char_panel.mxml中尝试使用Character :: modStats(...)而不是data.modStats我会把错误日志放在这个下面。
使用data.modStats(...)
时的错误日志TypeError:错误#1006:modStats不是函数。 at views :: char_panel / updateStats()[C:\ Users \ Zebrah \ Rotr \ Rotr \ src \ views \ char_panel.mxml:23] at views :: char_panel / onStatsResult()[C:\ Users \ Zebrah \ Rotr \ Rotr \ src \ views \ char_panel.mxml:39] 在flash.events::EventDispatcher/dispatchEventFunction() at flash.events::EventDispatcher/dispatchEvent() at mx.rpc :: CallResponder / result()[E:\ dev \ 4.5.1 \ frameworks \ projects \ rpc \ src \ mx \ rpc \ CallResponder.as:122] 在mx.rpc :: AsyncToken / http://www.adobe.com/2006/flex/mx/internal :: applyResult()[E:\ dev \ 4.5.1 \ frameworks \ projects \ rpc \ src \ mx \ RPC \ AsyncToken.as:239] 在mx.rpc.events::ResultEvent/http://www.adobe.com/2006/flex/mx/internal::callTokenResponders()[E:\ dev \ 4.5.1 \ frameworks \ projects \ rpc \ src \ MX \ RPC \事件\ ResultEvent.as:207] at mx.rpc :: AbstractOperation / http://www.adobe.com/2006/flex/mx/internal :: dispatchRpcEvent()[E:\ dev \ 4.5.1 \ frameworks \ projects \ rpc \ src \ mx \ RPC \ AbstractOperation.as:244] 在mx.rpc :: AbstractInvoker / http://www.adobe.com/2006/flex/mx/internal :: resultHandler()[E:\ dev \ 4.5.1 \ frameworks \ projects \ rpc \ src \ mx \ RPC \ AbstractInvoker.as:318] 在mx.rpc :: Responder / result()[E:\ dev \ 4.5.1 \ frameworks \ projects \ rpc \ src \ mx \ rpc \ Responder.as:56] 在mx.rpc :: AsyncRequest / acknowledge()[E:\ dev \ 4.5.1 \ frameworks \ projects \ rpc \ src \ mx \ rpc \ AsyncRequest.as:84] 在NetConnectionMessageResponder / resultHandler()[E:\ dev \ 4.5.1 \ frameworks \ projects \ rpc \ src \ mx \ messaging \ channels \ NetConnectionChannel.as:552] 在mx.messaging :: MessageResponder / result()[E:\ dev \ 4.5.1 \ frameworks \ projects \ rpc \ src \ mx \ messaging \ MessageResponder.as:235]
使用Character :: modStats(...)
的错误日志错误:错误#1034:类型强制失败:无法将valueObjects :: Character $转换为命名空间。 at views :: char_panel / updateStats()[C:\ Users \ Zebrah \ Rotr \ Rotr \ src \ views \ char_panel.mxml:23] at views :: char_panel / onStatsResult()[C:\ Users \ Zebrah \ Rotr \ Rotr \ src \ views \ char_panel.mxml:39] 在flash.events::EventDispatcher/dispatchEventFunction() at flash.events::EventDispatcher/dispatchEvent() at mx.rpc :: CallResponder / result()[E:\ dev \ 4.5.1 \ frameworks \ projects \ rpc \ src \ mx \ rpc \ CallResponder.as:122] 在mx.rpc :: AsyncToken / http://www.adobe.com/2006/flex/mx/internal :: applyResult()[E:\ dev \ 4.5.1 \ frameworks \ projects \ rpc \ src \ mx \ RPC \ AsyncToken.as:239] 在mx.rpc.events::ResultEvent/http://www.adobe.com/2006/flex/mx/internal::callTokenResponders()[E:\ dev \ 4.5.1 \ frameworks \ projects \ rpc \ src \ MX \ RPC \事件\ ResultEvent.as:207] at mx.rpc :: AbstractOperation / http://www.adobe.com/2006/flex/mx/internal :: dispatchRpcEvent()[E:\ dev \ 4.5.1 \ frameworks \ projects \ rpc \ src \ mx \ RPC \ AbstractOperation.as:244] 在mx.rpc :: AbstractInvoker / http://www.adobe.com/2006/flex/mx/internal :: resultHandler()[E:\ dev \ 4.5.1 \ frameworks \ projects \ rpc \ src \ mx \ RPC \ AbstractInvoker.as:318] 在mx.rpc :: Responder / result()[E:\ dev \ 4.5.1 \ frameworks \ projects \ rpc \ src \ mx \ rpc \ Responder.as:56] 在mx.rpc :: AsyncRequest / acknowledge()[E:\ dev \ 4.5.1 \ frameworks \ projects \ rpc \ src \ mx \ rpc \ AsyncRequest.as:84] 在NetConnectionMessageResponder / resultHandler()[E:\ dev \ 4.5.1 \ frameworks \ projects \ rpc \ src \ mx \ messaging \ channels \ NetConnectionChannel.as:552] 在mx.messaging :: MessageResponder / result()[E:\ dev \ 4.5.1 \ frameworks \ projects \ rpc \ src \ mx \ messaging \ MessageResponder.as:235]
提前感谢任何在此帮助中达到目前为止的人:D我会准备你可以做的任何sugestions。
答案 0 :(得分:0)
好吧,所以它不能用于数据的原因是那个事件虽然你说过
data = new Character();
数据实际上仍然是一个对象(现在看起来像一个字符)。使它工作
var myChar:Character = Character(data); // cast it to a Character and it now knows the method
myChar.modStats()
另一个电话不起作用的原因是你打电话的方式:
Character::modStats(...)
这就是在命名空间Character中查找名为modStats的方法。而是调用此
character.modStats()
它会起作用。
答案 1 :(得分:0)
你先生是我个人的英雄:D,我刚试过它并显示数据。
我更改了Character.AS中字符的构造函数,如下所示:
public function Character(obj:Object)
{
this._stats = new FinalStats();
this._gear = new GearList();
}
我不完全确定这是否正确,但正如你在答案中提到的那样:
var myChar:Character = Character(data);
我嘲笑我必须使用那个数据对象来初始化myChar ...我是对的吗?好吧,稍后当我有时间我会尝试添加新的视图和按钮来浏览视图并查看数据是否存在时:D