Is there a way to pass data through the scene.switch() function? I've seen this in other scene changing functions, but not this one and I don't know how to receive the data. How could I do that if my code looks something like this, where message is the second parameter passed through scene.switch() function.
class MyScene1 extends Phaser.Scene {
constructor() {
super('scene1')
}
create() {
this.scene.switch('scene2', 'greetings from scene 1')
}
}
class MyScene2 extends Phaser.Scene {
constructor() {
super('scene2')
}
create() {
// Set message to the value that was passed through scene.switch()
console.log(message)
}
}
Ok, so I did some research and it turns out you can do this in the init() function, like so:
class MyScene1 extends Phaser.Scene{
constructor(){
super('scene1')
}
create(){
this.scene.switch('scene2', 'greetings froms scene 1')
}
}
class MyScene2 extends Phaser.Scene{
constructor(){
super('scene2')
}
init(message){
console.log(message)
}
}
Related
Wonder if anyone can help?
I'm trying to call a parent's constructor or at a minimum get a reference to the parent class in some way without hardcoding anything.
class A {
static foo(options) {
parent::__construct(options); <- this is how you would get the parent in php
}
}
class B extends A {
}
Is this possible?
In a javascript class (and OOP in general), a static method is not part of an instance and therefore the object it resides in does not have a constructor.
You should avoid using static method for this sort of thing and use a standard constructor and call super() to call the parent constructor.
class A {
constructor(options) {
console.log('Options are:');
console.log(options);
}
}
class B extends A {
constructor(options) {
super(options);
}
}
const item = new B({item1: 'abc'});
Further reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/super
You can use super() to call parent constructor
class A {
constructor() {
console.log('I\'m parent ');
}
foo(){
console.log('Class A: Called foo');
}
}
class B extends A {
constructor() {
super();
}
foo(){
super.foo()
}
}
const b = new B();
b.foo();
I have a situation like that:
class TestClass {
constructor() {
shared logic
}
anotherFunction() {
shared logic
}
}
How can I achieve that not duplicating the code?
As always, create a function for the shared logic, either inside of the class or outside of it.
class TestClass {
constructor() {
this.sharedLogicFunction();
}
anotherFunction() {
this.sharedLogicFunction();
}
sharedLogicFunction() {}
}
Put your code to the anotherFunction() and call this function from constructor.
class TestClass {
constructor() {
this.anotherFunction();
}
anotherFunction() {
here is some logic to do
}
}
I am trying to call function outside from class and update the state of class. Is this correct way ?
function myFunc() {
this.Test("test")
}
class Notification extends Component {
constructor(props) {
super(props);
this.state = {
demoState:''
};
}
Test(data){
this.setState({
demoState:data
})
}
render(){
return(<div/>)
}
}
You haven't provided function usage in your example.
It's correct way, that's how the most function works. But don't tease react.js by using this argument - just pass an argument to myFunc
YES pass this an argument to myFunc.
function myFunc(this /* <----- */) {
this.Test("test")
}
Then call myFunc from inside of class like this:
myFunc(this);
May be, you need to pass object of that class to change the state. Have a look at myFunc & Notification.Test function changes.
function myFunc() {
let notification = new Notification({});
notification.Test("test");
console.log(notification.state);
}
class Notification extends Component {
constructor(props) {
super(props);
this.state = {
demoState: ''
};
}
Test(data) {
this.state = {
demoState: data
};
}
render() {
return ('<div/>')
}
}
class PathController {
constructor(){
}
getMainPage(){
alert("getMainPage");
}
setPushState(){
alert("setPushState");
}
}
class MainMenu extends PathController {
constructor (){
// call my PathController here
super();
getMainPage();
setPushState();
}
}
let aMainMenu = new MainMenu();
my intention is to call my getMainPage and setPushState at my MainMenu constructor , i tired this.getMainPage and this.setPushState and it is not working as well. can anyone tell me how to call it ?
Your super is your "this" since we are currently in the constructor. Here's how it should look:
class PathController {
constructor(){
}
getMainPage(){
alert("getMainPage");
}
setPushState(){
alert("setPushState");
}
}
class MainMenu extends PathController {
constructor (){
// call my PathController here
super();
super.getMainPage();
super.setPushState();
}
}
let aMainMenu = new MainMenu();
However once you are outside of the constructor, then you would use "this.getMainPage();"
One approach would be to pass property names to super() which call the functions at PathController parent constructor
class PathController {
constructor(fromMainMenu, ...props) {
if (fromMainMenu) {
for (let fn of props) {
this[fn]()
}
}
}
getMainPage(){
alert("getMainPage");
}
setPushState(){
alert("setPushState");
}
}
class MainMenu extends PathController {
constructor () {
// call my PathController here
super(true, "getMainPage", "setPushState");
}
}
let aMainMenu = new MainMenu();
I am curious can I call a extended class and have it still import the things it needs specifically.
Welcome Class:
import { ErrorLevel } from './error-level.js';
export class Welcome extends ErrorLevel {
constructor() {
super();
}
}
Error-Level Class:
import { Notification } from 'aurelia-notification';
export class ErrorLevel {
static inject() {
return [Notification];
}
constructor(notification) {
this.notification = notification;
}
}
I know once I call super() it will call the extended class and pass in 0 arguments. Is there a way for my ErrorClass constructor to pull in Notification when I call super()?
super([arguments]); // calls the parent constructor.
super.functionOnParent([arguments]);
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/super
Passing an argument in Welcome's super function calls the parent class constructor with that argument. You will see the log of this contains notification which is set to the argument we pass into super.
http://jsbin.com/raguqopesu/1/edit?js,console,output
class ErrorLevel {
constructor(notification) {
this.notification = notification;
}
}
class Welcome extends ErrorLevel {
constructor() {
super(Notification);
console.log(this);
}
}
const yo = new Welcome();
export class Welcome extends ErrorLevel {
constructor(notification) {
super(notification);
}
}