通过具有静态类型Class的引用访问可能未定义的属性alpha

时间:2015-12-08 17:11:45

标签: actionscript-3 flash

我不确定在角色的健康状况下降时如何设置透明度。

错误在第38行

package  
    {
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.ui.Keyboard;

    public class Character extends MovieClip
    {
        var velocity:Number; 
        var shootLimiter:Number;
        var health:Number;
        var maxHealth:Number;       


        public function Character()
        {
            velocity = 10;
            shootLimiter = 0;

            health = 100;
            maxHealth = 100;

            addEventListener("enterFrame", move);
            x = 300
            y = 150
        }

        function takeDamage(d)
        {
            health -= d;    
            if(health <= 0)
            {
                health = 0;
                kill();
            }
            Game.healthMeter.bar.scaleX = health/maxHealth;

            Character.alpha = health/100;
        }

        function kill()
        {
            var blood = new Blood();
            stage.addChild(blood);
            blood.x = this.x;
            blood.y = this.y;
            removeEventListener("enterFrame",move);
            this.visible = false;
            Game.gameOver();
        }
        function move(e:Event)
        {
            shootLimiter += 5;

            if(Key.isDown(Keyboard.RIGHT))
            {
                if(this.x <= 560)
                {
                    this.x = this.x + velocity;
                }
            }

            if(Key.isDown(Keyboard.LEFT))
            {
                if(this.x >= 40)
                {
                    this.x = this.x - velocity;
                }
            }
            if(Key.isDown(Keyboard.UP))
            {
                if(this.y > 20)
                {
                    this.y = this.y - velocity;
                }
            }
            if(Key.isDown(Keyboard.DOWN))
            {
                if(this.y < 280)
                {
                    this.y = this.y + velocity;
                }
            }

            if(Key.isDown(Keyboard.SPACE) && shootLimiter > 8)
            {
                shootLimiter = 0;
                var b = new Needles();
                stage.addChild(b);
                b.x = this.x+65;
                b.y = this.y+45;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

Character没有名为alpha的静态属性。您指的是该类的实例,因此它应该是this.alpha = health/100;而不是Character.alpha = health/100;