click小游戏demo

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <style type="text/css">
            *{ margin: 0; padding: 0;}
            #box{
                width: 1100px;
                height: 600px;
                border: 1px solid firebrick;
                margin: 30px auto;
                position: relative;
                overflow: hidden;
            }
            #box span{
                border-radius: 50%;
                position: absolute;
                display: block;
                cursor: pointer;
            }
            #scord{
                text-align: center;
                font-size: 28px;
                color: firebrick;
            }
            #btn{
                position: absolute;
                left: 50%;
                width: 140px;
                text-align: center;
                border: 1px solid firebrick;
                color: firebrick;
                line-height: 60px;
                top: 300px;
                margin-left: -70px;
                cursor: pointer;
                transition: 0.3s;
            }
        </style>
        <script type="text/javascript">
            window.onload = function(){
                var oBox = document.querySelector("#box");
                var oSord = document.querySelector("#scord");
                var oBtn = document.querySelector("#btn");
                var aDiv = oBox.getElementsByTagName("span");
                var num = 1;
                var scord = 0;
                var ON = false;
                var cArr = [];
                creatData();
                bind();
                //开始点击
                oBtn.onclick = function(){
                    this.style.transform = 'scale(0)';
                    if(ON){
                        num++;
                        cArr = [];
                        creatData();
                        ceatNode();
                    }else{
                        ceatNode();
                    }
                }
                //创建随机位置
                function creatData(){
                    for(var i = 0; i < num; i++){
                        var l = Math.floor(Math.random()*900)+100;
                        var t = -(Math.floor(Math.random()*20)+100);
                        var w = Math.floor(Math.random()*20)+50;
                        var c1 = Math.floor(Math.random()*255);
                        var c2 = Math.floor(Math.random()*255);
                        var c3 = Math.floor(Math.random()*255);
                        var speed = Math.floor(Math.random()*3)+1;
                        cArr.push({
                            l : l,
                            t : t,
                            w : w,
                            c1 : c1,
                            c2 : c2,
                            c3 : c3,
                            speed : speed
                        })
                    }
                }
                //创建节点
                function ceatNode(){
                    for(var i = 0; i < cArr.length; i++){
                        var oDiv = document.createElement("span");
                        oDiv.style.width = cArr[i].w+'px';
                        oDiv.style.height = cArr[i].w+'px';
                        oDiv.style.background = 'rgba('+cArr[i].c1+','+cArr[i].c2+','+cArr[i].c3+',1)';
                        oDiv.style.left = cArr[i].l+'px';
                        oDiv.style.top = cArr[i].t+'px';
                        oBox.appendChild(oDiv);
                        move(aDiv[i], cArr[i].speed, i)
                    }
                }
                //掉下运动
                function move(obj, s, Index){
                    clearInterval(obj.timer);
                    obj.timer = setInterval(function(){
                        obj.style.top = obj.offsetTop + s + 'px';
                        if(obj.offsetTop>600){
                            oBox.removeChild(obj);
                        }
                    },30);
                }
                //事件绑定
                function bind(){
                    oBox.onclick = function(e){
                        var e = e || window.event;  
                        var Target = e.srcElement || e.target;
                        if( Target.tagName.toUpperCase() == "SPAN" ){
                            Target.style.transition = "0.15s";
                            Target.style.transform = 'scale(2)';
                            var t = Target;
                            Target.addEventListener('transitionend', function(ev){
                                var ev = ev || window.event;
                                oBox.removeChild(t);
                                scord++;
                                oSord.innerHTML = '分数:'+scord;
                                if(oBox.children.length==0){
                                    oBtn.style.transform = 'scale(1)';
                                    oBtn.innerHTML = "下一关"+" "+'('+(num+1)+')';
                                    ON = true;
                                }
                                ev.stopPropagation();
                            })
                        }; 
                    }
                }
            }
        </script>
    </head>
    <body>
        <div id="box">

        </div>
        <div id="btn">开始</div>
        <div id="scord">分数:0</div>
    </body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
http://blog.csdn.net/xiaoxiao108/article/details/8913616 html5 挺火,写个html5游戏玩玩吧,想起cocos2d 貌似各个平台都有,网上找了找,下载了个Cocos2d-html5引擎包。 貌似另一个开源引擎lufylegend.js也很好,下次用用lufylegend.js试试。 开发环境 chrome Cocos2d-html5 游戏地址:http://hehe108.sinaapp.com/cocos2d/ (adsw回车) 实现方法如下 1.创建好 LayerGradient的子类 (里面放坦克子弹) 2.重写 onEnter 方法添加一些基本按钮 跟一些初始坦克,子弹 3.通过schedule方法 控制 坦克 子弹的重画 4.根据键盘按键(ASWD)确定出坦克的方向,根据坦克的方向修改坦克的X,Y轴坐标,来实现坦克的移动 5.通过cc.rectIntersectsRect函数来进行碰撞检测,实现子弹打击坦克 具体代码 1.在项目里面添加方向 var Direction = { L:0, U:1, D:2, R:3, STOP:4 }; 2.添加子弹类的相关属性 SPEED:10, WIDTH:15, HEIGHT:15, x:null, y:null, dir:null, live:true, tankClient:null, //LayerGradient子类TankClient 的引用 good:null, 3.子弹初始化,重画 ctor:function (x,y,good,dir,tankClient) { cc.associateWithNative( this, cc.Sprite ); this.x=x; this.y=y; this.dir=dir; this.tankClient=tankClient; this.good=good; this.initWithFile(s_missile); this.setPosition( cc.p(this.x, this.y) ); this.tankClient.addChild(this); }, Draw:function(){ if(!this.live){ this.tankClient.removeChild(this, true); return; } this.setPosition( cc.p(this.x, this.y) ); this.Move(); }, 4.添加子弹打击坦克的方法 HitTank:function(t){ if (cc.rectIntersectsRect(this.GetRectangle(), t.GetRectangle()) && t.live && this.live && this.good != t.good){ t.live = false; this.live = false; return true; } return false; }, 5.添加坦克类相关属性 SPEED:5, WIDTH:58, HEIGHT:58, x:0, y:0, l:false, u:false, r:false, d:false, dir:Direction["STOP"], ptDir:Direction["D"], tankClient:null, good:null, step:0, live:true, 6.在tank类中 坦克初始化,重画 ctor:function (x,y,good,tankClient) { cc.associateWithNative( this, cc.Sprite ); this.x=x; this.y=y; this.tankClient=tankClient; this.good=good; if(good){ this.initWithFile(s_tank); }else{ this.initWithFile(s_enemy); } this.setPosition( cc.p(this.x, this.y) ); this.tankClient.addChild(this); }, Draw:function(){ if (!this.live){ if (!this.good){ this.tankClient.removeChild(this, true); } this.tankClient.removeChild(this, true); return; } this.setPosition( cc.p(this.x, this.y) ); switch (this.ptDir) { case Direction["D"]: this.setRotation(0); //旋转精灵控制 炮筒方向 break; case Direction["U"]: this.setRotation(180); break; case Direction["L"]: this.setRotation(270); break; case Direction["R"]: this.setRotation(90); break; } this.Move(); }, 7.tank发子弹的方法 Fire:function() { if(!this.live) return null; for(var i=0;i<this.tankClient.missiles.length;i++){ var m = this.tankClient.missiles[i]; if (m.live == false){ m.x=this.x; m.y=this.y; m.live=true; m.dir=this.ptDir; m.good=this.good; this.tankClient.addChild(m); return m; } } var missile=new Missile(this.x,this.y,this.good,this.ptDir,this.tankClient); this.tankClient.missiles.push(missile); return missile; }, 8.LayerGradient加入坦克 this.tanks = []; this.myTank = new Tank(60,20, true, this); for (var i = 0; i < 10; i++){ this.tanks.push(new Tank(50 + 70 * (i + 1), 420, false, this)); } 9.LayerGradient中调用子弹打击坦克的方法 for(var i=0;i<this.missiles.length;i++){ var m = this.missiles[i]; m.HitTank(this.myTank); m.HitTanks(this.tanks); m.Draw(); } 10.控制坦克移动射击的部分代码 onKeyUp:function(key) { this.myTank.KeyReleased(key); }, onKeyDown:function(key) { this.myTank.KeyPressed(key); } 11.用Ant和compiler合并压缩js后发布到sae 如果你发现有什么不合理的,需要改进的地方,请留言。或者可以通过 328452421@qq.com 联系我,非常感谢。 http://blog.csdn.net/xiaoxiao108/article/details/8913616
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值