Flash 使用自定义事件时发生AS3引用错误
*编辑-当我试图更改movieclip的图形属性时,我发现出现错误的原因。* 我收到这个错误。“ReferenceError:Error#1069:在flash.events.MouseEvent上找不到属性项,并且在PlayScreen/onClick()上没有默认值。”。 我做了一些调查,大多数网站都说这是因为打字错误。但据我所知,它看起来很好 据我所知,我有两件事的安排完全相同。一个在单击时触发,另一个在悬停时触发。hover事件工作正常,但是当我使用onclick事件时,我收到了上面的错误。然而,即使我收到了错误,函数仍然正常工作 这是我的密码。 下面的代码创建一个新网格。然后添加自定义事件。悬停事件工作正常,没有错误。onclick事件工作正常,但我得到了错误Flash 使用自定义事件时发生AS3引用错误,flash,actionscript-3,Flash,Actionscript 3,*编辑-当我试图更改movieclip的图形属性时,我发现出现错误的原因。* 我收到这个错误。“ReferenceError:Error#1069:在flash.events.MouseEvent上找不到属性项,并且在PlayScreen/onClick()上没有默认值。”。 我做了一些调查,大多数网站都说这是因为打字错误。但据我所知,它看起来很好 据我所知,我有两件事的安排完全相同。一个在单击时触发,另一个在悬停时触发。hover事件工作正常,但是当我使用onclick事件时,我收到了上面的错
package {
import flash.events.MouseEvent;
import flash.display.MovieClip;
public class PlayScreen extends MovieClip {
public var grid:Grid;
public function PlayScreen() {
grid = new Grid();
grid.addEventListener( GridEvent.HOVER, onHover);
grid.addEventListener( GridEvent.TILECLICK, onClick);
grid.x = 0;
grid.y = 0;
addChild( grid );
}
public function onHover(event:*){
event.item.graphics.beginFill(0x66ff66);
event.item.graphics.lineStyle(2, 0x22ff22);
event.item.graphics.drawRect(-30*0.5, -30*0.5, 30, 30);
event.item.graphics.endFill();
}
public function onClick(event:*){
event.item.graphics.beginFill(0x000000);
event.item.graphics.lineStyle(2, 0x22ff22);
event.item.graphics.drawRect(-30*0.5, -30*0.5, 30, 30);
event.item.graphics.endFill();
}
下面是我的网格课。它包含一个movieclip,其中添加了两个mouseevent,然后触发自定义事件
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Grid extends MovieClip {
public function Grid() {
var test = new MovieClip();
test.x = 0;
test.y = 0;
test.graphics.beginFill(0x66ff66);
test.graphics.drawRect(-tileWidth*0.5, -tileHeight*0.5, tileWidth, tileHeight);
test.graphics.endFill();
test.addEventListener(MouseEvent.MOUSE_OVER, overTile);
test.addEventListener(MouseEvent.MOUSE_DOWN, clickTile);
addChild(test);
}
function overTile (event:*) {
dispatchEvent( new GridEvent( GridEvent.HOVER, event.target) );
}
function clickTile(event:*) {
dispatchEvent( new GridEvent( GridEvent.TILECLICK, event.target) );
}
}
下面是我的定制活动
package
{
import flash.events.Event;
public class GridEvent extends Event
{
public static const HOVER :String = "hover";
public static const TILECLICK :String = "click";
public var item;
public function GridEvent (type:String, item)
{
this.item = item;
super(type);
}
}
}
尝试为
事件
和项
指定类型。
使用Sprite
而不是MovieClip
还有一件事我没有实现——最好是为你的tiles创建一个自定义Tile类,然后你可以将GridEvent中的项目类型设置为Tile而不是Sprite/MovieClip
这应该行得通
package
{
import flash.events.MouseEvent;
import flash.display.MovieClip;
public class PlayScreen extends MovieClip
{
public var grid:Grid;
public function PlayScreen()
{
grid = new Grid();
grid.addEventListener( GridEvent.HOVER, onHover);
grid.addEventListener( GridEvent.TILECLICK, onClick);
grid.x = 0;
grid.y = 0;
addChild( grid );
}
private function onHover(event:GridEvent):void
{
if (event.item != null)
{
var g:Graphics = event.item.graphics;
g.beginFill(0x66ff66);
g.lineStyle(2, 0x22ff22);
g.drawRect(-30*0.5, -30*0.5, 30, 30);
g.endFill();
}
}
private function onClick(event:GridEvent):void
{
if (event.item != null)
{
var g:Graphics = event.item.graphics;
g.beginFill(0x000000);
g.lineStyle(2, 0x22ff22);
g.drawRect(-30*0.5, -30*0.5, 30, 30);
g.endFill();
}
}
}
}
这是你的全部源代码吗?当您甚至没有为事件提供类型时,Flash没有理由会给出关于MouseeEvent的错误。肯定还有其他一些问题。它不是所有的代码,因为完整的代码相当广泛。然而,我也有同样的问题。我发现这是当我试图改变图形属性。如果我不这样做,我就不会出错。所以我不明白为什么它会给我一个onclick函数的错误,而不是hover函数的错误?。谢谢你的回复:)谢谢你这已经解决了我的问题!您的示例非常好,我将创建一个tile类:)。
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Grid extends Sprite
{
public function Grid()
{
var test = new Sprite();
test.x = 0;
test.y = 0;
test.graphics.beginFill(0x66ff66);
test.graphics.drawRect(-tileWidth*0.5, -tileHeight*0.5, tileWidth, tileHeight);
test.graphics.endFill();
test.addEventListener(MouseEvent.MOUSE_OVER, overTile);
test.addEventListener(MouseEvent.MOUSE_DOWN, clickTile);
addChild(test);
test.mouseChildren = false;
}
private function overTile (event:MouseEvent):void
{
if (event.cancelable)
{
// stop MouseEvent from bubbling
event.stopImmediatePropagation();
}
dispatchEvent( new GridEvent( GridEvent.HOVER, event.target) );
}
private function clickTile(event:MouseEvent):void
{
if (event.cancelable)
{
// stop MouseEvent from bubbling
event.stopImmediatePropagation();
}
dispatchEvent( new GridEvent( GridEvent.TILECLICK, event.target) );
}
}
}
package
{
import flash.events.Event;
public class GridEvent extends Event
{
public static const HOVER :String = "hover";
public static const TILECLICK :String = "click";
public var item:Sprite;
public function GridEvent (type:String, item:Sprite)
{
this.item = item;
super(type);
}
}
}