0

所以我试图从我的身体里射出多颗子弹,这一切都有效,除了我有一个奇怪的问题,即只有一颗子弹出现并更新为新子弹设置位置。我有一个可以移动的玩家,它应该可以射击,我通过移动玩家和射击来测试这段代码。我一步一步地创建它。

跟踪 bulletContainer 的结果是正确的,因为它告诉我电影剪辑正在添加到舞台;我只知道这归结为某种我忘记的逻辑。

这是我的代码(子弹本身就是一个类)

更新* 这段代码中的所有内容都可以正常工作,除了我之前说过的一些代码似乎是重复的,因为我采用了不同的方法。

子弹神等级:

public class bulletGod extends MovieClip{
    //Register Variables
        //~Global
            var globalPath                      = "http://127.0.0.1/fleshvirusv3/serverside/"

        //~MovieCLips
            var newBullet:bulletClass           = new bulletClass();

        //~Boolean
            var loadingBulletInProgress:Number  = 0;
            var shootingWeapon:Number           = 0;

        //~Timers
            var fireBulletsInterval     = setInterval(fireBullets, 1);
            var bulletFireEvent;

        //~Arrays
            var bulletArray:Array               = new Array();
            var bulletType:Array                = new Array();
            var bulletContainer:Array           = new Array();

        //~Networking
            var netBulletRequest:URLRequest     = new URLRequest(globalPath+"bullets.php");
            var netBulletVariables:URLVariables = new URLVariables();
            var netBulletLoader:URLLoader       = new URLLoader();          

        //~Bullet Image Loader
            var mLoader:Loader                  = new Loader();
            var mRequest:URLRequest             = new URLRequest();

                public function bulletGod() {
                    //Load every bullet for every gun

                    //Compile data to be requested
                         netBulletVariables.act             = "loadBullets"
                         netBulletRequest.method            = URLRequestMethod.POST
                         netBulletRequest.data              = netBulletVariables;
                         netBulletLoader.dataFormat         = URLLoaderDataFormat.VARIABLES;

                         netBulletLoader.addEventListener(Event.COMPLETE, getBulletImages);
                         netBulletLoader.load(netBulletRequest);

                }

                private function getBulletImages(bulletImageData:Event){
                    //Request every bullet URL image

                    //Set vars
                    var bulletData = bulletImageData.target.data;

                    //Load images
                    for(var i:Number = 0; i < bulletData.numBullets; i++){
                        bulletArray.push(bulletData["id"+i.toString()]);
                        bulletType.push(bulletData["bullet"+i.toString()]);
                        //trace(bulletData["id"+i]+"-"+bulletData["bullet"+i]);
                    }

                    //All the arrays have been set start firing the image loader/replacer
                    var imageLoaderInterval = setInterval(imageReplacer, 10);

                }

                private function imageReplacer(){
                    //Check to see which image needs replacing
                    if(!loadingBulletInProgress){
                        //Begin loading the next image
                            //Search for the next "String" in the bulletType:Array, and replace it with an image
                            for(var i:Number = 0; i < bulletType.length; i++){
                                if(getQualifiedClassName(bulletType[i]) == "String"){
                                    //Load this image
                                        mRequest = new URLRequest(globalPath+"ammo/"+bulletType[i]);
                                        mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadImage);
                                        mLoader.load(mRequest);

                                        //Stop imageReplacer() while we load image
                                        loadingBulletInProgress = 1;

                                        //Stop this for() loop while we load image
                                        i = 999;
                                }
                            }
                    }
                }

                private function loadImage(BlackHole:Event){
                    //Image has loaded; find which array slot it needs to go into

                    for(var i:Number = 0; i <= bulletType.length; i++){
                        if(getQualifiedClassName(bulletType[i]) == "String"){
                            //We found which array type it belongs to; now replace the text/url location with the actual image data
                            var tmpNewBullet:MovieClip = new MovieClip;
                                tmpNewBullet.addChild(mLoader);

                            //Add image to array
                                bulletType[i] = tmpNewBullet;

                            //Restart loadingBullets if there are more left
                                loadingBulletInProgress = 0;

                            //Stop for() loop
                            i = 999;
                        }
                    }
                }
//###############################################################################################################################################
            private function fireBullets(){
                //If player is holding down mouse; Fire weapon at rate of fire.
                if(shootingWeapon >= 1){
                    if(bulletFireEvent == null){
                        //Start shooting bullets
                        bulletFireEvent = setInterval(allowShooting, 500);
                    }
                }

                if(shootingWeapon == 0){
                    //The user is not shooting so stop all bullets from firing
                    if(bulletFireEvent != null){
                        //Strop firing bullets
                        clearInterval(bulletFireEvent);
                        bulletFireEvent = null
                    }
                }
            }

            private function allowShooting(){
                //This function actually adds the bullets on screen

                //Search for correct bullet/ammo image to attach
                    var bulletId:Number = 0;
                    for(var i:Number = 0; i < bulletArray.length; i++){
                        if(bulletArray[i] == shootingWeapon){
                            //Bullet found
                            bulletId = i;

                            //End For() loop
                            i = 999;
                        }
                    }

                //Create new bullet
                        //Create Tmp Bullet
                        var tmpBulletId:MovieClip = new MovieClip
                            tmpBulletId.addChild(newBullet);
                            tmpBulletId.addChild(bulletType[bulletId]);
                        //Add To Stage
                            addChild(tmpBulletId)
                            bulletContainer.push(tmpBulletId);  //Add to array of bullets

                //Orientate this bullet from players body
                        var bulletTmpId:Number = bulletContainer.length
                            bulletTmpId--;
                            bulletContainer[bulletTmpId].x = Object(root).localSurvivor.x
                            bulletContainer[bulletTmpId].y = Object(root).localSurvivor.y

                            //addChild(bulletContainer[bulletTmpId]);   
            }


            //_______________EXTERNAL EVENTS_______________________
                public function fireBullet(weaponId:Number){
                    shootingWeapon = weaponId;
                }

                public function stopFireBullets(){
                    shootingWeapon = 0;
                }
    }

}

子弹类:

package  com{
        import flash.display.*
        import flash.utils.*
        import flash.net.*
        import flash.events.*
    public class bulletClass extends MovieClip {
            public var damage:Number = 0;

        public function bulletClass() {
            //SOME MOVEMENT CODE HERE
        }

        public function addAvatar(Obj:MovieClip){
            this.addChild(Obj);
        }

    }

}
4

3 回答 3

1

好吧......如果我可以这样说,这段代码看起来很错误。要么代码中缺少某些东西,要么此代码永远不会让子弹飞起来。

首先,您可以直接设置新子弹的 x 和 y(将“从玩家身体定位此子弹”之后的所有内容替换为):

tmpBulletId.x = Object(root).localSurvivor.x;
tmpBulletId.y = Object(root).localSurvivor.y;

也许这已经有所帮助,但是您的代码应该已经这样做了。

但要让这些子弹飞向任何方向,还需要添加一个事件监听器,如下所示:

tmpBulletId.addEventListener(Event.ENTER_FRAME, moveBullet);

function moveBullet(e:Event) {
    var movedBullet:MovieClip = MovieClip(e.currentTarget);
    if (movedBullet.x < 0 || movedBullet.x > movedBullet.stage.width || 
        movedBullet.y < 0 || movedBullet.y > movedBullet.stage.height) {

        // remove move listener, because the bullet moved out of stage
        movedBullet.removeEventListener(Event.ENTER_FRAME);
    }
    // remove the comment (the //) from the line that you need
    MovieClip(e.currentTarget).x += 1; // move right
    // MovieClip(e.currentTarget).y -= 1; // move up
    // MovieClip(e.currentTarget).x -= 1; // move left
    // MovieClip(e.currentTarget).y += 1; // move down
}

这个例子让你的子弹飞到右边。如果您需要它飞向另一个方向,只需用“向右移动”注释注释掉该行并取消注释其他行之一。

这当然是一个非常简单的示例,但它应该可以帮助您入门。

我希望这会有所帮助,并且我的答案不是问题的错误答案。

于 2011-03-07T14:15:47.307 回答
0

据我所知,您只能将一个 MovieClip 对象的副本添加到特定的孩子。最好的方法是使用 ByteArray 作为剪辑源并实例化新的 MovieClip 并将 ByteArray 作为源传递。它与子/父关系有关,因为 DisplayObject 只能有一个父对象(以及一种将对象从场景中分离的方法)。

于 2011-03-07T13:55:21.487 回答
0

好吧,我最终第三次从头开始编写整个代码并遇到了类似的问题,只是为了参考其他任何遇到随机问题的人,因为我发现这个问题很可能是某个地方的转换错误不一定违反任何编译规则。只是我在调用一个movieclip,而不是它自己的类。

于 2011-03-09T06:52:04.013 回答