Results 1 to 11 of 11
  1.    #1  
    i am porting a straight javascript yatzee web-app to webos. soon i will post the basic javascript version of the app, because it was simple to modify the layout to fit on the pre and it functioned perfectly. but i would like to incorporate menus, real buttons, etc, so i will be improving it over time.

    the one sticking point that i have so far is that the app uses setTimer to give the perception that the dice are rolling and setInterval to intermittently show that die faces changing. So far, i can't get anything to happen. i know that rollDice gets called because i had an alert at the beginning and it popped up, but i can't these timer functions to do anything...

    Can anyone find anything wrong with my code:
    Code:
    // Called when someone clicks the roll dice button.
    //
    YatzAssistant.prototype.rollDice = function(event) {
    	//track the number of times the dice were rolled this round
    	this.RollCounter++;
    
    	// start two timers -- the first repeatedly simulates a roll of the 
    	// dice; the second "one-shot" timer stops the first one. The system 
    	// assigns an ID number.  When the timers stop, we set the IDs to -1 
    	// so we can determine whether the timers are active or not.
    	this.timerID= window.setInterval(this.timerRoll.bind(this),50);
    	this.timerID2 = window.setTimeout(this.stopTimer.bind(this),800);
    	
    	//prevents further rolling if user has rolled three times in current round
    	if(this.RollCounter>=3)
    	{
    	  this.rollButtonModel.disabled = true
    		this.controller.modelChanged(this.rollButtonModel);
    	} 
    }
    Code:
    /*
    This function is called repeatedly by the timer set in the butRoll function
    For each of the unchecked dice it chooses a random number between 1 and 6 
    and displays the corresponding image.  The timer effect leads to various possible 
    rolls flickering by and provides some sense of the die being rolled.  It is 
    possible when a die is re-rolled that the previous value is rolled again; without 
    the timer effect the player may be left with a sense that the die was not rolled.  
    */
    function timerRoll()
    {
    	var i;
    	
    	//Simulates the rolling of unchecked dice and displays corresponding image
    	for(i=0; i<5; i++)
    	{
    		this.DieValues[i]=Math.floor(Math.random()*6+1);
    	}
    }
    Code:
    /*
    This function stops the timer so the dice are not rolling for ever. The 
    butRoll function started two timers.  The second timer is a "one shot" timer 
    that counts off one interval and at the end of it call this function the 
    purpose of which is to stop the first timer.  (Because the second timer is 
    a one-shot, it does not have to be stopped.)  The timer IDs are set to -1 
    so we can determine whther or not the timers are active.
    */
    function stopTimer()
    {
    	this.clearInterval(timerID);
    	this.timerID=-1;
    	this.timerID2=-1;
    	this.showDialogBox("Die Values", "| "+this.DieValues[0]+" | "+this.DieValues[1]+" | "+this.DieValues[2]+" | "+this.DieValues[3]+" | "+this.DieValues[4]+" |");
    }
  2. #2  
    Probably need to prototype the functions..

    Code:
    YatzAssistant.prototype.timerRoll = function()
    
    YatzAssistant.prototype.stopTimer = function()
    I had similar issues and that seemed to solve them.
  3. #3  
    At a glance, I would say try this without the window.

    this.timerID= setInterval(this.timerRoll.bind(this),50);

    You can debug with a command line interface using Putty. Search the forum for threads explaining the process.

    You can also just put try catch blocks around your code and display any errors in a div in your scene.

    try
    {

    }
    catch(err)
    {
    $('divDebug').innerHTML = err + '<BR/>';
    }
  4. #4  
    try just putting the function in the interval, unless you need it else where

    and try this.controller.window.setInterval
  5. #5  
    Upon further inspection, you need to declare your timerRoll and stopTimer functions as follows

    YatzAssistant.prototype.timerRoll = function(event)
    {

    }

    YatzAssistant.prototype.stopTimer = function(event)
    {

    }


    Then you should pre-cache them in your initialize method.

    For example,
    this.timerRollHandler = this.timerRoll.bind(this);

    Then you would just use this syntax


    this.timerID= setInterval(this.timerRollHandler, 50);

    This is the recommended method for doing setting up event handlers.
    Last edited by Blubble; 07/15/2009 at 10:22 AM.
  6.    #6  
    i will try out all of these suggestions tonight. Thanks!
  7. #7  
    I've been using:
    Code:
    var self = this;
    setTimeout(function() { self.functionToCall(); }, 50);
  8. #8  
    Quote Originally Posted by oil View Post
    I've been using:
    Code:
    var self = this;
    setTimeout(function() { self.function(); }, 50);
    If you are using the standard assistant structure, it is advisable that you use the .bind(this) syntax. It is also highly recommended that you use the pre cache method I described to avoid memory leaks and minimize the necessity for garbage collection. Also, there is no need to use var self = this;. It's an unnecessary extra line of code.
  9. #9  
    OK, working with an existing timer app I had this problem. Thus, using above references, I came to the following:

    TimerViewAssistant.prototype.setup = function()
    {
    ...
    // Bind the updateTimer method to the updateTimerHandler
    this.updateTimerHandler = this.updateTimer.bind(this);
    ...
    }

    TimerViewAssistant.prototype.startTimer = function (event)
    {
    ...
    this.interval = window.setInterval(this.updateTimerHandler, 1000);
    ...
    }

    TimerViewAssistant.prototype.updateTimer = function(event)
    {

    var appController, bannerParams;
    this.seconds -= 1;
    this.displayClock();
    if (this.seconds < 1) {
    this.stopTimer();
    appController = Mojo.Controller.getAppController();
    bannerParams = {
    messageText: "Time is up! " + this.startedTimer,
    soundClass: "alerts"
    };
    appController.showBanner(bannerParams, {},
    "timerStopped");
    }
    }
  10. shawalli's Avatar
    Posts
    83 Posts
    Global Posts
    85 Global Posts
    #10  
    man, I love the precentral dev forums. I was having problems with my setTimeout constantly firing. 1 google search later, I scanned this page and realized I forgot to bind my event handler. It's the little things like this that make webos' dev community like no other on the planet.
  11. #11  
    Quote Originally Posted by Blubble View Post
    Also, there is no need to use var self = this;. It's an unnecessary extra line of code.
    In Oil's example it actually is necessary. When you set an anonymous function like that you change the scope. The anonymous function is not a method of WhateverAssistant.prototype, so 'this' doesn't point there. That said, using a .bind(this) handle is much more efficient than using an anonymous function.
    danNielson.com - Slalom - twitter - facebook
    A posted thanks dies with the thread. Clicking the button lasts until the admins get bored with this site and throw it in the recycling bin.

Posting Permissions