【问题标题】:As3: Bullet giving me errors?As3:子弹给我错误?
【发布时间】:2014-04-06 08:29:02
【问题描述】:

我有一个向玩家生成子弹的敌人类。

        private function fireBullet()
        {
            if(isFiring)
            {
            fire();
            }
        }

    public function fire():void
    {
        var bullet:Bullet = new Bullet(x, y, rotation);
        stage.addChild(bullet);
    }

在子弹类中:

package  {

import flash.display.MovieClip;
import flash.events.Event;

public class Bullet extends MovieClip {

    private var _root:MovieClip;
    private var isVanished:Boolean = false;

    public function Bullet(x:int, y:int, rotation:Number) 
    {
        this.x = x;
        this.y = y;
        this.rotation = rotation;

        _root = MovieClip(root);
        addEventListener(Event.ENTER_FRAME, loop);
    }

    private function loop (event:Event):void
    {           
        if(this.hitTestObject(_root.assassin.hitbox))
               {
                   _root.hitPoints -= 30;
                               }


        else
        {
            y-=Math.cos(rotation/-180*Math.PI)*(15);
            x-=Math.sin(rotation/-180*Math.PI)*(15);
        }

        if(this.x < 0 || this.x > _root.stageWidth || this.y > _root.stageWidth || this.y < 0)
        {
            removeChild(this);
            removeEventListener(Event.ENTER_FRAME, loop);
        }
    }
}

}

但是,当我开始游戏时,我收到关于第 23 行的 1009 错误,并且由于子弹甚至没有移动,游戏速度迅速变慢。

我也收到 1063 错误,预期为 3,但实际为 0。

ArgumentError: Error #1063: Argumentblabla for Bullet(). Expected 3 but 0 were shown. ((translated))
at flash.display::Sprite/constructChildren()
at flash.display::Sprite()
at flash.display::MovieClip()
at Main()[C:\Venture Inc\Main.as:189]

这是 main 的样子

//Constructor
        public function Main()
        {

            addChild(container_staff);
            addChild(container_wall);

                  etc etc etc

【问题讨论】:

  • 我觉得我们缺少一些上下文。你能把所有的代码贴出来吗?
  • 有很多代码,所以我真的不知道还要发布什么。我注意到,如果我删除除 (_root.hitPoints -= 30;) 之外的所有代码,我仍然会得到错误。似乎错误意味着_root。为空?
  • 如果您不确定从哪里开始,最好的故障排除方法是添加一些跟踪语句,查看哪些在错误之前执行,以及一旦您发现错误在哪里可能会开始注释行以查看是否可以消除错误。一旦你发现它修复它应该很容易。
  • 如我所说,如果我只有私有函数循环 (event:Event):void { _root.hitPoints -= 30;它仍然给我错误。它必须与_root 相关。 (不过,它的定义是正确的:private var _root:MovieClip; 然后 _root = MovieClip(root); 在构造函数中。
  • 当然可以,只是我看不到它的定义位置。假设未显示的部分是正确的,您的所有代码看起来都很好。如果您遇到错误,显然不是?错误读起来就像您在调用方法/构造函数时忘记添加一些参数

标签: actionscript-3 runtime-error movieclip addchild projectile


【解决方案1】:
ArgumentError: Error #1063: Argumentblabla for Bullet(). Expected 3 but 0 were shown. ((translated))

Bullet 类构造函数有 3 个参数。更准确地检查你的代码,我认为你有这样的东西:

var bullet: Bullet = new Bullet();

您的代码也有问题。每个项目符号都订阅 ENTER_FRAME。集中您的游戏,创建主循环,并从那里更新所有游戏角色(子弹、敌人等)。

【讨论】:

    猜你喜欢
    • 2014-04-08
    • 1970-01-01
    • 2021-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-12
    • 2014-12-22
    相关资源
    最近更新 更多