Jump to content

problem with time.events


Dzambor
 Share

Recommended Posts

I'm trying to get show text word by word on the screen. There was an example - I tried to adapt it to my files and can't remove "Uncaught TypeError: Cannot read property 'events' of undefined"  

If I change 

 this.time.events.repeat(120, line.length, nextWord, this);

to

 game.time.events.repeat(120, line.length, nextWord, this);

I have problem with "Uncaught TypeError: Cannot read property 'time' of undefined" 

 

Game.Intro = function(game) {};

var content = [
  "The sky above the port was the color of television, tuned to a dead channel.",
  "From Neuromancer by William Gibson"
];

var line = [];
var wordIndex = 0;
var lineIndex = 0;

var wordDelay = 120;
var lineDelay = 400;

Game.Intro.prototype = {
  create: function(game) {

    text = game.add.text(32, 32, '', {
      font: "15px Arial",
      fill: "#19de65"
    });

    nextLine();


  }
}

function nextLine(game) {

  if (lineIndex === content.length) {
    //  We're finished
    this.state.start('MainMenu');
  }

  //  Split the current line on spaces, so one word per array element
  line = content[lineIndex].split(' ');

  //  Reset the word index to zero (the first word in the line)
  wordIndex = 0;

  //  Call the 'nextWord' function once for each word in the line (line.length)
  this.time.events.repeat(120, line.length, nextWord, this);

  //  Advance to the next line
  lineIndex++;

}

function nextWord(game) {

  //  Add the next word onto the text string, followed by a space
  text.text = text.text.concat(line[wordIndex] + " ");

  //  Advance the word index to the next word in the line
  wordIndex++;

  //  Last word?
  if (wordIndex === line.length) {
    //  Add a carriage return
    text.text = text.text.concat("\n");

    //  Get the next line after the lineDelay amount of ms has elapsed
    this.time.events.add(400, nextLine, this);
  }

}

It drives me crazy. I would be thankful for any help.

Link to comment
Share on other sites

Got that in index. html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Animapocalypse</title>
    <script src = "phaser.js"> </script>
    
    <script src = "Boot.js"> </script>
    
    <script src = "Preloader.js"> </script>

    <script src = "Intro.js"> </script>
    
    <script src = "MainMenu.js"> </script>
    
    <script src = "About.js"> </script>

    <script src = "Help.js"> </script>

    <script src = "Level1.js"> </script>
</head>
<body>
	
	<script type = "text/javascript">
    window.onload = function (){
        var game = new Phaser.Game(800, 600, Phaser.CANVAS, '');
        
        game.state.add('Boot',Game.Boot);
        game.state.add('Preloader',Game.Preloader);
        game.state.add('Intro',Game.Intro);
        game.state.add('MainMenu',Game.MainMenu);
        game.state.add('About',Game.About);
        game.state.add('Help',Game.Help);
        game.state.add('Level1',Game.Level1);
        game.state.start('Boot');
    };
        
    </script>
</body>
</html>

 

Link to comment
Share on other sites

21 hours ago, samme said:

If you haven't defined game, that won't work.

You can keep this.time.events, it's the same.

I just changed 

21 hours ago, Dzambor said:

nextLine();

nextLine(game);

and its partially running 

its get stuck on:

Quote

this.time.events.add(400, nextLine, this);

any Ideas how to fix it ?

 

Edit. And other files using  game definition from index.html work perfectly fine but not this one

Link to comment
Share on other sites

Hi @Dzambor,

The problem is that the callbacks did not send the game parameter to the nextWord (game) and nextLine(game) functions, and therefore could not create the time events since game was undefined.
To fix it, I changed the callbacks of the time events.
This is the code:


var Game={};

Game.Intro = function(game) {};

var content = [
  "The sky above the port was the color of television, tuned to a dead channel.",
  "From Neuromancer by William Gibson"
];

var line = [];
var wordIndex = 0;
var lineIndex = 0;

var wordDelay = 120;
var lineDelay = 400;

Game.Intro.prototype = {
  create: function(game) {
	  var t=this;
	console.log(game);
    text = game.add.text(32, 32, '', {
      font: "15px Arial",
      fill: "#19de65"
    });

    nextLine(game);


  }
}

function nextLine(game) {

  if (lineIndex === content.length) {
	/*  
    //  We're finished
	this.state.start('MainMenu');*/
	console.log('Code for new state disabled');
  }

  //  Split the current line on spaces, so one word per array element
  line = content[lineIndex].split(' ');
var g=game;
  //  Reset the word index to zero (the first word in the line)
  wordIndex = 0;
  //  Call the 'nextWord' function once for each word in the line (line.length)
  game.time.events.repeat(120, line.length, function(){nextWord(game);}, this);

  //  Advance to the next line
  lineIndex++;

}

function nextWord(game) {
  //  Add the next word onto the text string, followed by a space
  text.text = text.text.concat(line[wordIndex] + " ");

  //  Advance the word index to the next word in the line
  wordIndex++;
  //  Last word?
  if (wordIndex === line.length) {
    //  Add a carriage return
	text.text = text.text.concat("\n");
    //  Get the next line after the lineDelay amount of ms has elapsed
    game.time.events.add(400, function(){nextLine(game);}, this);
  }

}

Regards.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...