HittestObject在ActionScript类中不起作用

发布于 2025-02-02 01:32:51 字数 501 浏览 3 评论 0原文

我正在尝试测试.AS文件中的子弹是否触摸.fla文件中定义的播放器。

我在.fla文件中有此IF语句

if (spaceBarPressed == true) {
    var eb = new EnemyBullet(player)
    stage.addChild(eb)
    eb.x = enemy.x + 120
    eb.y = enemy.y + 120
}

,并且在我的.AS文件中有此语句

public function EnemyBullet(player) {
    addEventListener(Event.ENTER_FRAME, update)
    if (this.hitTestObject(player)) {
        trace("hit")
    }
}
function update(event:Event) {
    this.x+=5
}

,但我似乎无法使其工作。

I am trying to test if the bullet defined in the .as file is touching the player defined in the .fla file.

I have this if statement in my .fla file

if (spaceBarPressed == true) {
    var eb = new EnemyBullet(player)
    stage.addChild(eb)
    eb.x = enemy.x + 120
    eb.y = enemy.y + 120
}

and this in my .as file

public function EnemyBullet(player) {
    addEventListener(Event.ENTER_FRAME, update)
    if (this.hitTestObject(player)) {
        trace("hit")
    }
}
function update(event:Event) {
    this.x+=5
}

But I can't seem to get it to work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜点 2025-02-09 01:32:52

我通过将对象播放器发送到类中的函数eneybullet,然后将其存储在类型的DisplayObject中,以将其存储在函数更新中。

.fla文件中的if statment的内容:

if (spaceBarPressed == true) {
    var eb = new EnemyBullet(player)
    stage.addChild(eb)
    eb.x = enemy.x + 120
    eb.y = enemy.y + 120
}

.as file 的内容

package  {
    
    import flash.display.MovieClip;
    import flash.display.DisplayObject;
    import flash.events.Event   
    
    public class EnemyBullet extends MovieClip {
        var $thing:DisplayObject
        
        public function EnemyBullet($testObject:DisplayObject) {
            $thing = $testObject
            addEventListener(Event.ENTER_FRAME, update)
        }
        function update(e:Event) {
            this.x+=5
            if ($thing.hitTestObject(this) ) {
                trace("HIT")
            }
        }

    }
    
}

I fixed it by sending the object player to the function EnemyBullet in the class, and stored it in a variable of type DisplayObject to reference it in the function update.

Contents of if Statment in .fla file:

if (spaceBarPressed == true) {
    var eb = new EnemyBullet(player)
    stage.addChild(eb)
    eb.x = enemy.x + 120
    eb.y = enemy.y + 120
}

Contents of .as file

package  {
    
    import flash.display.MovieClip;
    import flash.display.DisplayObject;
    import flash.events.Event   
    
    public class EnemyBullet extends MovieClip {
        var $thing:DisplayObject
        
        public function EnemyBullet($testObject:DisplayObject) {
            $thing = $testObject
            addEventListener(Event.ENTER_FRAME, update)
        }
        function update(e:Event) {
            this.x+=5
            if ($thing.hitTestObject(this) ) {
                trace("HIT")
            }
        }

    }
    
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文