道法自然

率性、自我、随心...
posts - 61, comments - 82, trackbacks - 0, articles - 2
  教师博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

食物链的AS代码

Posted on 2008-07-20 14:29 周泉 阅读(166) 评论(0)  编辑 收藏 引用 网摘 所属分类: 技术文章
要做一个食物链的Flash。其实也很简单的,几种生物,拖动组成食物链,点“提交”按钮后判断对错。但AS不熟,经常几天思考及请教,终于写成。特放上Blog记录一下。

一、想实现的功能:
用鼠标拖动生物到各个框中,组成食物链,点“提交”进行判断对错。点“重做”做初始化重做。
二、我的想法:
拖物件,松开鼠标后判断是否碰到四个热区。如果否,物件返回原位;如果是,再判断热区是否已经有物体放进去了。如果有,物件返回原位;如果没有,放下。对两个函数进行赋值(1个用于判断热区是否已经有物件,另一个用于判断食物链是否正确)。

//=============================生物代码开始===============================================
//鼠:
on (rollOver) {
    this.gotoAndStop(3);
}
on (rollOut) {
    this.gotoAndStop(1);
}
on (press) {
    this.gotoAndStop(1);
    if (_root.w4 == 0) {
    startDrag(this, true);}
//w4用与检验物体是否已经放到框内。如果没有就可以拖动,如果已经放在框内就不能拖动。但做出来后,放在框内的物体双击一下,就自己跑回原位了,不知道是哪里出错。
}
on (release) {
    this.gotoAndStop(1);
    stopDrag();
    if (this.hitTest(_root.hittest1)) {
        switch (_root.in1) {
        case 0 :
            _x = 100;
            _y = 452;
            mySound = new Sound();
            mySound.attachSound("成功", true);
            mySound.start();
            _root.A = 3;  //A是表示第一个框,B/C/D类推。赋值表示生物的级别,用于检验食物链是否排错
            _root.in1 = 1;   //in1函数用于判断第一个框是否有物体,有则赋值1。使其它生物不能再放入。in2/in3/in4如此类推。
            _root.w4 = 1;
            break;
        case 1 :
            mySound = new Sound();
            mySound.attachSound("失败", true);
            mySound.start();
            _x = 165;
            _y = 185;
            break;
        }
    } else if (this.hitTest(_root.hittest2)) {
        switch (_root.in2) {
        case 0 :
            _root.B = 3;
            _root.in2 = 1;
            _root.w4 = 1;
            mySound = new Sound();
            mySound.attachSound("成功", true);
            mySound.start();
            _x = 280;
            _y = 452;
            break;
        case 1 :
            mySound = new Sound();
            mySound.attachSound("失败", true);
            mySound.start();
            _x = 165;
            _y = 185;
            break;
        }
    } else if (this.hitTest(_root.hittest3)) {
        switch (_root.in3) {
        case 0 :
            _root.C = 3;
            _root.in3 = 1;
            _root.w4 = 1;
            mySound = new Sound();
            mySound.attachSound("成功", true);
            mySound.start();
            _x = 455;
            _y = 452;
            break;
        case 1 :
            mySound = new Sound();
            mySound.attachSound("失败", true);
            mySound.start();
            _x = 165;
            _y = 185;
            break;
        }
    } else if (this.hitTest(_root.hittest4)) {
        switch (_root.in4) {
        case 0 :
            _root.D = 3;
            _root.in4 = 1;
            _root.w4 = 1;
            mySound = new Sound();
            mySound.attachSound("成功", true);
            mySound.start();
            _x = 630;
            _y = 452;
            break;
        case 1 :
            mySound = new Sound();
            mySound.attachSound("失败", true);
            mySound.start();
            _x = 165;
            _y = 185;
            break;
        }
    } else {
    mySound = new Sound();
    mySound.attachSound("失败", true);
    mySound.start();
    _x = 165;
    _y = 185;}
}
//====================生物代码结束====================================



//==========另,提交按钮的代码:========================
on (release) {
    if (A == 1 && B == 2 && D>C && C>B) {
        gotoAndStop(22);
        //棒极了
    } else {
        gotoAndStop(21);
        //出错了
    }
}
//==========提交按钮的代码结束========================



最终写成这样,问题解决!





只有注册用户登录后才能发表评论。