【问题标题】:actionscript 3 - issues with object speedactionscript 3 - 对象速度问题
【发布时间】:2015-01-22 21:53:49
【问题描述】:

所以我刚刚建立了一个系统,其中一个物体从一个移动的物体向另一个物体发射。

我现在遇到的问题是它当前计算的方式,它根据被射击的物体与最初发射时另一个物体之间的距离使子弹移动得更慢。

视频讲解。

我希望子弹停留的速度:http://gyazo.com/3682d9adde0ed08aa675702d190c0d46.mp4

物体关闭时的速度: http://gyazo.com/553b0fb82d89a84f206ea00a9dc8daf0.mp4

这是从黄色物体发射的子弹的代码以及所有计算:

private function createBullet(): void {
        _pcos = Math.cos((this.rotation) * Math.PI / 180);
        _psin = Math.sin((this.rotation) * Math.PI / 180);
        _startX = this.x - _barrelLength * _pcos;
        _startY = this.y - _barrelLength * _psin;
        _endX = Init._player.x + 1 * _pcos + Math.random() * _bulletSpread -           _bulletSpread * .5;
        _endY = Init._player.y + 1 * _psin + Math.random() * _bulletSpread - _bulletSpread * .5;
        var tempBullet: MovieClip = new Bullet();
        tempBullet.vx = (_endX - _startX);
        tempBullet.vy = (_endY - _startY);
        tempBullet.x = _startX;
        tempBullet.y = _startY;
        tempBullet.rotation = -(Math.atan2(Init._player.x - this.x, Init._player.y - this.y)) * 180 / Math.PI;
        tempBullet.maxDistance = _maxDistance;
        _bullets.push(tempBullet);
        stage.addChild(tempBullet);
    }

    public function updateBullets(): void {
        var i: int;
        var tempBullet: MovieClip;
        for (i = 0; i < _bullets.length; i++) {
            tempBullet = _bullets[i];
            tempBullet.x += tempBullet.vx / _bulletSpeed;
            tempBullet.y += tempBullet.vy / _bulletSpeed;
            }
        }
    }

如果有人可以帮助计算,将不胜感激,谢谢。

【问题讨论】:

    标签: performance actionscript-3 rotation


    【解决方案1】:

    你想让你的子弹考虑到射击物体的速度吗?如果是,则将original 设置为 1,否则设置为 0。

        private static const original=1;
        private function createBullet(): void {
            _pcos = Math.cos((this.rotation) * Math.PI / 180);
            _psin = Math.sin((this.rotation) * Math.PI / 180);
            _startX = this.x - _barrelLength * _pcos;
            _startY = this.y - _barrelLength * _psin;
            _endX = Init._player.x + 1 * _pcos + Math.random() * _bulletSpread -               _bulletSpread * .5;
            _endY = Init._player.y + 1 * _psin + Math.random() * _bulletSpread - _bulletSpread * .5;
            var tempBullet: MovieClip = new Bullet();
            var activeAngle:Number=Math.atan2(_endY-_startY,_endX-_startX);
            tempBullet.vx = _bulletSpeed*Math.cos(activeAngle)+original*this.vx;
            tempBullet.vy = _bulletSpeed*Math.sin(activeAngle)+original*this.vy;
            tempBullet.x = _startX;
            tempBullet.y = _startY;
            tempBullet.rotation = -(Math.atan2(Init._player.x - this.x, Init._player.y - this.y)) * 180 / Math.PI;
            tempBullet.maxDistance = _maxDistance;
            _bullets.push(tempBullet);
            stage.addChild(tempBullet);
        }
    
        public function updateBullets(): void {
            var i: int;
            var tempBullet: MovieClip;
            for (i = 0; i < _bullets.length; i++) {
                tempBullet = _bullets[i];
                tempBullet.x += tempBullet.vx;
                tempBullet.y += tempBullet.vy;
            }
        }
    }
    

    请注意,_bulletSpeed 以每帧像素而不是每秒像素为单位测量,并假定为静态。该代码未经测试,请确保正负号已正确设置为您的角度系统。

    【讨论】:

    • 嗨,非常感谢您的帮助,我最初收到错误 Line 210, Column 64 : 1119: Access of possible undefined property vx through a reference with static type code.functions:EnemyYellow。所以我只是用 1 替换了 this.vx/this.vy 并且它工作得很好。再次感谢。
    • 我希望你的单位也有速度属性,而不仅仅是子弹。如果有,请将它们放入代码中,否则,将 +original*... 部分从两个分配中删除。
    猜你喜欢
    • 2011-08-12
    • 2011-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多