这是这个问题的后续问题,我不想继续发表评论并阻止人们获得来之不易的声誉...... :)
在我的 Cairngorm 命令类中,为了让它编译,我需要告诉它 myCanvas 是什么,所以我使用了这一行:
var myCanvas : MyCanvas = new MyCanvas;
不过,我猜这是错误的,因为尽管它可以编译,但如果我尝试执行以下操作:
if (myCanvas.subObject.value == 0) { ... }
它会抛出这个错误:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at com.foo.bar.command::MyCommand/execute()
好像子对象不存在一样。看起来我可能会得到一个新的 MyCanvas 实例,而不是我想要的来自 main.mxml 的实例,其 id 为 myCanvas。我对吗?我该如何解决?
编辑(格林威治标准时间+1 晚上 10:59):好的,所以看起来我在这里太含糊了。这是我的main.mxml
:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:control="com.foo.bar.control.*"
xmlns:business="com.foo.bar.business.*"
xmlns:view="com.foo.bar.view.*"
applicationComplete="com.foo.bar.util.StartupUtil.init()"
horizontalScrollPolicy="off"
verticalScrollPolicy="off"
borderThickness="0"
paddingBottom="0"
paddingLeft="0"
paddingTop="0"
paddingRight="0"
>
<mx:Script>
<![CDATA[
import com.foo.bar.model.PlayerModelLocator;
[Bindable]
private var model : PlayerModelLocator = PlayerModelLocator.getInstance();
]]>
</mx:Script>
<!-- ========================================================================== -->
<!-- the ServiceLocator where we specify the remote services -->
<business:Services id="services" />
<!-- the FrontController, containing Commands specific to this application -->
<control:PlayerController id="controller" />
<!-- ========================================================================== -->
<mx:Style source="assets/main.css" />
<view:MyCanvas id="myCanvas" />
</mx:Application>
这是我的com/foo/bar/command/MyCommand.as
:
package com.foo.bar.command {
/* add to controller
addCommand( MyEvent.EVENT_CHANGE_VOLUME, ChangeVolumeCommand );
*/
import flash.net.SharedObject;
import com.adobe.cairngorm.control.CairngormEvent;
import com.adobe.cairngorm.commands.ICommand;
import com.foo.bar.model.PlayerModelLocator;
import com.foo.bar.event.MyEvent;
import com.foo.bar.view.*;
public class ChangeVolumeCommand implements ICommand {
public function execute(event:CairngormEvent):void {
var model : PlayerModelLocator = PlayerModelLocator.getInstance();
var myEvent : MyEvent = MyEvent(event);
var myCanvas : MyCanvas = new MyCanvas();
var so:SharedObject = SharedObject.getLocal("fixie.video");
if (myCanvas.subObject.value == 0) {
trace("subobject value is 0");
}
}
}
}
myCanvas
基本上,我想在main.mxml
使用myCanvas
对象中获取具有 ID 的对象的句柄MyCommand.as