zoukankan      html  css  js  c++  java
  • (原)用pixi.js 实现 方块阵点击后原地自转效果

    源码

       各位,请教一个问题,我这个还有BUG,我是想实现,点击一下可以 停止转动,然后再点一下重新转动。而不是一直加速,有没有什么好办法?

       PS:问题已经解决,谢谢评论的大神@Antineutrino !
    http://files.cnblogs.com/files/kmsfan/pixi.js
    <script src="~/Scripts/pixi.js"></script>
    
    <div id="container"></div>
    
    
    <script>
        var renderer = PIXI.autoDetectRenderer(800, 500, { backgroundColor: 0x1099bb });
        $("#container").append(renderer.view);
        var stage = new PIXI.Container();
    
        var container = new PIXI.Container();
        stage.addChild(container);
        
        var bunnyArray = new Array();
    
        for (var i = 0; i < 5; i++)
        {
            bunnyArray[i] = new Array();
    
        }
    
        //载入图片 
        //for (var i = 0; i < 5; i++)
        //{
        //    for (var j = 0; j < 5; j++)
        //    {
        //        var bunny = PIXI.Sprite.fromImage("/Content/img/bunny.png");
        //        bunny.x = 40 * j;
        //        bunny.y = 40 * i;
        //        bunny.interactive = true;
        //        //bunny.on("mousedown",onClick);
                
        //        bunnyArray[i][j]=bunny;
    
        //        container.addChild(bunnyArray[i][j]);
        //    }
        //}
    
        for (var i = 0; i < 5; i++) {
            for (var j = 0; j < 5; j++) {
                var rect = new PIXI.Graphics();
                var width = 70;
    
                rect.lineStyle(1, randomColor());
                rect.interactive = true;
                rect.hitArea = new PIXI.Rectangle(width * i, width * j, width, width);
                rect.drawRect(width * i, width * j, width, width);
                
                bunnyArray[i][j] = rect;
                container.addChild(bunnyArray[i][j]);
    
            }
        }
    
        for (var i = 0; i < bunnyArray.length; i++)
        {
            for (var j = 0; j < bunnyArray[i].length; j++)
            {
    
                bunnyArray[i][j].on("click", onClick);
            }
    
        }
    
        container.x = 200;
        container.y = 60;
    
        renderImage();
    
        function renderImage()
        {
            requestAnimationFrame(renderImage);
            //renderer.render(container);
            renderer.render(container);
            //renderer.render(stage);
        }
    
        function animate() {
            requestAnimationFrame(animate);
    
            var bunny1 = thisPointer;
    
            bunny1.rotation += 0.03;
        cancelAnimationFrame(request);
           
    
        }
    
        function animateErase()
        {
            //requestAnimationFrame(animateErase);
            var bunny1 = thisPointer;
            bunny1.rotation -= 0.01;
        }
    
        var thisPointer;
        var request;
        function onClick(eventData)
        {
         
            thisPointer = calcuatePos(eventData);
        request = requestAnimationFrame(animate);
         
          
        }
    
        //生成随机颜色
        function randomColor() {
    
            var colorStr = Math.floor(Math.random() * 0xFFFFFF).toString(16).toUpperCase();
    
            return "000000".substring(0, 6 - colorStr) + colorStr;
    
        }
    
        //判断是否点击了这个东西
        function calcuatePos(eve)
        {
            var x = (eve.data.global.x);
            var y = (eve.data.global.y);
    
            
            x = x - container.x;
            y = y - container.y;
    
    
            for (var i = 0; i < bunnyArray.length; i++) {
                for (var j = 0; j < bunnyArray[i].length; j++) {
    
                    var instance = bunnyArray[i][j];
    
                    if (instance.hitArea.x <= x && instance.hitArea.x + 70 >= x && instance.hitArea.y <= y && instance.hitArea.y+70>=y) {
                        instance.x = instance.hitArea.x+70/2;
                        instance.y = instance.hitArea.y+70/2;
                        instance.pivot.x = instance.hitArea.x+70/2;
                        instance.pivot.y = instance.hitArea.y+70/2;
                        return instance;
                    }
                    
                }
    
            }
        }
    
    
    
    </script>
    

    效果 

      

     
  • 相关阅读:
    《我是一只IT小小鸟》
    &&、||、?:、,四个运算符的求值顺序
    C Traps and Pitfalls 练习4.2
    “检测到LoaderLock”的解决办法
    VS中代码对齐等快捷键
    贪心 Greedy Algorithms
    这些最基本的程序优化方法你用过吗?
    内存区划分、内存分配、常量存储区、堆、栈、自由存储区、全局区[C++][内存管理][转载]
    [原创]让对话框的控件支持tooltips
    Debug 运行正常但 Release 失败的问题,Debug 和 Release 编译方式的本质区别
  • 原文地址:https://www.cnblogs.com/kmsfan/p/4839730.html
Copyright © 2011-2022 走看看