scene.vue 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <template>
  2. <div class="game-scene" id="gameSceneWrap" ref="gameScene">
  3. </div>
  4. </template>
  5. <script>
  6. import * as PIXI from 'pixi.js'
  7. require('pixi-spine')
  8. export default {
  9. name: 'gameScene',
  10. data () {
  11. return {
  12. wrapper: null,
  13. app: null
  14. }
  15. },
  16. methods: {
  17. loadSource () {
  18. PIXI.loader
  19. .add('bg', `textures/bg.jpg`)
  20. .add('spineCharacter', 'textures/people/60044.json')
  21. .load(this.loadedSource)
  22. },
  23. loadedSource () {
  24. this.initBg()
  25. this.initSpine()
  26. },
  27. initBg () {
  28. let cat = new PIXI.Sprite(PIXI.loader.resources.bg.texture)
  29. this.app.stage.addChild(cat)
  30. },
  31. initSpine () {
  32. let animation = new PIXI.spine.Spine(PIXI.loader.resources.spineCharacter.spineData)
  33. animation.x = 100
  34. animation.y = 200
  35. this.app.stage.addChild(animation)
  36. let style = new PIXI.TextStyle({
  37. fontFamily: 'Microsoft Yahei',
  38. fontSize: 32,
  39. fill: '#ffde00'
  40. })
  41. let message = new PIXI.Text('my name', style)
  42. message.anchor.x = 0.5
  43. message.x = animation.x
  44. message.y = animation.y - 180
  45. this.app.stage.addChild(message)
  46. if (animation.state.hasAnimation('walk1')) {
  47. animation.state.setAnimation(0, 'walk1', true)
  48. this.peopleWalk(animation, message)
  49. }
  50. },
  51. peopleWalk (animation, message) {
  52. this.app.ticker.add(delta => {
  53. this.walk(delta, animation, message)
  54. })
  55. },
  56. walk (delta, animation, message) {
  57. if (animation.x <= 50) {
  58. animation.scale.x = -1
  59. } else if (animation.x >= 500) {
  60. animation.scale.x = 1
  61. }
  62. animation.x -= animation.scale.x * 1
  63. message.x -= animation.scale.x * 1
  64. }
  65. },
  66. mounted () {
  67. this.wrapper = document.getElementById('gameSceneWrap')
  68. this.app = new PIXI.Application({ width: 604, height: 222, transparent: true })
  69. this.wrapper.appendChild(this.app.view)
  70. this.loadSource()
  71. }
  72. }
  73. </script>
  74. <style lang="scss">
  75. .game-scene{
  76. height: px2rem(222);
  77. margin: px2rem(20) 0;
  78. canvas{
  79. display: block;
  80. width: px2rem(604);
  81. height: 100%;
  82. margin: 0 auto;
  83. }
  84. }
  85. </style>