【问题标题】:AS3: Error #1034: Type Coercion failed: cannot convert to flash.display.DisplayObjectAS3:错误 #1034:类型强制失败:无法转换为 flash.display.DisplayObject
【发布时间】:2012-01-26 21:43:00
【问题描述】:

在使用 hitTestObject 时遇到了一些问题,现在 Flash 告诉我它无法将我的船转换为显示对象。我的问题是船类从 Sprite 开始扩展,所以我不知道发生了什么:

编译错误显示如下:

TypeError: Error #1034: Type Coercion failed: cannot convert Ship$ to flash.display.DisplayObject.
at Control/controlgame()

Control/controlgame() 是这样的:

function controlgame(e:Event) {
        for (var i = 0; i < wprojectiles.length; i ++) {
            if (wprojectiles[i].x < -200 || wprojectiles[i].x > 750 || wprojectiles[i].y < -200 || wprojectiles[i].y > 600) {
                parent.removeChild(wprojectiles[i]);
                wprojectiles.splice(i,1);
            }
            if (wprojectiles[i].hitTestObject(Ship)) {
                parent.removeChild(wprojectiles[i]);
                wprojectiles.splice(i,1);
            }
        }
    }

使用调试器,我得到这个错误:

TypeError: Error #1034: Type Coercion failed: cannot convert Ship$ to flash.display.DisplayObject.
at Control/controlgame()[C:\Users\Harry\Desktop\Flash Games\Games\Dodge\Control.as:29]

上面sn-p中看到的第29行是这样的:

if (wprojectiles[i].hitTestObject(Ship)) {

在这里扯我的头发,尝试了我能想到的一切,但我每次都收到这个错误,不管我做什么!非常感谢您的帮助!

提前致谢。

e:如果有相关性,这是我的文档类文件

【问题讨论】:

    标签: actionscript-3


    【解决方案1】:

    你在哪里声明Ship?看起来您正在使用Class 进行测试而不是实例...您在某处有类似Ship = new ShipClass() 的东西吗?

    【讨论】:

    • 我的船级叫做 Ship。让我改变一下,告诉你我怎么走。
    • 没有骰子,问题保持不变。我将我的船放在时间线/舞台上,因为如果我尝试输入“var s = new Ship()”,它会说 s 未定义。另外我刚刚进行了编辑..这是文档类有什么不同吗?我之前在命中测试对象方面没有遇到过这个问题。谢谢。
    • 好吧,如果你有一个文档类,你应该在那里声明类似public var s:Ship = new Ship(); 的东西。那么它不应该是未定义的..
    • public var s:Ship = new Ship(); s.x = 200; s.y = 200; 对两行都抛出错误,即C:\Users\Harry\Desktop\Flash Games\Games\Dodge\Control.as, Line 13 1120: Access of undefined property s.
    • 这里可能有误会。看看这个解释类结构的article。船舶的声明应该像本例中的“属性”一样声明。访问属性将在内部类方法或构造函数中......所以设置s.x = 200应该在方法内部!
    猜你喜欢
    • 1970-01-01
    • 2012-09-04
    • 2013-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多