我是Actionscript 3编程的新手。我无法使用e.currentTarget作为对我点击的对象的引用。这是对象的缩写代码:
public class Character extends Sprite
{
private var _bodyPartLocations:Array = new Array();
private var _head:Head
private var _body:Body
public var characterImage:Sprite = new Sprite
private var _thumbnailBackground:CharacterThumbnailBackground
private var _headScaled:Head
public var thumbnail:Sprite = new Sprite
public function Character()
{
_bodyPartLocations =
[
[50, 0], //Head
[53, 80], //Body
];
_head = new Head();
_body = new Body();
Util.addSprite(_head, characterImage, _bodyPartLocations[0][0], _bodyPartLocations[0][1]);
Util.addSprite(_body, characterImage, _bodyPartLocations[1][0], _bodyPartLocations[1][1]);
_thumbnailBackground = new CharacterThumbnailBackground();
_headScaled = new Head;
_headScaled.scaleX = 0.8;
_headScaled.scaleY = 0.8;
Util.addSprite(_thumbnailBackground, thumbnail, 0, 0);
Util.addSprite(_headScaled, thumbnail, 0, 0);
}
}
基本上,我正在尝试创建两个精灵(characterImage和thumbnail),它们将成为整个角色精灵的一部分。 Util类只包含addChild的这个快捷方式和我的位置:
static public function addSprite(addedSprite:Sprite, toSprite:Sprite, xPos:int = 0, yPos:int = 0):void
{
toSprite.addChild(addedSprite);
addedSprite.x = xPos;
addedSprite.y = yPos;
}
我遇到的问题出现在我的角色创建菜单屏幕上。目前,它只包括一个显示字符缩略图的空间(并存储在一个数组中),一个用于在点击字符缩略图时显示字符图像的空间,最后一个"创建字符& #34;按钮。单击该按钮时,将创建一个字符,其缩略图将被赋予一个事件监听器,并添加缩略图以显示在菜单上(位置布尔检查只是我用字符数组绑定字符位置数组的方式,我省略了其他位置检查):
private function createCharacterHandler(e:MouseEvent):void
{
if (_characters.length < 6)
{
var char:Character = new Character();
char.thumbnail.addEventListener(MouseEvent.CLICK, characterClickHandler);
if (_positionZero == false)
{
Util.addSprite(char.thumbnail, this, _characterThumbnailPositions[0][0], _characterThumbnailPositions[0][1]);
_positionZero = true;
}
_characters.push(char);
}
这让我想到了这个问题。如果我单击字符缩略图,则此事件处理程序将执行:
private function characterClickHandler(e:MouseEvent):void
{
var selectedChar:Character = e.currentTarget as Character;
Util.addSprite(selectedChar.characterImage, this, _characterImagePosition[0][0], _characterImagePosition[0][1]);
}
正如您所看到的,我尝试使用e.currentTarget来尝试显示与我单击的角色的缩略图关联的characterImage。代码在我测试时运行,但是当我点击我生成的角色的缩略图时,Flash播放器会停止并且我收到此错误:
[Fault] exception, information=TypeError: Error #1009: Cannot access a property or method of a null object reference.
搜索互联网时,我看到其他人使用e.currentTarget作为对点击对象的引用(但我不想为我生成的每个字符创建实例名称,因为我计划生成字符并且定期删除,因此命名每一个变得太困难了)。我哪里错了?
使用更新编辑:
因此,将characterClickHandler更改为:
private function characterClickHandler(e:MouseEvent):void
{
var selectedChar:Character = DisplayObject(e.currentTarget).parent as Character;
Util.addSprite(selectedChar.characterImage, this, _characterImagePosition[0][0], _characterImagePosition[0][1]);
}
仍然会产生相同的错误错误。我甚至试过这个表达没有效果:
private function characterClickHandler(e:MouseEvent):void
{
var selectedThumbnail:DisplayObject = e.currentTarget as DisplayObject;
var selectedChar:Character = selectedThumbnail.parent as Character;
Util.addSprite(selectedChar.characterImage, this, _characterImagePosition[0][0], _characterImagePosition[0][1]);
}
为了查看我的Util.addSprite是否正常工作,我更改了createCharacterHandler以包含除缩略图之外的字符图像:
private function createCharacterHandler(e:MouseEvent):void
{
if (_characters.length < 6)
{
var char:Character = new Character();
char.thumbnail.addEventListener(MouseEvent.CLICK, characterClickHandler);
if (_positionZero == false)
{
Util.addSprite(char.thumbnail, this, _characterThumbnailPositions[0][0], _characterThumbnailPositions[0][1]);
_positionZero = true;
}
Util.addSprite(char.characterImage, this, _characterImagePosition[0][0], _characterImagePosition[0][1]);
_characters.push(char);
}
此代码为我提供了在用户点击缩略图后我想要的最终结果,因为角色图像现在也自动出现在菜单上。但是,我希望将&#34;用户点击缩略图&#34;一部分。
答案 0 :(得分:1)
您的问题是您的事件监听器未附加到Character
实例,而是Character
的缩略图(子):
var char:Character = new Character();
char.thumbnail.addEventListener(MouseEvent.CLICK, characterClickHandler);
因此e.currentTarget
指的是该缩略图,而e.currentTarget as Character;
执行会导致null
返回,因为缩略图不会转换为字符。
查看你的代码,看起来缩略图实际上是角色的孙子,所以你必须这样做:
var selectedChar:Character = DisplayObject(DisplayObject(e.currentTarget).parent).parent as Character;
由于如果您稍后更改层次结构,那么像这样的链接父母是丑陋的并且容易破坏,也许更好的方法是抽象出您的事件。所以在你的Character
课程中,请执行以下操作:(其中......是您现有的代码)
...
public var thumbnail:Sprite;
public static const THUMBNAIL_CLICKED:String = "ThumbnailClicked";
public function Character()
{
...
thumbnail = new Sprite();
thumbnail.addEventListener(MouseEvent.CLICK, thumbnailClickHandler,false,0,true);
...
private function thumbnailClickHandler(e:Event):void {
this.dispatchEvent(new Event(THUMBNAIL_CLICKED));
}
然后,在你的其他课程中你会这样做:
private function createCharacterHandler(e:MouseEvent):void
{
if (_characters.length < 6)
{
var char:Character = new Character();
char.addEventListener(Character.THUMBNAIL_CLICKED, characterClickHandler);
...
private function characterClickHandler(e:Event):void
{
var selectedChar:Character = e.currentTarget as Character;
Util.addSprite(selectedChar.characterImage, this, _characterImagePosition[0][0], _characterImagePosition[0][1]);
}