Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access Vue this from the scene object #43

Open
schatteleyn opened this issue Nov 25, 2021 · 6 comments
Open

Access Vue this from the scene object #43

schatteleyn opened this issue Nov 25, 2021 · 6 comments

Comments

@schatteleyn
Copy link

Hey there, I'm trying to access the "vue this" inside the scene object where all the magic happens, as to be able to interact between the game and the app. I've tried many ways but can't seem to find a way to properly do it, and the documentation is a bit lacking on the subject.
What's the correct way to get the vue context inside the game component ? Also, is this the right way to create interaction between the two parts ?

@jdnichollsc
Copy link
Member

Can you use an event emitter approach instead?

@schatteleyn
Copy link
Author

Well that's what I wanted to do, but I still need this.$emit inside my create() method, thus I need access to vue this

@jdnichollsc
Copy link
Member

Well that's what I wanted to do, but I still need this.$emit inside my create() method, thus I need access to vue this

Try using a third party EventEmitter system, e.g: https://www.npmjs.com/package/eventemitter3

@jdnichollsc
Copy link
Member

I mean, Phaser has an EventEmitter system and VueJS too, so try using another alternative so that they can communicate, it could even be with Service workers, imagination is the limit! <3

@Marchiuzzz
Copy link

You probably already found a solution but I'll just share how I did it for any future readers.
Declare a variable "vue" (or whatever you want to call it) outside export default block and then assign this variable in the mounted method. Example:

<script>
var vue;
var game = {
 scale: {...},
 scene: {... <we can access vue's property myText with vue.myText here> ...},
 physics: {...}
}
export default {
 data(){
  return {
   myText: '',
  };
 },
 mounted(){
  vue = this; 
 }
}
</script>

Then inside phaser game code you can access the vue variable - which will be the instance of vue and therefore you will be access methods, properties, etc. like so vue.myText

@SunKechang
Copy link

You probably already found a solution but I'll just share how I did it for any future readers. Declare a variable "vue" (or whatever you want to call it) outside export default block and then assign this variable in the mounted method. Example:

<script>
var vue;
var game = {
 scale: {...},
 scene: {... <we can access vue's property myText with vue.myText here> ...},
 physics: {...}
}
export default {
 data(){
  return {
   myText: '',
  };
 },
 mounted(){
  vue = this; 
 }
}
</script>

Then inside phaser game code you can access the vue variable - which will be the instance of vue and therefore you will be access methods, properties, etc. like so vue.myText

dude, you've saved my life!!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants