Teddy

没事乱写的地方
posts - 9, comments - 15, trackbacks - 0, articles - 0
  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理

2011年11月16日

以保存对javascript的些许理解

/****************/
/*****game.js****/
/****************/
Array.prototype.remove = function (obj) {
    for (var i = 0, l = this.length; i < l; i++) {

        if (this[i] == obj) {
            this.splice(i, 1);
            break;
        }
    }
}

var Game = {
    //循环ID
    timeId: -1,
    //context2d对象
    gamebg: null,
    //游戏背景信息
    gameinfo: {},
    //开始按钮
    btn: null,
    //初始化
    snake: null,
    food: null,
    init: function () {
        //获取游戏背景dom
        var game_dom = document.getElementById('gamebg'),
        This = this;
        //设置游戏背景信息
        this.gameinfo = {
            width: game_dom.clientWidth,
            height: game_dom.clientHeight,
            left: game_dom.offsetLeft,
            top: game_dom.offsetTop
        };
        //获取context2d对象
        this.gamebg = game_dom.getContext('2d');
        //绑定键盘按下事件
        document.onkeydown = function (e) { This.keyDown(e); };
        //创建小球
        this.snake = new Snake(7, this.gameinfo.width, this.gameinfo.height);
        this.food = new Food(100, 100, 4, 'red');
        //this.createFood();
    },

    keyDown: function (e) {
        var d = this.snake.direction;
        switch (e.keyCode) {
            case 37:
                d = 9;
                break;
            case 38:
                d = 12;
                break;
            case 39:
                d = 3;
                break;
            case 40:
                d = 6;
                break;
            case 32:
                this.pause(document.getElementById('Button2'));
                break;
        }
        if (Math.abs(this.snake.direction - d) != 6) {
            this.snake.oldDirection = this.snake.direction;
            this.snake.direction = d;
        }
        else {
            this.snake.back = 1;
        }
    },

    //btn:开始按钮dom
    start: function (btn) {

        if (this.btn) this.reset();

        this.btn = btn;
        this.btn.disabled = 'disabled';
        var This = this;
        this.init();
        //开始画画
        this.timeId = setInterval(function () {
            This.process();
        }, 80);
    },

    process: function () {
        //清除画面
        this.gamebg.clearRect(0, 0, this.gameinfo.width, this.gameinfo.height);
        this.snake.update(this.food);
        var body = this.snake.Body;
        Canvas.arc(this.gamebg, this.food.X, this.food.Y, this.food.Radius, this.food.Color);
        for (var i = 0; i < body.length; i++) {
            Canvas.arc(this.gamebg, body[i].X, body[i].Y, body[i].Radius, body[i].Color);
        }

        //判断游戏是否结束
        if (this.snake.Body.length == 0) {
            clearInterval(this.timeId);
            var score = document.getElementById("score");
            alert('您的分数是:' + score.innerHTML);
            this.btn.disabled = '';
        }
    },

    //重置游戏数据
    reset: function () {
        this.food = null;
        this.snake = null;
        this.timeId = -1;
        this.gamebg = null;
        this.gameinfo = {};
        var score = document.getElementById("score");
        score.innerHTML = 0;
    },

    pause: function (btn) {
        if (btn.value == 'Pause') {
            clearInterval(this.timeId);
            btn.value = 'Run';
        }
        else if (btn.value == 'Run') {
            btn.value = 'Pause';
            var This = this;
            this.timeId = setInterval(function () {
                This.process();
            }, 80);
        }
    }

}

 

/****************/
/*****food.js****/
/****************/
var Food = function (x, y, radius, color) {
    this.X = x;
    this.Y = y;
    this.Radius = radius;
    this.Color = color;
    this.fpt = 10000;
    this.lastUpdata = 0;
}

Food.prototype =
{
    update: function () {
        this.X = parseInt(Math.random() * 380 + 8, 10);
        this.Y = parseInt(Math.random() * 380 + 8, 10);
    }

}

 

 

/****************/
/*****Canvas.js****/
/****************/
var Canvas = {
    //画圆
    //ctx:context2d对象,x:圆心x坐标,y:圆心y坐标,radius:半径,color:颜色
    arc: function (cxt, x, y, radius, color) {
        cxt.fillStyle = color;
        cxt.beginPath();
        cxt.arc(x, y, radius, 0, Math.PI * 2, true);
        cxt.closePath();
        cxt.fill();
    }

}

 

 

/****************/
/*****Bone.js****/
/****************/
var Bone = function (x, y, radius, color) {
    this.X = x;
    this.Y = y;
    this.Radius = radius;
    this.Color = color;

}

 

/****************/
/*****Snake.js****/
/****************/
var Snake = function (length, width, height) {
    this.fpt = 40;
    this.Body = [];
    this.lastUpdata = 0;
    this.speed = 8;
    this.oldDirection = 6;
    this.direction = 6;
    var centerX = parseInt(width / 2, 10);
    var centerY = parseInt(height / 2, 10);

    var bone = new Bone(centerX, centerY, 4, 'blue');
    this.Body.push(bone);

    for (var i = 0; i < length - 1; i++) {
        var bone = new Bone(centerX, centerY - (8 * (i + 1)), 4, 'blue');
        this.Body.push(bone);
    }

    this.cornerX = centerX;
    this.cornerY = centerY;
    this.back = 0;
}

Snake.prototype =
{
    update: function (food) {
        if (this.lastUpdata % this.fpt == 0) {

            if (this.back == 1) {
                this.back = 0;
                var last = this.Body[this.Body.length - 1];
                var secondLast = this.Body[this.Body.length - 2];

                if (last.Y - secondLast.Y > 0) {
                    this.direction = 6;
                }
                else if (last.Y - secondLast.Y < 0) {
                    this.direction = 12;
                }
                else if (last.X - secondLast.X < 0) {
                    this.direction = 9;
                }
                else if (last.X - secondLast.X > 0) {
                    this.direction = 3;
                }
                this.Body.reverse();
            }


            //记录蛇头的原始坐标
            this.cornerX = this.Body[0].X;
            this.cornerY = this.Body[0].Y;

            //定义蛇头的新位置
            switch (this.direction) {
                case 12:
                    this.Body[0].Y -= this.speed;
                    break;
                case 6:
                    this.Body[0].Y += this.speed;
                    break;
                case 3:
                    this.Body[0].X += this.speed;
                    break;
                case 9:
                    this.Body[0].X -= this.speed;
                    break;
                default:
                    break;
            }
            //蛇身的新位置为前一个节点的位置
            for (var i = this.Body.length - 1; i > 1; i--) {
                this.Body[i].X = this.Body[i - 1].X;
                this.Body[i].Y = this.Body[i - 1].Y;
            }

            //定义蛇尾的新位置
            this.Body[1].X = this.cornerX;
            this.Body[1].Y = this.cornerY;

            this.eatFood(food);
            var over = false;
            for (var j = 0; j < this.Body.length; j++) {
                var bone = this.Body[j];
                if (bone.X < 0 || bone.Y < 0 || bone.X > 400 || bone.Y > 400) {
                    over = true;
                    break;
                }
            }

            if (over) {
                this.Body.length = 0;
            }
        }

        //时间累加
        this.lastUpdata += 10;
    },

    eatFood: function (food) {
        var i, l, bone, isTouch = false;
        //遍历蛇身体
        for (i = 0, l = this.Body.length; i < l; i++) {
            var bone = this.Body[i];
            //判断蛇身与食物的圆心距
            if (Math.sqrt(Math.pow(bone.X - food.X, 2) + Math.pow(bone.Y - food.Y, 2)) <= bone.Radius + food.Radius) {
                isTouch = true;
                break;
            }
        }

        //给蛇增加一节
        if (isTouch) {
            var head = this.Body[0];
            var x = head.X;
            var y = head.Y;
            switch (this.direction) {
                case 12:
                    y = y - head.Radius * 2;
                    break;
                case 3:
                    x = x + head.Radius * 2;
                    break;
                case 6:
                    y = y + head.Radius * 2;
                    break;
                case 9:
                    x = x - head.Radius * 2;
                    break;
            }
            var newBone = new Bone(x, y, head.Radius, head.Color);
            this.Body.push(newBone);

            //随机改变食物的位置
            food.X = -100;
            food.Y = -100;
            setTimeout(function () {
                food.update();
            }, 800);
            var score = document.getElementById("score");
            score.innerHTML = parseInt(score.innerHTML) + 1;
            if (this.fpt > 10) {
                this.fpt -= 10;
            }
        }
    }

} 

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="game.js" type="text/javascript"></script>
    <script src="Bone.js" type="text/javascript"></script>
    <script src="Canvas.js" type="text/javascript"></script>
    <script src="Snake.js" type="text/javascript"></script>
    <script src="food.js" type="text/javascript"></script>
</head>
<body>
    <canvas id="gamebg" width="400" height="400" style="background: Black;">您的浏览器不支持html5,请使用chrome或者ff</canvas>
    <input id="Button1" type="button" value="Start" onclick="Game.start(this)" />
    <input id="Button2" type="button" value="Pause" onclick="Game.pause(this)" />
    <br />
<div id="score">0</div>
</body>

</html> 

 

 

posted @ 2011-11-16 16:58 Teddy 阅读(140) 评论(0) 编辑

2011年6月14日

一:在QQ、TT浏览器中,uploadify上传控件不能正常工作,显示出了上传进度条,但是进度不走。解决办法如下,在前台初始化uploadify控件时,‘uploader’属性需要加随机数,让uploadify.swf文件重新下载到IE临时文件夹。在js中我加入了时间戳:'uploader''../js/jquery.uploadify-v2.1.4/uploadify.swf?t=' + new Date().getTime()

 

二:解决在FireFox中不工作,需要做如下4步修改:

1、Global.asax文件中,实现Application_BeginRequest函数:

 

void Application_BeginRequest(object sender, EventArgs e) 
    {
        
try
        {
            
string session_param_name = "ASPSESSID";
            
string session_cookie_name = "ASP.NET_SessionId"
            
if (HttpContext.Current.Request.Form[session_param_name] != null)
            {
                updatecookie(session_cookie_name, HttpContext.Current.Request.Form[session_param_name]);
            }
            
else if (HttpContext.Current.Request.QueryString[session_param_name] != null)
            {
                updatecookie(session_cookie_name, HttpContext.Current.Request.QueryString[session_param_name]);
            }
        }
        
catch
        {
        }

        
//此处是身份验证
        try
        {
            
string auth_param_name = "AUTHID"
            
string auth_cookie_name = FormsAuthentication.FormsCookieName;
            
if (HttpContext.Current.Request.Form[auth_param_name] != null)
            {
                updatecookie(auth_cookie_name, HttpContext.Current.Request.Form[auth_param_name]);
            }
            
else if (HttpContext.Current.Request.QueryString[auth_param_name] != null)
            {
                updatecookie(auth_cookie_name, HttpContext.Current.Request.QueryString[auth_param_name]);
            }
        }
        
catch { }

 } 

    

 

2、前台js修改,注意红色代码:

 1 <script type="text/javascript">
 2         function pageLoad() {
 3             var auth = $("#<%=hfAuth.ClientID %>").val();
 4             var AspSessID = $("#<%=hfAspSessID.ClientID %>").val();
 5             $("#uploadify").uploadify({
 6                 'uploader''../js/jquery.uploadify-v2.1.4/uploadify.swf?t=' + new Date().getTime(),
 7                 'script''../JQueryUploadHandler.ashx?type=logo',
 8                 'cancelImg''../js/jquery.uploadify-v2.1.4/cancel.png',
 9                 'folder''../images/UploadFile',
10                 'queueID''fileQueueImg',
11                 'auto'true,
12                 'multi'false,
13                 'height'22,
14                 'buttonImg''../images/file_select.png',
15                 'fileDesc''图片文件',
16                 'fileExt''*.gif;*.jpg;*.jpeg;*.png',
17                 'removeCompleted'true,
18                 'simUploadLimit''1',
19                 scriptData: { ASPSESSID: AspSessID, AUTHID: auth },
20                 onComplete: function(event, queueID, fileObj, response, data) {
21                 alert("文件:" + fileObj.name + " 上传成功");
22                 },
23                 onError: function(event, queueID, fileObj) {
24                     alert("文件:" + fileObj.name + " 上传失败");
25                 }
26             });
27            
28         }

29 </script>  


 

3、前台页面加入两个隐藏控件:

1 <asp:HiddenField ID="hfAuth" runat="server" />

2 <asp:HiddenField ID="hfAspSessID" runat="server" /> 


 

4、后台pageload函数给两个隐藏控件赋值 :

1 if (!IsPostBack)
2         {
3             this.hfAuth.Value = Request.Cookies[FormsAuthentication.FormsCookieName] == null ?
4                 string.Empty : Request.Cookies[FormsAuthentication.FormsCookieName].Value;
5             this.hfAspSessID.Value = Session.SessionID;

 

 三:解决uploadify控件在updatepanel中不工作,需要每次updatepanel更新时,重新初始化uploadify控件,因此将初始化代码写到function pageLoad()的js函数中,并且后台pageload中加入如下代码,该行代码不能放入if (!IsPostBack)条件判定内部。

 

protected void Page_Load(object sender, EventArgs e)
{
        
this.Page.RegisterClientScriptBlock("uploadStyle""<script>pageLoad()</script>");

 

 

 

posted @ 2011-06-14 09:58 Teddy 阅读(320) 评论(0) 编辑

2008年3月7日

摘要: 继续SAS宏功能(上),介绍一些常用的SAS宏函数、宏变量,及其宏程序写法。阅读全文

posted @ 2008-03-07 16:21 Teddy 阅读(3130) 评论(0) 编辑

2008年3月6日

 SAS宏功能(上)

概念

将一个变量,一段程序或者一个文本命名,供以后调用

SAS宏主要功能:

获取SAS系统信息

SAS在启动时就创建了一些自动宏变量,用以存储当前SAS进程启动的日期,时间,版本号及其它信息,用户可以在任何情况下使用这些宏变量。

条件执行过程步和数据步

例如每天提交一份生产情况的详细报告,每周五增加一份汇总报告。使用宏功能每天运行同一个程序就可以实现上述任务

开发交互式系统

使用SAS宏语言的%WINDOW语句及一些基本的编程语句可开发交互式用户界面。

产生与数据无关的SAS程序,但可展示与数据有关的结果

宏功能可保持SAS程序的独立性和移植性。一段程序在多种情况下均可运行,得到期望的结果。

在不同的SAS数据步或过程步之间传递数据

SAS宏变量可在SAS的任何地方被引用,具有全局性,所以成为不同过程间传递数据最方便的手段。

重复执行SAS程序

例:打印20个不同但有规律的SAS数据集中的内容。

%MACRO PRINT;

                   %DO NN=70 % TO 89;

                   Proc print data=year&NN;

                            Title “19&NN Sales Data”;

                   Run;

                   %END;

         %MEND PRINT;

%PRINT;

凡用到SAS宏语言的变量和语言成分,都以符号%&开始。当SAS程序提交后,在编译阶段,系统对程序逐词扫描过程中,凡遇到%&开始的词,就启动宏语言处理器对此进行处理:

对于宏变量或宏程序的定义就进行登记

对于宏变量的调用就进行解读(Resolve),对宏程序的调用则读入相应的源程序进行编译

对其它宏语句编译成相应的程序。

%INCLUDE语句

全局语句,可出现在程序任意位置。读入存放在外部文件中的一段文本作为程序并按程序执行。用法: %INCLUDE 文件设定</SOURCE2>

例:标签了dst(‘abc.sas’)的文件中存放的一段SAS程序: %include dst(‘abc.sas’);

(未完待续)

posted @ 2008-03-06 15:47 Teddy 阅读(2216) 评论(0) 编辑

摘要: SAS与数据库系统的通信程序例程. 将工作中的学习心得记录在此,与各位朋友交流阅读全文

posted @ 2008-03-06 15:40 Teddy 阅读(806) 评论(2) 编辑

2006年10月23日

摘要: 在ASP.NET1.1中执行应用程序往往不能得到期望的结果,甚至可以看到应用程序存在于进程中,而程序并没有报错。这是因为微软为安全考虑禁掉了这一功能(ASP.NET2.0却不存在此问题),本文通过实例向大家介绍怎样解决这一问题。阅读全文

posted @ 2006-10-23 21:16 Teddy 阅读(1985) 评论(6) 编辑

2006年10月20日

摘要: Windows Workflow Foundation 主要由 .NET 驱动的运行库环境组成,该环境处理在 Visual Studio 设计器中设计和实现的特殊对象。Microsoft .NET Framework 2.0 是支持 Windows Workflow Foundation 所必需的。单独的安装程序包为 Visual Studio 2005 添加了 Windows Workflow Foundation 设计器和项目模板支持。一旦安装,就会向 Visual Studio 2005 中的标准项目列表中添加一个全新的节点阅读全文

posted @ 2006-10-20 15:29 Teddy 阅读(1628) 评论(2) 编辑

2006年10月19日

摘要: 从ASP.NET中可以将数据导出成各种形式的文档,本文以导出成EXCEL格式为例,讲述了导出的方法和要点,已经导出中文格式需要注意的事项。阅读全文

posted @ 2006-10-19 20:41 Teddy 阅读(540) 评论(1) 编辑

2006年10月18日

摘要: C#语言可以用自带的类NameValueCollection来访问存在配置文件中的信息,给编程带来了极大的方便;而且可以运用这一特性增强应用程序的兼容性。阅读全文

posted @ 2006-10-18 20:22 Teddy 阅读(1780) 评论(4) 编辑