主题:  骰子的动态拖拽

diy980013

职务:普通成员
等级:1
金币:0.0
发贴:100
#12004/6/28 17:40:24
六个骰子,鼠标在任意一个骰子上按下鼠标,只要是在响应鼠标动作的骰子上方存在的骰子都要与该骰子一起被拖拽,并根据拖拽到达目的地的骰子个数重新确定被拖拽骰子的坐标。自定义了两个函数,在骰子响应鼠标动作时候分调用函数。但是测试时发现符合条件的骰子不能一起被拖拽,怎么也找不到原因?着急,麻大家帮忙看看到底是什么地方出了问题?谢谢!

源代码:

//初始化每列的骰子个数total1、total2、total3
//初始化H;倒计时控制变量startflag;
//建立数组originalx及originaly存储骰子初始位置坐标;
_root.onLoad = function() {
    _root.total1 = 2;
    _root.total2 = 2;
    _root.total3 = 2;
    _root.dice0._x = _root.dice1._x;
    H = _root.dice0._height;
    _root.startflag = 0;
    originalx_dice = new Array();
    originaly_dice = new Array();
    for (var i = 0; i<6; ++i) {
        originalx_dice[i] = _root["dice" add i]._x;
        originaly_dice[i] = _root["dice" add i]._y;
        _root["dice" add i].useHandCursor = false;
    }
};
//自定义函数randomsum()随机选取大于0小于21的三个数字a、b、c,赋值给文本框变量sun1、sum2、sum3
//a、b、c之和为21,a、b、c之间的任何组合都不能是(1,1,19)或(2,2,17)
function randomsum() {
    _root.startflag = 1;
    _root.ttime = Math.round(getTimer()/1000);
    a = random(21);
    b = random(21);
    c = 21-a-b;
    f = !((a == b == 1 && c == 19) || (a == c == 1 && b == 19) || (a == 19 && b == c == 1) || (a == b == 2 && c == 17) || (a == c == 2 && b == 17) || (a == 17 && b == c == 2));
    g = (a>0 && b>0 && c>0);
    if (f && g) {
        _root.sum1 = a;
        _root.sum2 = b;
        _root.sum3 = c;
    } else {
        randomsum();
    }
}
//自定义dice响应按下鼠标的函数Press(dice_name)
function Press(dice_name) {
    dice_name.startDrag();
    tempx_dice = dice_name._x;
    tempy_dice = dice_name._y;
    Array_temp = new Array();
    Ayyay_tempxlist = new Array();
    Ayyay_tempylist = new Array();
    for (var m = 0; m<6; m++) {
        if (_root["dice" add m]._x == dice_name._x && _root["dice" add m]._y<dice_name._y) {
            Array_temp.push(m);
            Ayyay_tempxlist.push(_root["dice" add m]._x);
            Ayyay_tempylist.push(_root["dice" add m]._y);
        }
    }
    if (dice_name._x == originalx_dice[0]) {
        _root.total1 = _root.total1-(Array_temp.length+1);
        if (_root.total1<=0) {
            _root.total1 = 0;
        }
    } else if (dice_name._x == originalx_dice[2]) {
        _root.total2 = _root.total2-(Array_temp.length+1);
        if (_root.total2<=0) {
            _root.total2 = 0;
        }
    } else if (dice_name._x == originalx_dice[4]) {
        _root.total3 = _root.total3-(Array_temp.length+1);
        if (_root.total3<=0) {
            _root.total3 = 0;
        }
    }
    _root.onMouseMove = function() {
        if (Array_temp.length>0) {
            ok = true;
        } else {
            ok = false;
        }
        if (ok) {
            for (k=0; k<Array_temp.length; k++) {
                eval("_root.dice" add Array_temp[k])._x = Array_tempxlist[k]+(dice_name._x-tempx_dice);
                eval("_root.dice" add Array_temp[k])._y = Array_tempylist[k]+(dice_name._y-tempy_dice);
            }
        }
    };
    _root.onMouseUp = function() {
        ok = false;
        delete dice_name;
    };
}
//自定义dice响应按下鼠标的函数Release(dice_name)
function Release(dice_name) {
    dice_name.stopDrag();
    if (Math.abs(dice_name._x-originalx_dice[0])<50 && Math.abs(dice_name._x-originalx_dice[0])>0) {
        dice_name._x = originalx_dice[0];
        dice_name._y = originaly_dice[1]-_root.total1*H;
        for (k=0; k<Array_temp.length; k++) {
            _root["_root.dice" add Array_temp[k]]._x = Array_tempxlist[k]+(dice_name._x-tempx_dice);
            _root["_root.dice" add Array_temp[k]]._y = Array_tempylist[k]+(dice_name._y-tempy_dice);
        }
        _root.total1 = _root.total1+Array_temp.length+1;
    } else if (Math.abs(dice_name._x-originalx_dice[2])<50 && Math.abs(dice_name._x-originalx_dice[2])>0) {
        dice_name._x = originalx_dice[2];
        dice_name._y = originaly_dice[3]-_root.total2*H;
        for (k=0; k<Array_temp.length; k++) {
            _root["_root.dice" add Array_temp[k]]._x = Array_tempxlist[k]+dice_name._x-tempx_dice;
            _root["_root.dice" add Array_temp[k]]._y = Array_tempylist[k]+dice_name._y-tempy_dice;
        }
        _root.total2 = _root.total2+Array_temp.length+1;
    } else if (Math.abs(dice_name._x-originalx_dice[4])<50 && Math.abs(dice_name._x-originalx_dice[4])>0) {
        dice_name._x = originalx_dice[4];
        dice_name._y = originaly_dice[5]-_root.total3*H;
        for (k=0; k<Array_temp.length; k++) {
            _root["_root.dice" add Array_temp[k]]._x = Array_tempxlist[k]+dice_name._x-tempx_dice;
            _root["_root.dice" add Array_temp[k]]._y = Array_tempylist[k]+dice_name._y-tempy_dice;
        }
        _root.total3 = _root.total3+Array_temp.length+1;
    } else {
        dice_name._x = tempx_dice;
        dice_name._y = tempy_dice;
        for (k=0; k<Array_temp.length; k++) {
            _root["_root.dice" add Array_temp[k]]._x = Array_tempxlist[k];
            _root["_root.dice" add Array_temp[k]]._y = Array_tempylist[k];
        }
        if (dice_name._x == originalx_dice[0]) {
            _root.total1 = _root.total1+(Array_temp.length+1);
            if (_root.total1<=0) {
                _root.total1 = 0;
            }
        } else if (dice_name._x == originalx_dice[2]) {
            _root.total2 = _root.total2+(Array_temp.length+1);
            if (_root.total2<=0) {
                _root.total2 = 0;
            }
        } else if (dice_name._x == originalx_dice[4]) {
            _root.total3 = _root.total3+(Array_temp.length+1);
            if (_root.total3<=0) {
                _root.total3 = 0;
            }
        }
        delete dice_name;
    }
}
//游戏开始,调用自定义函数randomsum随机取得sum1、sun2、sum3
_root.start.onRelease = function() {
    randomsum();
};
_root.onEnterFrame = function() {
    //控制游戏时间为60秒倒计时,动态显示得分
    if (_root.startflag == 1) {
        _root.time_old = Math.round(getTimer()/1000);
        _root.time_new = _root.time_old-_root.ttime;
        _root.count = 60-_root.time_new;
        if (_root.count<=0) {
            _root.count = 60;
            _root.startflag = 0;
            _root.start._visible = true;
        }
    }
};
//dice0响应鼠标动作调用函数
_root.dice0.onPress = function() {
    dice_name = _root.dice0;
    trace(dice_name);
    Press(dice_name);
};
_root.dice0.onRelease = function() {
    dice_name = _root.dice0;
    trace(dice_name);
    Release(dice_name);
};
//dice1响应鼠标动作调用函数
_root.dice1.onPress = function() {
    dice_name = _root.dice1;
    trace(dice_name);
    Press(dice_name);
};
_root.dice1.onRelease = function() {
    dice_name = _root.dice1;
    trace(dice_name);
    Release(dice_name);
};
//dice2响应鼠标动作调用函数
_root.dice2.onPress = function() {
    dice_name = _root.dice2;
    trace(dice_name);
    Press(dice_name);
};
_root.dice2.onRelease = function() {
    dice_name = _root.dice2;
    trace(dice_name);
    Release(dice_name);
};
//dice1响应鼠标动作调用函数
_root.dice3.onPress = function() {
    dice_name = _root.dice3;
    trace(dice_name);
    Press(dice_name);
};
_root.dice3.onRelease = function() {
    dice_name = _root.dice3;
    trace(dice_name);
    Release(dice_name);
};
//dice1响应鼠标动作调用函数
_root.dice4.onPress = function() {
    dice_name = _root.dice4;
    trace(dice_name);
    Press(dice_name);
};
_root.dice4.onRelease = function() {
    dice_name = _root.dice4;
    trace(dice_name);
    Release(dice_name);
};
//dice1响应鼠标动作调用函数
_root.dice5.onPress = function() {
    dice_name = _root.dice5;
    trace(dice_name);
    Press(dice_name);
};
_root.dice5.onRelease = function() {
    dice_name = _root.dice5;
    trace(dice_name);
    Release(dice_name);
};

Flash 动画



浮尘

职务:普通成员
等级:3
金币:7.0
发贴:1258
#22004/6/28 22:33:11
这是做什么用的呀,怎么我拽拽的就剩下一个了呀?



diy980013

职务:普通成员
等级:1
金币:0.0
发贴:100
#32004/6/29 9:02:25
就是因为不知道为什么最后居然只是拖拽成了一个 所以才上来求教大家呢 谢谢



diy980013

职务:普通成员
等级:1
金币:0.0
发贴:100
#42004/6/29 9:13:19
我的思路是:只要在同一列的骰子,X坐标相同,并且小于于鼠标所按骰子的Y坐标,都应该被一起拖动,按下时新建数组存储符合条件的骰子及各自的坐标值及计算各列的骰子个数;并且释放鼠标时判断释放的位置,然后根据释放位置的骰子个数计算被拖动的骰子的坐标值。现在发现调用自定义函数却不能产生预期的效果,很是郁闷!请大家帮忙看看,谢谢。



UndeadCraft

职务:版主
等级:4
金币:10.0
发贴:1993
#52004/6/30 9:17:57
不能用==来判断,应该是一个范围。你按下的时候坐标肯定会不相等的