简体   繁体   English

vue.js-Craftyjs单击不起作用

[英]vue.js - craftyjs click does not work

I have the following code yet nothing happens. 我有以下代码,但没有任何反应。

    Crafty.e('2D, Canvas, Color, Mouse')
        .attr({x: 10, y: 10, w: 40, h: 40})
        .color('orange')
        .bind('Click', function(e){
          alert('clicked', MouseEvent);
          console.log("hello");
          Crafty.log("Clicked right button");
        });

Am I missing something? 我想念什么吗?

Edit: 编辑:

Example code using answer: 使用答案的示例代码:

<template>
    <div ref='game' id='game'></div>
</template>

<script>
  /* eslint-disable no-unused-vars */
  require('@/assets/game/crafty-min.js')
  import image from '@/assets/game/background/environment_forest_evening.png'
  import button from '@/assets/game/buttons/blank-light-blue-button-md.png'
  /* eslint-enable no-unused-vars */

  export default{
    name: 'game',
    data() {
      return {
          game: null
      }
    },
    mounted: function () {
        Crafty.init(500,350, document.getElementById('game'));
        // Crafty.canvas.init();

        Crafty.e('2D, Canvas, Color, Mouse')
            .attr({x: 10, y: 10, w: 40, h: 40})
            .color('orange')
            .bind('Click', function(e){
              alert('clicked', MouseEvent);
              console.log("hello");
              Crafty.log("Clicked right button");
            });
    },
    methods: {
    },
    destroyed () {
      // this.game.destroy()
    },
    updated () {

    }
  }
</script>

<style>
    #game {
        border: 1px solid black;
        margin: auto;
        height: 300px;
        width: 100%;
    }
</style>

Did you initialize Crafty? 您初始化了Crafty吗? I tried your code in jsfiddle 我在jsfiddle中尝试了您的代码

// Init Crafty: //初始化狡猾:

Crafty.init();
Crafty.canvas.init();

Crafty.e('2D, Canvas, Color, Mouse')
        .attr({x: 10, y: 10, w: 40, h: 40})
        .color('orange')
        .bind('Click', function(e){
          alert('clicked', MouseEvent);
          console.log("hello");
          //Crafty.log("Clicked right button");
        });

http://jsfiddle.net/d0Ltog5s/1/ http://jsfiddle.net/d0Ltog5s/1/

Edit : as per your comment here is the one using vue. 编辑:根据您的评论,这里是使用vue的评论。 (I can't use fiddle for export so used old style (我不能用小提琴出口,所以用的是旧样式

Jsfiddle link : https://jsfiddle.net/x0es9214/1/ Jsfiddle链接: https ://jsfiddle.net/x0es9214/1/

 Vue.component('game', { template: `<div ref='game' id='game'></div>`, data: function () { return { game: null }; }, mounted: function () { Crafty.init(); Crafty.canvas.init(); Crafty.e('2D, Canvas, Color, Mouse') .attr({x: 10, y: 10, w: 40, h: 40}) .color('orange') .bind('Click', function(e){ alert('clicked', MouseEvent); console.log("hello"); //Crafty.log("Clicked right button"); }); //alert('crafty'); } }) new Vue({ el: '#app' }) 
 <script src="https://unpkg.com/vue/dist/vue.js"></script> <script src="https://unpkg.com/vue-resource/dist/vue-resource.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/crafty/0.5.4/crafty-min.js"></script> <div id="app"> <game></game> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM