Javascript動畫類比

來源:互聯網
上載者:User

動畫類比

主要效果就是滑鼠點擊地圖,人物會在地圖上行走。

主要代碼

  關於移動還是用到了向量(Vector)的概念

  判斷移動方向碼,根據方向來選擇走路效果需要用到的圖片

                direction: function(pos, target) {                    var z = Math.abs(target.x - pos.x);                    if (target.x > pos.x) {                        if (target.y >= pos.y - z && target.y <= pos.y + z) {                            return 'E';                        }                        if (target.y < pos.y - z) {                            return 'N';                        }                        if (target.y > pos.y + z) {                            return 'S';                        }                    }                    if (target.x < pos.x) {                        if (target.y >= pos.y - z && target.y <= pos.y + z) {                            return 'W';                        }                        if (target.y < pos.y - z) {                            return 'N';                        }                        if (target.y > pos.y + z) {                            return 'S';                        }                    }                }

   走路效果的代碼,就是不停的切換圖片來達到走路效果

                walk: function(obj, wdir) {                    var s = obj.style;                    this.pos.x += this.man.w;                    switch (wdir) {                        case 'E':                            this.pos.y = 216;                            break;                        case 'N':                            this.pos.y = 324;                            break;                        case 'S':                            this.pos.y = 0;                            break;                        case 'W':                            this.pos.y = 108;                            break;                    }                    if (this.pos.x >= this.img.w) {                        this.pos.x = 0;                        //this.pos.y += this.man.h;                        if (this.pos.y >= this.img.h) {                            this.pos.x = 0;                            this.pos.y = 0;                        }                    }                    s.left = -this.pos.x + 'px';                    s.top = -this.pos.y + 'px';                }

修正

   修正滑鼠點擊後,人物的腳在滑鼠的點擊位置,主要是加一個位移量,半個人物的寬度和一個人物的高度,因為原先位置是在左上方(left,top)

 this.footPos = this.lxyPos.add(this.tempPos); var t = target.subtract(this.footPos).normalize();

 示範效果

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.