如何为Bubble Shooter游戏设置气泡? AS3.0

时间:2012-07-11 07:00:11

标签: actionscript-3

我正在使用ActionScript 3.0制作一个泡泡射击游戏,但我无法正确放置气泡。看起来应该是这样的:

00000000
 00000000
00000000
 00000000

但我现在只放置第一排和第三排,我不知道我做错了什么..到目前为止,这是我的代码:

bubble_mc:(这是我的泡泡,有6种不同的泡泡)

package  {
    import flash.display.MovieClip;
    public class bubble_mc extends MovieClip {
        public function bubble_mc(val:uint,row:uint,col:uint) {
            gotoAndStop(val+1);
            name=row+"_"+col;
            x=50+col*36;
            y=40+row*36;
        }

    }

}

主要

package  {
    import flash.display.Sprite;
    import flash.events.KeyboardEvent;
    import flash.events.Event;
    public class Main extends Sprite {
        private const ROT_SPEED:uint=2;
        private const R:uint=18;
        private const D:Number=R*Math.sqrt(3);
        private var bubbleArr:Array=new Array();
        private var cannon:cannon_mc;
        private var bubble:bubble_mc;
        private var left:Boolean=false;
        private var right:Boolean=false;
        private var bubCont:Sprite;
        public function Main() {
            placeContainer();
            placeCannon();
            stage.addEventListener(KeyboardEvent.KEY_DOWN,onKDown);
            stage.addEventListener(KeyboardEvent.KEY_UP,onKUp);
            addEventListener(Event.ENTER_FRAME,onEFrame);
        }
        private function placeCannon():void {
            cannon=new cannon_mc();
            addChild(cannon);
            cannon.y=385.5;
            cannon.x=320;
        }
        private function onKDown(e:KeyboardEvent):void {
            switch(e.keyCode) {
                case 37 :
                    left=true;
                    break;
                case 39 :
                    right=true;
                    break;
            }
        }
        private function onKUp(e:KeyboardEvent):void {
            switch(e.keyCode) {
                case 37 :
                    left=false;
                    break;
                case 39 :
                    right=false;
                    break;
            }
        }
        private function onEFrame(e:Event):void {
            if (left) {
                cannon.rotation-=ROT_SPEED;
            }
            if (right) {
                cannon.rotation+=ROT_SPEED;
            }
        }
        private function placeContainer():void {
            bubCont=new Sprite();
            addChild(bubCont);
            bubCont.graphics.lineStyle(1,0xffffff,1);
            for (var i:uint=0; i<4; i++) {
                if (i%2==0) {
                    for (var j:uint=0; j<15; j++) {
                        bubbleArr[i]=new Array();
                        bubbleArr[i][j]=Math.floor(Math.random()*6);
                        bubble = new bubble_mc(bubbleArr[i][j],i,j);
                        bubCont.addChild(bubble);
                    }
                } else {
                        if (j<14) {
                            bubbleArr[i][j]=Math.floor(Math.random()*6);
                            bubble = new bubble_mc(bubbleArr[i][j],i,j);
                            bubble.x=68+i*36;
                            bubCont.addChild(bubble);
                        }
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我已经解决了!万一有人有同样的问题,这里是我解决它的代码! (我对x坐标所做的更改并不重要..)

<强> bubble_mc:

package  {
    import flash.display.MovieClip;
    public class bubble_mc extends MovieClip {
        public function bubble_mc(val:uint,row:uint,col:uint) {
            gotoAndStop(val+1);
            name=row+"_"+col;
            x=59+col*36;
            y=40+row*36;
        }
    }
}

主要

package  {
    import flash.display.Sprite;
    import flash.events.KeyboardEvent;
    import flash.events.Event;
    public class Main extends Sprite {
        private const ROT_SPEED:uint=2;
        private const R:uint=18;
        private const D:Number=R*Math.sqrt(3);
        private var bubbleArr:Array=new Array();
        private var cannon:cannon_mc;
        private var bubble:bubble_mc;
        private var left:Boolean=false;
        private var right:Boolean=false;
        private var bubCont:Sprite;
        public function Main() {
            placeContainer();
            placeCannon();
            stage.addEventListener(KeyboardEvent.KEY_DOWN,onKDown);
            stage.addEventListener(KeyboardEvent.KEY_UP,onKUp);
            addEventListener(Event.ENTER_FRAME,onEFrame);
        }
        private function placeCannon():void {
            cannon=new cannon_mc();
            addChild(cannon);
            cannon.y=385.5;
            cannon.x=320;
        }
        private function onKDown(e:KeyboardEvent):void {
            switch(e.keyCode) {
                case 37 :
                    left=true;
                    break;
                case 39 :
                    right=true;
                    break;
            }
        }
        private function onKUp(e:KeyboardEvent):void {
            switch(e.keyCode) {
                case 37 :
                    left=false;
                    break;
                case 39 :
                    right=false;
                    break;
            }
        }
        private function onEFrame(e:Event):void {
            if (left) {
                cannon.rotation-=ROT_SPEED;
            }
            if (right) {
                cannon.rotation+=ROT_SPEED;
            }
        }
        private function placeContainer():void {
            var iRow:Boolean=false;
            bubCont=new Sprite();
            addChild(bubCont);
            bubCont.graphics.lineStyle(1,0xffffff,1);
            for (var i:uint=0; i<4; i++) {
                if (! iRow) {
                    for (var j:uint=0; j<15; j++) {
                        bubbleArr[i]=new Array();
                        bubbleArr[i][j]=Math.floor(Math.random()*6);
                        bubble = new bubble_mc(bubbleArr[i][j],i,j);
                        bubCont.addChild(bubble);
                        iRow=true;
                    }
                } else {
                    for (j=0; j<15; j++) {
                        bubbleArr[i]=new Array();
                        bubbleArr[i][j]=Math.floor(Math.random()*6);
                        bubble = new bubble_mc(bubbleArr[i][j],i,j);
                        bubble.x=77+j*36;
                        bubCont.addChild(bubble);
                        iRow=false;
                    }
                }
            }
        }
    }
}