Colon

create: function() => unexpected token :

Recommended Posts

Hey I am trying to get a plugin running, the Dialog Manager Plugin
It gave me already a few problems like require.js but I think I solved them.

But now the create: function() gives me a problem. It says "Uncaught SyntaxError: Unexpected token :"
I found a good explanation about the differences of function create() vs create: function()
But I am not realy sure what to do with the code to get it running. Kind of new to phaser. I think there will be an easy solution for it but I don't see it and can't find very much about it. 
This is my code so far. Maybe you can help me with that. Thanks! :) I am using phaser 2 of course

<!doctype html> 
<html lang="en"> 
<head> 
	<meta charset="UTF-8" />
	<title>DiaTester</title>
	<script src="plugin/phaser.js"></script>
    <script src="//cdn.jsdelivr.net/phaser/2.2.2/phaser.min.js"></script>
    <script src="plugin/lodash.min.js"></script>
    <script src="plugin/phreaknation.manager.dialog.min.js"></script>
    <script src="plugin/require.js" "></script>

    <style type="text/css">
        body {
            margin: 0;
        }
    </style>
</head>
<body>

<script type="text/javascript">

var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update });

function preload(){
	
};

function create(){

	game.plugins.add(PhreakNation.Plugins.DialogManager);

};

function update(){
};

var config = require(['plugin/config2.js']);
var manDiag;

(function() {
  'use strict';

  var state = function state(game) {};

  state.prototype = {
    ...	
    create: function () {
      ...
      manDiag = this.game.plugins.add(PhreakNation.Plugins.DialogManager);
      manDiag.load(config);
      var Conv = manDiag.get('dialog', 1);
      
      Conv.play();
      
      console.log('[%s]: %s', Conv.actor().name(), Conv.text());
      
      var list = Conv.answers();
      var choice = _.random(0, 2);
      console.log('Options: ', list);
      console.log('Chose: `%s`', list[choice]);
      Conv.choose(choice);
      console.log('[%s]: %s', Conv.actor().name(), Conv.text());

      Conv.next();
      console.log('[%s]: %s', Conv.actor().name(), Conv.text());

      Conv.next();
      console.log('[%s]: %s', Conv.actor().name(), Conv.text());

      list = Conv.answers();
      choice = _.random(0, 2);
      console.log('Options: ', list);
      console.log('Chose: `%s`', list[choice]);
      Conv.choose(_.random(0, 1));
      console.log('[%s]: %s', Conv.actor().name(), Conv.text());

      Conv.next();
      console.log('[%s]: %s', Conv.actor().name(), Conv.text());
      }
      ...
    },
    ...
  };

  window.MyGame.states.MyState = state;
})();


</script>
</body>
</html>

 

Share this post


Link to post
Share on other sites

1.No I didn't solve it yet. I can't get it running.
2.Maybe, I am not sure, at least I have an index.html now an my example in a .js file. 

It doesn't give me any errors but it doesn't go thru the create:function()

Working on it for many hours now, but no success. And also in the config.js was that line which gave me an error: 

modulee.exports = { ....

The "modulee" gives me the error. Gives me no more error with "module" thats ok, an easy fix but that means it was never tested? I am just confused.

 

var config = require(['config2.js']);
console.log("%cDialog", "color:white; background:red");
var manDiag;
(function() {
  'use strict';

  var state = function state(game) {};
console.log("%cstate", "color:white; background:red");
  state.prototype = {
    
    create: function () {
      console.log("%cCreate:", "color:white; background:red");
      manDiag = this.game.plugins.add(PhreakNation.Plugins.DialogManager);
      manDiag.load(config);
      var Conv = manDiag.get('dialog', 1);
      
      Conv.play();
      console.log("%cStarting my awesome gme", "color:white; background:red");
      console.log('[%s]: %s', Conv.actor().name(), Conv.text());
      
      var list = Conv.answers();
      var choice = _.random(0, 2);
      console.log('Options: ', list);
      console.log('Chose: `%s`', list[choice]);
      Conv.choose(choice);
      console.log('[%s]: %s', Conv.actor().name(), Conv.text());

      Conv.next();
      console.log('[%s]: %s', Conv.actor().name(), Conv.text());

      Conv.next();
      console.log('[%s]: %s', Conv.actor().name(), Conv.text());

      list = Conv.answers();
      choice = _.random(0, 2);
      console.log('Options: ', list);
      console.log('Chose: `%s`', list[choice]);
      Conv.choose(_.random(0, 1));
      console.log('[%s]: %s', Conv.actor().name(), Conv.text());

      Conv.next();
      console.log('[%s]: %s', Conv.actor().name(), Conv.text());
      }
      
    
    
  };

//  window.MyGame.states.MyState = state;
})();

Thanks

Share this post


Link to post
Share on other sites

@Colon, well... Shame on me I didn't work with modules yet. But. About using of states. Here is JUST EXAMPLE (Even don't know whether the following information will be useful for you):

Let's assume we have 3 files: index.html, preloader.js, application.js.

index.html:


<!doctype html>
<html>
    <head>
        <title>My App :)</title>
        <meta charset="utf-8">

        <script src="../libs/phaser/phaser2/2.11.1.js" type="text/javascript"></script>
        <script src="js/application.js" type="text/javascript"></script>
        <script src="js/preloaded.js" type="text/javascript"></script>
    </head>
    <body>

        <script type="text/javascript">

        var game = null;

        document.addEventListener("DOMContentLoaded", function(event) { 

            game = new Phaser.Game({
                
                width: window.innerWidth * window.devicePixelRatio,
                height: window.innerHeight * window.devicePixelRatio,
                renderer: Phaser.WEBGL_MULTI,
                parent: '',
                transparent: false,
                antialias: true,
                physicsConfig: null,
                
                preserveDrawingBuffer: true,
                enableDebug: false
            });

            // add states to State Manager
            game.state.add('Preloader', MyGame.Preloader);
            game.state.add('Application', MyGame.Application);

            // start "Preloader" state
            game.state.start('Preloader');
            
        });
            

        </script>

    </body>

</html>

 

preloader.js:


var MyGame = {

};

MyGame.Preloader = function (game) {

};

MyGame.Preloader.prototype = {

    // this function will be called first
    init: function () {

    },

    // second
    preload: function () {

        this.load.path = "assets/images/";
        this.load.images(["image1", "image2"]);

        this.load.path = "assets/sprites/";
        this.load.atlas("sprite1");
        this.load.atlas("sprite2");

    },

    // third
    create: function () {

        // here we start a new state "Application"
        this.state.start("Application");

    },
};

 

application.js:



MyGame.Application = function (game) {

};

MyGame.Application.prototype = {
    
    init: function () {

    },

    create: function () {

    },

    update: function () {

    },

    render: function () {

    },

};

 

So we can call each state as an object with its methods (such as init, create, update, render, loadupdate and more...)

I'd recommend you to read Interphase book. As of me it is the best book for phaser 2. States are explained there too.

Share this post


Link to post
Share on other sites

Thanks, the book is good. I read the first 100 pages about the states and it helped me alot. 
I am still not sure where and how I should use the exmaple code.
The example code is an own state I think, but the stateManager does not find it, because of the (function(){ 'use strict' ...
If I delete the (function(){ 'use strict' ... the state is found. What can I do that the stateManager finds that state, even if I am using (function(){ 'use strict' ... ?

If I dont use it, stateManager finds the state, but it gives me an error that the Conv.play() is not a function.

1: Line 3&4 , what I think, is that this means, that the following method or state? in the brackets uses strict, which helps to throw more exceptions. 

(function() {
  'use strict'; ....

and 2: Line  6-8  if we look at my preloader.js and the example, var state is equal to my Game.preloader. What is the difference between function state(game) and function (game)

var state = function state(game) {};

  state.prototype = { ...

and pretty sure that Line 48 will give me a problem too

window.MyGame.states.MyState = state;

 

Here is my code so far:
index.html

<!doctype html>
<html>
	<head>
        <meta charset="UTF-8"/>
        <title>2DWorld</title>
        <script src="scripts/phaser.min.js" type="text/javascript"></script>
        <script src="states/preloader.js" type="text/javascript"></script>
        <script src="states/app.js" type="text/javascript"></script>
        <script src="states/dialog.js" type="text/javascript"></script>
        <script src="scripts/lodash.min.js" type="text/javascript"></script>
        <script src="scripts/phreaknation.manager.dialog.min.js" type="text/javascript"></script>
        <script src="scripts/require.js" type="text/javascript"></script>
    </head> 
    <body>
        
		<script type="text/javascript">
            

        var game = new Phaser.Game(800, 600);
        
       
        game.state.add("Preloader", Game.preloader);
        game.state.add("Dialog", Game.dialog);
    //    game.state.add("App", Game.app); 
        
        game.state.start("Preloader", false);

            
		</script>
    

        
        
    </body>
</html>

preloader.js 

var Game = {};

var manDiag;
var config;

Game.preloader = function (game) {};

Game.preloader.prototype = {
    
    init: function(){
        
    },
    
    preload: function(){
        
        console.log("preloader, preloaded");
    },
    
    create: function(){
        game.plugins.add(PhreakNation.Plugins.DialogManager);
 //       manDiag = this.game.plugins.add(PhreakNation.Plugins.DialogManager);
        config = require (['scripts/config.js']);
 //       manDiag.load(config);

        this.state.start("Dialog");
    },
};

dialog.js 


(function() {
  'use strict';

  Game.dialog = function state(game) {};

  Game.dialog.prototype = {
    
    create: function () {
      
      manDiag = this.game.plugins.add(PhreakNation.Plugins.DialogManager);
      manDiag.load(config);
      var Conv = manDiag.get('dialog', 1);
      
      Conv.play();
      
      console.log('[%s]: %s', Conv.actor().name(), Conv.text());
      
      var list = Conv.answers();
      var choice = _.random(0, 2);
      console.log('Options: ', list);
      console.log('Chose: `%s`', list[choice]);
      Conv.choose(choice);
      console.log('[%s]: %s', Conv.actor().name(), Conv.text());

      Conv.next();
      console.log('[%s]: %s', Conv.actor().name(), Conv.text());

      Conv.next();
      console.log('[%s]: %s', Conv.actor().name(), Conv.text());

      list = Conv.answers();
      choice = _.random(0, 2);
      console.log('Options: ', list);
      console.log('Chose: `%s`', list[choice]);
      Conv.choose(_.random(0, 1));
      console.log('[%s]: %s', Conv.actor().name(), Conv.text());

      Conv.next();
      console.log('[%s]: %s', Conv.actor().name(), Conv.text());
      }
      
    }
    
}
// window.MyGame.states.MyState = state;
 )


Thanks,
Colon

Share this post


Link to post
Share on other sites

@Colon, why do you need to use immediately called function? (function()

If you still want to use "(function(){})()", I assume this code will be useful for you:


'use strict';

Game.dialog = function state(game) {};
	
(function() {
    Game.dialog.prototype = {
    
    	create: function () {
      
      	    console.log("here we are...");
    	}
      
    }
})();

but I can't imagine why do you need to do so. There is no point of it... (So it's a bad code);

 

On 11/22/2018 at 7:44 AM, Colon said:

it gives me an error that the Conv.play() is not a function. 

So Conv.play() is not a function. What is Conv at all?

Share this post


Link to post
Share on other sites
On 11/24/2018 at 8:18 PM, dude78 said:

(So it's a bad code)

Yes it maybe is....

I checked the actual plugin due to the Conv.play(), and I adressed it wrong, its (id, type) not (type, id) as it says in the example. 
And I have a new error now (Cannot read property '0' of undefined). Just running from one error to another. Not sure right now if I will get that script running some time.
Thanks so much for you help so far, not sure what I should do with it. 
Do you got that plugin running? 

Share this post


Link to post
Share on other sites
19 hours ago, Colon said:

Yes it maybe is....

@Colon, Just there is no point to use it. If you need sth to be done when this state called, just add it either to create or init function. That code ("bad code") does nothing. But if I got your idea, a tip above might be useful.

19 hours ago, Colon said:

Do you got that plugin running? 

I didn't run it yet. I even don't whether it's for free or not. I'll take a look at it later and let you know then.

Share this post


Link to post
Share on other sites

ok so basically, my project is about dialogs with npcs.
I have for example a json file, generated with basic information about a npc: job, where/when he/she was born, name, how he/she looks.
The plan is that I can walk over to that npc, and have a dialog with them. I can ask the npc suggested questions and he will answer properly. 

So my idea was to use that plugin, because it looks usefull and a bit like what I am planing to do. But maybe I should just try to write my own one and down't spend anymore time on that plugin to get it running and trying to convert then. 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.