pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

pastebin - collaborative debugging tool View Help


Posted by Anonymous on Tue 18 Aug 21:57 (modification of post by view diff)
report abuse | download | new post

  1. function FirstAssistant(levelchoice) {
  2.     /* this is the creator function for your scene assistant object. It will be passed all the
  3.            additional parameters (after the scene name) that were passed to pushScene. The reference
  4.            to the scene controller (this.controller) has not be established yet, so any initialization
  5.            that needs the scene controller should be done in the setup function below. */
  6.     this.levelchoice = levelchoice
  7. }
  8.      
  9. FirstAssistant.prototype.setup = function (choice) {
  10.     /* this function is for setup tasks that have to happen when the scene is first created */
  11.     try{
  12.        
  13.        
  14.         this.skinCookie = new Mojo.Model.Cookie("skinCookie");
  15.     document.getElementById('bg').style.backgroundImage = 'url("images/' + this.skinCookie.get() + '/bg.png")'
  16.  
  17.     this.bricks = new Array(40); //set up for 5 rows of 8 bricks, don't all have to be used becuase arrays are dynamic
  18.     this.bricksleft = 0;
  19.     this.fieldwidth = 320; // if in the future landscape mode is enable, change.
  20.     this.fieldheight  = 460; //how tall should the field be, probably will be changed
  21.     //this.columns = 8; //fully adjustable, just change this value for columns and the rest will follow
  22.     this.rows = 5;
  23.     this.brickwidth = this.fieldwidth / this.columns; // bricks per row
  24.     this.brickheight = 25; // experimental value, can be adjusted a later
  25.     this.speedCookie = new Mojo.Model.Cookie("speedCookie");
  26.     this.speed = this.speedCookie.get();
  27.     this.speed = this.speed *3
  28.     this.ballfunc = this.ball.bind(this);
  29.     //this.drawballfunc = this.drawball.bind(this);
  30.    
  31.     this.radius = 8; //this will be used to account for javascript positioning the ball image according to the top left corner rather than center
  32.     $('ball').style.width = this.radius*2 + "px";
  33.     $('ball').style.height = this.radius*2 + "px";
  34.     this.z = 0 //the "Z" variable is to hold the count of the ball interval
  35.    
  36.    
  37.     this.paddlewidth = 80;
  38.     this.paddleheight = 15;
  39.     this.paddlex = 110;
  40.     this.paddley       = 380; // where does the paddle live in the fieldheight?  
  41.     $('paddle').style.width = this.paddlewidth + "px";
  42.     $('paddle').style.height = this.paddleheight + "px";
  43.     $('paddle').style.left = this.paddlex + "px";
  44.     $('paddle').style.top = (this.paddley) + "px";
  45.    
  46.     $('flickUp').style.left = 10 + "px";
  47.     $('flickUp').style.top = (this.paddley - 40) + "px";
  48.     this.score = 0;
  49.     //$('lives').style.left = 10 + "px";
  50.     //$('lives').style.top = (this.fieldheight / 2-25) + "px";
  51.     $('lives').innerHTML = 'Lives: ' + (this.lives);
  52.     //$('score').style.left = 145 + "px";
  53.     //$('score').style.top = (this.fieldheight / 2-25) + "px";
  54.     $('score').innerHTML = 'Score: ' + (this.score);
  55.    
  56.     if (this.skinCookie.get()=="fade" || this.skinCookie.get()=="beatles"){
  57.         $('lives').style.color = "black"
  58.         $('score').style.color = 'black'
  59.         $('level').style.color = "black"
  60.     }
  61.     //$('level').style.left = 80 + "px";
  62.     //$('level').style.top = (this.fieldheight / 2+10) + "px";
  63.     this.dragStartHandler = this.dragStart.bindAsEventListener(this);
  64.     this.draggingHandler = this.dragging.bindAsEventListener(this);
  65.     this.dragEndHandler = this.dragEnd.bindAsEventListener(this);
  66.     this.flickHandler = this.handleFlick.bindAsEventListener(this);
  67.     if (this.levelchoice!== undefined){
  68.         this.start(this.levelchoice)
  69.     }
  70.     else {
  71.         this.level2Cookie = new Mojo.Model.Cookie("level2Cookie");
  72.         this.start(this.level2Cookie.get())
  73.     }
  74.  
  75.     /* use Mojo.View.render to render view templates and add them to the scene, if needed. */
  76.     //window.setTimeout(this.start.bind(this), 1500); // in the future this will be tied to a button pree or some other even so the game doesnt start on load
  77.     /* setup widgets here */
  78.     /* add event handlers to listen to events from widgets */
  79.     this.trackingArea = this.controller.get('tracking-area'); // if this is commented out the user cant hit anywhere on screen to begin moving the paddle
  80.     this.trackingArea2 = this.controller.get('paddle');
  81.     this.trackingArea3 = this.controller.get('info')
  82.    
  83.     Element.observe(this.trackingArea, Mojo.Event.flick, this.flickHandler)
  84.    }catch(e){$('error').innerHTML = e}
  85. };
  86.  
  87. FirstAssistant.prototype.handleFlick = function (event) {
  88.    //$('error').innerHTML = 'x:' + event.velocity.x + 'y:' + event.velocity.y
  89.    if(event.velocity.y<0){
  90.     var r = this.speed/Math.sqrt(event.velocity.x*event.velocity.x +event.velocity.y*event.velocity.y)
  91.     this.speedx=event.velocity.x*r
  92.    
  93.     this.speedy = -Math.round(Math.sqrt(this.speed*this.speed - (this.speedx * this.speedx)))
  94.     //$('error').innerHTML = Math.sqrt(this.speedx*this.speedx + this.speedy*this.speedy)
  95.     //this.drawtimer = window.setInterval(this.drawballfunc, 35);
  96.     this.balltimer = window.setInterval(this.ballfunc, 50);
  97.     $('flickUp').style.visibility = "hidden";
  98.    this.redeploy()
  99.    Element.observe(this.trackingArea, Mojo.Event.dragStart, this.dragStartHandler);
  100.    Element.observe(this.trackingArea2, Mojo.Event.dragStart, this.dragStartHandler);
  101.    Element.observe(this.trackingArea3, Mojo.Event.dragStart, this.dragStartHandler);
  102.    Element.stopObserving(this.trackingArea, Mojo.Event.flick, this.flickHandler)
  103.     }
  104.  
  105. }
  106. FirstAssistant.prototype.getLevel = function (level) {
  107.         try {
  108.         //var level = 0;    
  109.         this.levelLoad = new Levels()
  110.        
  111.         this.meta       = this.levelLoad.levelMeta[level]
  112.        
  113.             $('level').innerHTML = level;
  114.  
  115.         this.bricksleft = this.meta[2];
  116.        
  117.         this.level      = level;
  118.        
  119.         this.columns    = this.meta[0];
  120.         this.rows       = this.meta[1];
  121.         this.brickwidth = this.fieldwidth / this.columns;
  122.        
  123.         this.grid       = this.levelLoad.levelGrid[level];
  124.         }catch (e){$('error').innerHTML=e;}
  125.             for (n = 0; n <= (this.columns*this.rows); n++) {
  126.                 this.bricks[n] = this.grid[n];
  127.                
  128.             }
  129.         $('level').innerHTML = 'Level: ' + (this.level+1);
  130.         this.skinCookie = new Mojo.Model.Cookie("skinCookie");
  131.  
  132.  
  133. };
  134.  
  135. FirstAssistant.prototype.drawLevel = function () {
  136.     var rh = "";
  137.     for (n = 0; n <= (this.rows - 1); n++) {
  138.         for (i = 0; i <= (this.columns - 1); i++) {
  139.             if (this.bricks[n * this.columns + i] !== 0) { //checks to see if brick should be drawn
  140.                 rh = rh + '<img ' + 'id=' + '"r' + n + 'c' + i + '"' +'src="images/'+this.skinCookie.get()+'/'+this.bricks[n*this.columns+i]+'.png"' + 'style="' + 'position:absolute;' + 'z-index:7;' + ' width:' + this.brickwidth + 'px;' + 'height:' + this.brickheight + 'px;' + ' top:' + (this.brickheight * n) + 'px;' + ' left:' + (this.brickwidth * i) + 'px;' + '"' + '/>';
  141.             }
  142.            
  143.         }
  144.     }
  145.     $('bricks').innerHTML = rh;
  146. };
  147.  
  148. FirstAssistant.prototype.start = function (level) {
  149.    
  150.    
  151.     //$('error').innerHTML = "levelCookie set to:" + (this.levelCookie.get()+1);
  152.     this.getLevel(level);
  153.     this.drawLevel();
  154.    
  155.     this.ballx = 20;
  156.     this.bally = this.paddley
  157.     $('ball').style.left = this.ballx + "px";
  158.     $('ball').style.top = this.bally + "px";
  159.    
  160.     this.lives = 3;
  161.    
  162.    
  163.     $('lives').innerHTML = 'Lives: ' + (this.lives);
  164.     $('score').innerHTML = 'Score: ' + (this.score);
  165. };
  166.  
  167.  
  168. FirstAssistant.prototype.ball = function () {
  169.     this.ballx = this.ballx + this.speedx;
  170.     this.bally = this.bally + this.speedy;
  171.     //if (this.bally>(this.rows+1)* this.brickheight){
  172.         this.checkcollision();
  173.     //}
  174.     if (this.bally <= 0) {
  175.         if (this.bally<0){
  176.             this.bally=-this.bally
  177.         }
  178.         this.speedy = -this.speedy;
  179.     }
  180.     if ((this.bally >= (this.paddley - this.radius * 2))
  181.         && (this.bally <= (this.paddley + this.brickwidth))
  182.         && (this.ballx >= (this.paddlex - this.radius * 2)
  183.         && this.ballx <= (this.paddlex + this.paddlewidth)) ) { //bounce if touching paddle.
  184.             this.bally = this.paddley-this.radius*2
  185.             this.speedx = 1.5*this.speed * ((this.ballx+ this.radius) - (this.paddlex + this.paddlewidth / 2 )) / (this.radius * 2 + this.paddlewidth); //
  186.             this.speedy = -Math.sqrt(this.speed * this.speed - this.speedx * this.speedx);
  187.     }
  188.     if (this.bally >= (this.paddley + (this.radius * 14))) {  // lose ball if 8 ball heights below paddle. used basically as a timeout
  189.             //this.speedx = 0;
  190.             //this.speedy = 0;
  191.             //this.bally = this.bally - 3;
  192.             try{
  193.                 $('ball').style.left = this.ballx + "px";
  194.                 $('ball').style.top = this.bally + "px";
  195.                 this.ballx = 20;
  196.                 this.bally = this.paddley
  197.                 $('flickUp').style.visibility = 'visible';
  198.                 $('ball').style.left = this.ballx + "px";
  199.                 $('ball').style.top = this.bally + "px";
  200.                 this.lives--
  201.                 $('lives').innerHTML = 'Lives: ' + (this.lives);
  202.                 window.clearInterval(this.balltimer)
  203.                 window.clearInterval(this.drawtimer)
  204.                 Element.stopObserving(this.trackingArea, Mojo.Event.dragStart, this.dragStartHandler);
  205.                 Element.stopObserving(this.trackingArea2, Mojo.Event.dragStart, this.dragStartHandler);
  206.                 Element.stopObserving(this.trackingArea3, Mojo.Event.dragStart, this.dragStartHandler);
  207.  
  208.                 Element.observe(this.trackingArea, Mojo.Event.flick, this.flickHandler)
  209.                 if (this.lives <= 0) {
  210.                 this.gameover();
  211.                 //break;
  212.                 } else {
  213.                 //lost ball, play bzzzzt sound, and redeploy.  
  214.                     this.controller.serviceRequest('palm://com.palm.audio/systemsounds', {
  215.                             method:"playFeedback",
  216.                             parameters:{name: 'error_03'},
  217.                             onSuccess:{},
  218.                             onFailure:{}
  219.                     });
  220.                 }
  221.             }catch(e){$('error').innerHTML = e}
  222.     }   
  223.     if (this.ballx <= 0 || this.ballx >= (this.fieldwidth - (this.radius * 2))) {
  224.         this.speedx = -this.speedx;
  225.         if (this.ballx < 0) {
  226.             this.ballx = -this.ballx;
  227.         }
  228.         else if (this.ballx > (this.fieldwidth - (this.radius * 2))) {
  229.             this.ballx = (this.fieldwidth - (this.radius * 2)) - (this.ballx - (this.fieldwidth - (this.radius * 2)));
  230.         }
  231.     }
  232.     //this.z++
  233.     //if (this.z == 2){
  234.         //this.z = 0;
  235.     $('ball').style.top = this.bally + "px";
  236.     $('ball').style.left = this.ballx + "px";
  237.     $('paddle').style.left = this.paddlex + "px";
  238.     /*this.x++
  239.     this.currentTime= new Date()
  240.     $('error').innerHTML =(this.x/(this.currentTime-this.startTime)*1000);*/    
  241. };
  242. FirstAssistant.prototype.redeploy = function () {
  243.    //this.x=0
  244.    //this.startTime = new Date ()
  245.    
  246.    
  247.     //JRB as written if you don't move the paddle, the game is always exactly the same.
  248.     // that's at a minimum boring. so, replace the 1 in speedx with a random number from 0 to 5
  249.    // this.speedx = -Math.floor(Math.random() * this.speed); //this.speedx = 1; //how many pixels the ball will move in the x direction each time it's position is calculate
  250.     //JRB which of course requires a more complex definition for y...
  251.    // this.speedy = -Math.round(Math.sqrt(this.speed*this.speed - (this.speedx * this.speedx))); //
  252. };
  253.  
  254. FirstAssistant.prototype.checkcollision = function () {
  255.     var c = Math.floor((this.ballx + this.radius) / this.brickwidth); //what column the ball is in
  256.     var r = Math.floor((this.bally + this.radius) / this.brickheight); //what row the ball is in
  257.     for (n = 0; n <= (this.rows - 1); n++) {
  258.         var b = n * this.columns + c; //which brick is being tested
  259.         if (this.bally <= ((n + 1) * this.brickheight) && this.bally >= (n * this.brickheight) && this.bricks[b] !== 0 || this.bally+this.radius*2<=((n+1)*this.brickheight) && this.bally+this.radius*2 >= (n*this.brickheight) && this.bricks[b]!==0){
  260.         this.collisionOccured('y',b,c);
  261.         }
  262.        
  263.         else if (this.ballx<=this.brickwidth*(c-1) && this.ballx>=this.brickwidth*(c) && this.bricks[b-1]!==0 && r==(b-c)/this.columns){
  264.             this.collisionOccured('x',b-1,c-1);
  265.         }
  266.         else if ((this.ballx+this.radius*2)<=this.brickwidth*(c+2) && (this.ballx+this.radius*2)>=this.brickwidth*(c+1) && this.bricks[b+1]!==0 && r==(b-c)/this.columns){
  267.             this.collisionOccured('x',b+1,c+1);
  268.         }
  269.     }
  270. };
  271. FirstAssistant.prototype.collisionOccured = function (axis,brick, column){
  272.     this.r = (brick-column)/this.columns // what row
  273.     if (axis=='y'){
  274.         if (this.speedy>0){
  275.             this.bally =  this.brickheight*(this.r)-this.radius*2
  276.         }
  277.         else {
  278.             this.bally =  this.brickheight*(this.r +1)  
  279.         }
  280.         this.speedy=-this.speedy;
  281.     }
  282.     else {
  283.         if (this.speedx>0){
  284.                     this.ballx = this.brickwidth * column -this.radius*2
  285.  
  286.         }
  287.         else {
  288.                     this.ballx = this.brickwidth * (column+1)
  289.  
  290.         }
  291.         this.speedx=-this.speedx;
  292.        
  293.     }
  294.  
  295.     this.bricks[brick]= this.bricks[brick]-1; //takes away one hit     
  296.        
  297.     if (this.bricks[brick]>0){
  298.        
  299.         $('r'+this.r+'c'+column).src='"images/'+this.skinCookie.get()+'/' + this.bricks[brick] + '.png"';
  300.     }       
  301.     if (this.bricks[brick] === 0) {
  302.         $('r' + n + 'c' + column).style.visibility = 'hidden';
  303.         this.bricksleft--;
  304.         this.score = this.score + 10;
  305.         $('score').innerHTML = 'Score: ' + (this.score);
  306.             if (this.bricksleft === 0) {
  307.                 if (this.levelLoad.levelMeta[this.level2Cookie.get()+1]===undefined){
  308.                    this.beatGame()
  309.                 }
  310.                 this.winner();
  311.             }
  312.     }
  313. };
  314.  
  315. FirstAssistant.prototype.dragStart = function (event) {
  316.     this.paddlex = Event.pointerX(event.down) - (this.paddlewidth / 2);
  317.     $('paddle').style.left = this.paddlex + "px";
  318.     Element.observe(this.trackingArea, Mojo.Event.dragging, this.draggingHandler);
  319.     Element.observe(this.trackingArea, Mojo.Event.dragEnd, this.dragEndHandler);
  320.     Element.observe(this.trackingArea2, Mojo.Event.dragging, this.draggingHandler);
  321.     Element.observe(this.trackingArea2, Mojo.Event.dragEnd, this.dragEndHandler);
  322.     Element.observe(this.trackingArea3, Mojo.Event.dragging, this.draggingHandler);
  323.     Element.observe(this.trackingArea3, Mojo.Event.dragEnd, this.dragEndHandler);
  324.     Event.stop(event);
  325. };
  326. FirstAssistant.prototype.dragging = function (event) {
  327.     this.paddlex = Event.pointerX(event.move) - (this.paddlewidth / 2);
  328.     if (parseInt(this.paddlex) < 0) {
  329.         this.paddlex = 0
  330.     };
  331.     if (parseInt(this.paddlex) > (this.fieldwidth - this.paddlewidth)) {
  332.         this.paddlex = this.fieldwidth - this.paddlewidth;
  333.        
  334.     }
  335.    
  336.     Event.stop(event);
  337. };
  338. FirstAssistant.prototype.dragEnd = function (event) {
  339.     Element.stopObserving(this.trackingArea, Mojo.Event.dragging, this.draggingHandler);
  340.     Element.stopObserving(this.trackingArea2, Mojo.Event.dragEnd, this.dragEndHandler);
  341.     Element.stopObserving(this.trackingArea3, Mojo.Event.dragEnd, this.dragEndHandler);
  342.  
  343.     Event.stop(event);
  344. };
  345.  
  346.  
  347. FirstAssistant.prototype.activate = function (event) {
  348.     /* put in event handlers here that should only be in effect when this scene is active. For
  349.            example, key handlers that are observing the document */
  350. ;
  351. };
  352. FirstAssistant.prototype.deactivate = function (event) {
  353.     /* remove any event handlers you added in activate and do any other cleanup that should happen before
  354.            this scene is popped or another scene is pushed on top */
  355.     window.clearInterval(this.balltimer);
  356. };
  357. FirstAssistant.prototype.cleanup = function (event) {
  358.     /* this function should do any cleanup needed before the scene is destroyed as
  359.            a result of being popped off the scene stack */
  360. };
  361.    
  362. //JRB this is a very very very ugly tool, but it's the only one I know of that
  363. // will do this.
  364. function pausecomp(millis)
  365. {
  366.     var date = new Date();
  367.     var curDate = null;
  368.     do { curDate = new Date(); }
  369.     while(curDate-date < millis);
  370. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me so that I can delete my post