动画开发之PIXI开发

简单的移动小游戏只要引入pixi.min.js就可以,
如果要用spine动画(龙骨也支持导出spine格式的)就要引入pixi-spine.js
如果还有声音的支持引入pixi-sound.js
 
学习网址:
 
- 官网http://www.pixijs.com
 

- 入门资料https://github.com/kittykatattack/learningPixi

- pixi中文翻译教程https://www.bookstack.cn/read/LearningPixi/loading/

- spine动画https://github.com/pixijs/pixi-spine

- 声音https://github.com/pixijs/pixi-sound
 
 下面是一个demo:
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        * {padding: 0; margin: 0}
        body{
            font-size: 12px;
        }
        #app{
            width: 19.2rem;
            height: 10.8rem;
            position: absolute;
            left: 50%;
            top: 50%;
            margin-left: -9.6rem;
            margin-top: -5.4rem;
     
        }
    </style>
</head>
<body style="background:black;">
    <div id="app"></div>
</body>
<script>
    function setHtmlFontSize() {
        var w = document.body.getBoundingClientRect().width||document.documentElement.getBoundingClientRect().width;
        var fontSize = ''
        fontSize = w/1920*100
        document.getElementsByTagName('html')[0].style.fontSize = fontSize+'px';
    }
    setHtmlFontSize()
    window.onresize=function(){
        setHtmlFontSize()
    }
    // document.getElementById('app').style.cursor="url('static/assets/images/default.png'),auto";
</script>
<script src="../pixi/pixi.min.js"></script>
<script src="../pixi/pixi-spine.js"></script>
<script src="../pixi/pixi-sound.js"></script>
<script>
 
 
//设置别名
var Application = PIXI.Application;
var loader = PIXI.loader;
var Sprite = PIXI.Sprite;
var resources = PIXI.loader.resources;
var Container = PIXI.Container;
var Graphics = PIXI.Graphics;
 
//实例化PIXI,并设置画布
var app = new Application({
    width:1920,
    height:1080,
    antialias:true,
    transparent:false,
    resolution:1,
    autoResize:true
})
 
//设置画布的rem,自适应画布
app.view.style.width = '19.2rem';
app.view.style.height = '10.8rem';
 
window.stage = app.stage;
// 设置画布全屏展示
// app.renderer.view.style.position = "absolute";
// app.renderer.view.style.display = "block";
// app.renderer.autoResize = true;
// app.renderer.resize(window.innerWidth,window.innerWidth/1920*1080)
// app.renderer.resize.width=window.innerWidth;
 
document.getElementById('app').appendChild(app.view);
 
//添加纹理,promise保证json文件load结束,然后执行下面的其他渲染及动画逻辑
new Promise((resolve, reject) => {
            loader.add('question','static/content.json');
            loader.add('resources','static/resource.json');
            loader.load(() => {
                let content = resources["resources"].data;
                // console.log(content)
                // console.log(content)
                content.forEach(value => {
                    // console.log(value.key, value.path)
                    //添加其他资源
                    loader.add(value.key, value.path);
                 
                    // //监听image/audio
                    if(value.key.indexOf('image')>-1){
                        var newImg = new Image();
                        newImg.src = value.path;
                        newImg.onerror = function(e){
                            console.log(e);
                        }
                    }else if(value.key.indexOf('audio')>-1){
                        var newAudio = new Audio();
                        newAudio.src = value.path;
                        newAudio.onerror = function(e){
                            console.log(e)
                        }
                    }
                 
                });
                loader.load((l,r) =>{
                    // // window.res = r;
                    // console.log(l,r)
                    resolve(r)
                });
                loader.onError.add((e) => {
                    console.log("loader加载错误");
                });
                loader.onProgress.add((e) => {
                    // console.log("loader加载进程中");
                    // document.getElementsByClassName('runner')[0].style.width = e.progress*6.8/100+'rem'
                });
                loader.onComplete.add((e) => {
                    // console.log("loader加载完成");
                });
 
            })
        })
        .then(()=>{
            // console.log(res)
            setup(resources)
        })
 
//加载的资源
function setup(res) {
     
    var gameScene;
    window.res=res
    window.question = res.question.data;
 
     /**
     * 添加声音
    */
    var bgSound = res.audio_Bg.sound
    var rightSound = res.audio_right.sound
    var wrongSound = res.audio_wrong.sound
    var finishSound = res.audio_finish.sound
    // bgSound.play()
 
    // 全部页面容器
    var allPage = new Container();
    app.stage.addChild(allPage)
 
    //开始
    var startPage = new Container();
    allPage.addChild(startPage)
    //背景
    var startBg = new Sprite.fromImage(res.image_startBg.url);
    startPage.addChild(startBg);
 
    // btn按钮
    var startBtnAnimationUI = new PIXI.spine.Spine(res.animation_btn.spineData)
    var startBtnAnimation = startBtnAnimationUI.state.setAnimation(0,'animation',true)
    startBtnAnimationUI.x=960;
    startBtnAnimationUI.y=843;
    startBtnAnimationUI.cursor = 'pointer'
    startBtnAnimationUI.interactive = true;
    startBtnAnimationUI.on('pointerdown',function(){
        // console.log(1)
        startPage.visible = false;
        gameScene.visible = true;
        bgSound.play()
        bgSound.loop = true;
    })
    startPage.addChild(startBtnAnimationUI)
     
     
    //游戏页面
    gameScene = new Container();
    gameScene.visible = false;
    allPage.addChild(gameScene);
 
    //背景
    var bgImage = new Container();
    var gameBg = res.question.data.sources[0].bgImage
    var gameBgRes = res[gameBg.image.name].url;
    var gameBg = new Sprite.fromImage(gameBgRes);
    bgImage.addChild(gameBg);
    gameScene.addChild(gameBg);
 
    //母鸡
    var hen = new Container()
    var exNoAnimationCon = new PIXI.spine.Spine(res.animation_animates.spineData);
    let exNoAnimation = exNoAnimationCon.state.setAnimation(0, 'wait', true);
    exNoAnimationCon.x = 960;
    exNoAnimationCon.y = 543;
    hen.addChild(exNoAnimationCon);
    gameScene.addChild(hen);
 
    //鸡蛋
    var eggs = new Container();
    gameScene.addChild(eggs);
 
    //结果页
    var result = new Container();
    gameScene.addChild(result);
    var resultBg = new Sprite.fromImage(res.image_resultAn.url);
    //设置背景色
    let rectangle = new Graphics();
    rectangle.alpha = 0.5
    rectangle.beginFill(0x1b1919);
    rectangle.drawRect(0, 0, 1920, 1080);
    rectangle.endFill();
    rectangle.x = 0;
    rectangle.y = 0;
    let style = new PIXI.TextStyle({
        fontFamily: "Arial",
        fontSize: 80,
        fill: "white",
        stroke: 'yellow',
        strokeThickness: 4,
        dropShadow: true,
        dropShadowColor: "#000000",
        dropShadowBlur: 4,
        dropShadowAngle: Math.PI / 6,
        dropShadowDistance: 6,
    });
    //设置文字
    var resultText = new PIXI.Text('2',style)
    resultText.position.set(1000,620)
 
    var resultTextOver = new PIXI.Text('Game over!',style)
    resultTextOver.position.set(750,900)
 
    result.addChild(rectangle);
    result.addChild(resultBg);
    result.addChild(resultText);
    result.addChild(resultTextOver);
    result.visible = false;
 
    // 引入的数据
     
    var eggEvent = []
    var answers=['A','B','C','D']
    for(let j=0;j<question.sources[0].answer.flags.length;j++){
        let  egg = new Sprite.fromImage(res[question.sources[0].answer.flags[j].image_name].url);
        //坐标位置
        egg.x = question.sources[0].answer.flags[j].x;
        egg.y = question.sources[0].answer.flags[j].y;
 
        egg.startX = question.sources[0].answer.flags[j].x;
        egg.startY = question.sources[0].answer.flags[j].y;
        egg.answer = answers[j]
        // console.log(question.sources[0].answer.flags[j].x)
        //设置锚点位置为中心
        egg.anchor.set(0.5);
        eggEvent.push(egg);
        //这个循环不行
        // egg.interactive = true;
        // 设置鸡蛋的抓取
        // egg.on('pointerdown',eggPointerDown)
        // egg.on('pointermove',eggPointerMove)
        // egg.on('pointerup',eggPointerUp)
        // egg.on('pointerupoutsize',pointerUpOutsize)
        eggs.addChild(egg);
         
    }
 
    /**
     * 测试得出map forEach 可以添加方法  for循环不可以
    */
    eggEvent.map(function(value,index){
        value.interactive=true;
        value.on('pointerdown',eggPointerDown)
        value.on('pointermove',eggPointerMove)
        value.on('pointerup',eggPointerUp)
        value.on('pointerupoutsize',pointerUpOutsize)
        /**
         *
        */
       var appDomStyle = document.getElementById('app').style
        var flag=false;
        function eggPointerDown(ev){
            flag = true;
            var pointerPosition = ev.data.getLocalPosition(this.parent)
            //鼠标位置与egg锚点的距离
            this.posX = pointerPosition.x-this.x
            this.posY = pointerPosition.y-this.y
            // console.log(this.posX,this.posY)
            appDomStyle.cursor = "url('static/assets/images/drag.png'),auto";
        }
        function eggPointerUp(ev){
            flag = false;
            appDomStyle.cursor = "url('static/assets/images/default.png'),auto";
            // console.log(exNoAnimationCon.x)
            // console.log(this.x)
            // console.log(exNoAnimationCon.width/2)
            console.log(this.answer)
            if(Math.abs(exNoAnimationCon.x-this.x ) <= exNoAnimationCon.width/2 && Math.abs(exNoAnimationCon.y-this.y )<= exNoAnimationCon.height/2){
                // alert(1)
                if(this.answer==="A") {
                    rightSound.play()
                    exNoAnimationCon.state.setAnimation(0, 'right', true);
                    //禁止交互行为
                    eggs.children.forEach((item,index)=>{
                            item.buttonMode = false;
                            item.interactive = false;
     
                    });
                    this.visible=false;
                    finishSound.play()
                    // bgSound.paused = true
                    exNoAnimationCon.state.addAnimation(0, 'finish', false);
                     
                    setTimeout(()=>{
                        bgSound.stop()
                        result.visible = true;
                    },6500)
 
                } else {
                    wrongSound.play()
                    exNoAnimationCon.state.setAnimation(0, 'wrong', false);
                    exNoAnimationCon.state.addAnimation(0, 'wait', true);
                    this.position.x = this.startX
                    this.position.y = this.startY
                }
            } else {
                exNoAnimationCon.state.setAnimation(0, 'wait', true);
                this.position.x = this.startX
                this.position.y = this.startY
            }    
        }
        function pointerUpOutsize(ev){
            flag = false;
            appDomStyle.cursor = "url('static/assets/images/default.png'),auto";
        }
        function eggPointerMove(ev){
            var pointerPosition = ev.data.getLocalPosition(this.parent)
            // console.log(pointerPosition)
            // console.log(flag)
            if(flag) {
                appDomStyle.cursor="url('static/assets/images/drag.png'),auto";               
                this.position.x = pointerPosition.x - this.posX;
                this.position.y = pointerPosition.y - this.posY; 
                if(Math.abs(exNoAnimationCon.x-this.x ) <= exNoAnimationCon.width/2 && Math.abs(exNoAnimationCon.y-this.y )<= exNoAnimationCon.height/2){
                // alert(1)
                    exNoAnimationCon.state.setAnimation(0, 'drag', true);
                }else{
                    exNoAnimationCon.state.setAnimation(0, 'wait', true);
                }             
            }
        }
    })
 
 
 
}
 
 
 
 
 
</script>
</html>

  

如果觉得文章不错,可以给小编发个红包给予鼓励.

posted @   地铁程序员  阅读(2838)  评论(2编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
点击右上角即可分享
微信分享提示