推箱子游戏的测试代码

      Flash 2005-2-24 16:1
测试代码如下:看上去很繁琐 其实很简单 也许是我的代码还可以优化。
------------------------------------------------------------------
class moveBox {
    var map:Array;
    var dir:Array
    
    public function moveBox() {
        _root.dir=[[1,0],[0,1],[-1,0],[0,-1]]
        init()
        var human=_root["human"]
        var mini=_root["mini"]
        var box=_root["box"]
        human.onKeyDown=function(){
            
            if(Key.isDown(Key.LEFT)){
            //trace(_root.dir[2][0])
                if(direct(human._x,human._y,2)=="space"){
                    human._x+=_root.dir[2][0]*20
                    human._y+=_root.dir[2][1]*20
                    }else if(direct(human._x,human._y,2)=="box"){
                    human._x+=_root.dir[2][0]*20
                    human._y+=_root.dir[2][1]*20
                    _root.map[box._x/20][box._y/20]=0
                    box._x+=_root.dir[2][0]*20
                    box._y+=_root.dir[2][1]*20
                    _root.map[box._x/20][box._y/20]=2
                    
                    }
                }
                if(Key.isDown(Key.UP)){
                if(direct(human._x,human._y,3)=="space"){
                    human._x+=_root.dir[3][0]*20
                    human._y+=_root.dir[3][1]*20
                    }else if(direct(human._x,human._y,3)=="box"){
                    human._x+=_root.dir[3][0]*20
                    human._y+=_root.dir[3][1]*20
                    _root.map[box._x/20][box._y/20]=0
                    box._x+=_root.dir[3][0]*20
                    box._y+=_root.dir[3][1]*20
                    _root.map[box._x/20][box._y/20]=2
                    //_root.map[box._x/20][box._y/20]=2
                    }
                }
                if(Key.isDown(Key.RIGHT)){
                if(direct(human._x,human._y,0)=="space"){
                    human._x+=_root.dir[0][0]*20
                    human._y+=_root.dir[0][1]*20
                    //trace("run")
                    }else if(direct(human._x,human._y,0)=="box"){
                    human._x+=_root.dir[0][0]*20
                    human._y+=_root.dir[0][1]*20
                    _root.map[int(box._x/20)][int(box._y/20)]=0
                    box._x+=_root.dir[0][0]*20
                    box._y+=_root.dir[0][1]*20
                    _root.map[int(box._x/20)][int(box._y/20)]=2
                    //trace(_root.map[box._x/20][box._y/20])
                    //_root.map[box._x/20][box._y/20]=2
                    }
                }
                if(Key.isDown(Key.DOWN)){
                if(direct(human._x,human._y,1)=="space"){
                    human._x+=_root.dir[1][0]*20
                    human._y+=_root.dir[1][1]*20
                }else if(direct(human._x,human._y,1)=="box"){
                human._x+=_root.dir[1][0]*20
                    human._y+=_root.dir[1][1]*20
                    _root.map[box._x/20][box._y/20]=0
                    box._x+=_root.dir[1][0]*20
                    box._y+=_root.dir[1][1]*20
                    _root.map[box._x/20][box._y/20]=2
                    //_root.map[box._x/20][box._y/20]=2
                    }
                }
                if(box._x==mini._x and box._y==mini._y){
                trace("successfully")
                }
            }
        Key.addListener(human)
        function direct(X,Y,D){
        var x=X/20;
        var y=Y/20;
        var d=D;
        //trace(d)
        //trace(_root.map[x+_root.dir[d][0]][y+_root.dir[d][1]])
        //trace(_root.map[x+_root.dir[2*d][0]][y+_root.dir[2*d][1]])
        if(_root.map[x+_root.dir[d][0]][y+_root.dir[d][1]]==0){
            return "space"
            //trace(_root.map[x+_root.dir[2*d][0]][y+_root.dir[2*d][1]])
            }
            if(_root.map[x+_root.dir[d][0]][y+_root.dir[d][1]]==2 && _root.map[x+2*_root.dir[d][0]][y+2*_root.dir[d][1]]==0){
                //trace("sdfsdf")
            //_root.map[x+_root.dir[d][0]][y+_root.dir[d][1]]=2
            //_root.map[x+2*Number(_root.dir[d][0])][y+2*Number(_root.dir[d][1])]==2
            //_root.map[box._x/20][box._y/20]=2
            //trace(_root.map[x+_root.dir[2*d][0]][y+_root.dir[2*d][1]])
            //trace("adfafdasd")
            return "box"
            }
            
            if(_root.map[x+_root.dir[d][0]][y+_root.dir[d][1]]==2 and _root.map[x+2*Number(_root.dir[d][0])][y+2*Number(_root.dir[d][1])]==1){
                return false
                //trace(_root.map[x+_root.dir[2*d][0]][y+_root.dir[2*d][1]])
                }
        }
    }
    public function init() {
        var mapheight=10;
        var mapwidth=10
        _root.createEmptyMovieClip("map",2)
        _root.createEmptyMovieClip("human",3)
        _root.createEmptyMovieClip("box",4)
        _root.createEmptyMovieClip("mini",5)
        _root["mini"]._x=8*20
        _root["mini"]._y=8*20
        _root["human"]._x=1*20
        _root["human"]._y=1*20
        _root["box"]._x=3*20
        _root["box"]._y=4*20
        //trace(_root["human"]._x)
        creatBox(_root["mini"],0,0,0xff0000)
        creatBox(_root["human"],0,0,0x00ff00)
        creatBox(_root["box"],0,0,0x000000)
        
        //trace(typeof _root["human"])
        
        _root.map = [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
                [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
                [1, 1, 1, 1, 0, 0, 0, 0, 0, 1],
                [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
                [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
                [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
                [1, 0, 0, 0, 0, 0, 0, 0, 1, 1],
                [1, 0, 0, 0, 0, 1, 0, 0, 0, 1],
                [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]];
        _root.map[3][4]=2
        for(var i =0;i<mapheight;i++){
            for(var j=0;j<mapwidth;j++){
                if(_root.map[i][j]==1){
                    creatBox(_root["map"],i,j,0xcccccc)
                    }
                }
            }
    }
    public function creatBox(obj,X,Y,Colo) {
    var Xs=X*20
    var Ys=Y*20;
    with(obj){
        lineStyle(1,0x000000,100)
        beginFill(Colo,100);
        moveTo(Xs,Ys)
        lineTo(Xs+20,Ys)
        lineTo(Xs+20,Ys+20)
        lineTo(Xs,Ys+20)
        lineTo(Xs,Ys)
        endFill();
        }
    }
}
标签集:TAGS:
回复Comments() 点击Count()

回复Comments

{commentauthor}
{commentauthor}
{commenttime}
{commentnum}
{commentcontent}
作者:
{commentrecontent}