|
@@ -1,76 +1,81 @@
|
|
|
<template>
|
|
|
- <div ref="gameContainer" class="game-container"></div>
|
|
|
+ <div ref="gameContainer" class="gameContainer"></div>
|
|
|
</template>
|
|
|
|
|
|
-<script>
|
|
|
+<script setup name="JumpRope">
|
|
|
import { onMounted, ref } from 'vue';
|
|
|
import Phaser from 'phaser';
|
|
|
+const gameContainer = ref(null);
|
|
|
+const game = ref(null);
|
|
|
+const animatorProperty = ref(null);
|
|
|
+const player = ref(null);
|
|
|
+const frameRate = ref(null);
|
|
|
|
|
|
-export default {
|
|
|
- name: 'JumpRope',
|
|
|
- setup() {
|
|
|
- const gameContainer = ref(null);
|
|
|
-
|
|
|
- onMounted(() => {
|
|
|
- // 创建 Phaser 游戏实例
|
|
|
- const config = {
|
|
|
- type: Phaser.AUTO,
|
|
|
- parent: gameContainer.value,
|
|
|
- width: 800,
|
|
|
- height: 800,
|
|
|
- transparent: true, // 设置为透明背景
|
|
|
- scene: {
|
|
|
- preload,
|
|
|
- create,
|
|
|
- update,
|
|
|
- },
|
|
|
- };
|
|
|
-
|
|
|
- new Phaser.Game(config);
|
|
|
-
|
|
|
- let rope, player;
|
|
|
-
|
|
|
- function preload() {
|
|
|
- this.load.image('rope', 'static/images/animation/2.png'); // 跳绳的图片
|
|
|
- this.load.image('player', 'static/images/animation/1.png'); // 玩家角色的图片
|
|
|
- }
|
|
|
+function preload() {
|
|
|
+ this.load.spritesheet('character', 'static/images/animation/jumprope.png', {
|
|
|
+ frameWidth: 480,
|
|
|
+ frameHeight: 480
|
|
|
+ });
|
|
|
+}
|
|
|
+function create() {
|
|
|
+ // 创建动画
|
|
|
+ animatorProperty.value = this.anims.create({
|
|
|
+ key: 'sports',
|
|
|
+ frames: this.anims.generateFrameNumbers('character', { start: 0, end: 2 }),
|
|
|
+ frameRate: 10,
|
|
|
+ repeat: -1 // -1表示无限循环
|
|
|
+ });
|
|
|
+ // 创建精灵并播放动画
|
|
|
+ player.value = this.add.sprite(240, 240, 'character');
|
|
|
+}
|
|
|
|
|
|
- function create() {
|
|
|
- // 创建跳绳
|
|
|
- rope = this.add.image(400, 300, 'rope').setOrigin(0.5, 0.5);
|
|
|
- rope.angle = 0;
|
|
|
+function update() {
|
|
|
+ animatorProperty.value.frameRate = frameRate.value;
|
|
|
+}
|
|
|
|
|
|
- // 创建玩家角色
|
|
|
- player = this.add.image(400, 450, 'player').setOrigin(0.5, 0.5);
|
|
|
+// 触发运动
|
|
|
+const sports = (data) => {
|
|
|
+ frameRate.value = data;
|
|
|
+ player.value.play('sports');
|
|
|
+};
|
|
|
|
|
|
- // 创建动画
|
|
|
- this.tweens.add({
|
|
|
- targets: rope,
|
|
|
- angle: 360,
|
|
|
- duration: 1000,
|
|
|
- repeat: -1,
|
|
|
- yoyo: true,
|
|
|
- ease: 'Sine.easeInOut',
|
|
|
- });
|
|
|
- }
|
|
|
+onMounted(() => {
|
|
|
+ // 创建 Phaser 游戏实例
|
|
|
+ const config = {
|
|
|
+ type: Phaser.AUTO,
|
|
|
+ parent: gameContainer.value,
|
|
|
+ width: 480,
|
|
|
+ height: 480,
|
|
|
+ transparent: true, // 设置为透明背景
|
|
|
+ scene: {
|
|
|
+ preload,
|
|
|
+ create,
|
|
|
+ update
|
|
|
+ }
|
|
|
+ };
|
|
|
+ game.value = new Phaser.Game(config);
|
|
|
+});
|
|
|
|
|
|
- function update() {
|
|
|
- // 每帧更新的逻辑,如果有的话
|
|
|
- }
|
|
|
- });
|
|
|
+onBeforeUnmount(() => {
|
|
|
+ if (game.value) {
|
|
|
+ game.value.destroy(true);
|
|
|
+ }
|
|
|
+});
|
|
|
|
|
|
- return {
|
|
|
- gameContainer,
|
|
|
- };
|
|
|
- },
|
|
|
-};
|
|
|
+//暴露给父组件用
|
|
|
+defineExpose({
|
|
|
+ sports
|
|
|
+});
|
|
|
</script>
|
|
|
|
|
|
<style scoped>
|
|
|
-.game-container {
|
|
|
- width: 100%;
|
|
|
- height: 100%;
|
|
|
- margin: 0 auto;
|
|
|
- position: relative;
|
|
|
+.gameContainer {
|
|
|
+ width: 480px;
|
|
|
+ height: 480px;
|
|
|
+ position: fixed;
|
|
|
+ z-index: 999;
|
|
|
+ left: 50%;
|
|
|
+ margin-left: -240px;
|
|
|
+ bottom: 1vh;
|
|
|
}
|
|
|
</style>
|