Ver Mensaje Individual
  #1 (permalink)  
Antiguo 24/05/2009, 14:32
Avatar de pato12
pato12
 
Fecha de Ingreso: septiembre-2007
Ubicación: Salta
Mensajes: 1.620
Antigüedad: 17 años, 1 mes
Puntos: 101
Problema con velocidad

Hola,
Tengo este codigo de un juego de snake y no se como bajarle la velocidad:
Código actionscript:
Ver original
  1. stop();
  2. // Snake Game by Strille, 2004, www.strille.net
  3.  
  4. blockSize = 15;   // the block width/height in number of pixels
  5. gameHeight = 16; // the game height in number of blocks
  6. gameWidth  = 33; // the game width in number of blocks
  7. SNAKE_BLOCK = 15; // holds the number used to mark snake blocks in the map
  8. SCORE_X_BLOCK = 40;
  9. score = 0; // keeps track of the score
  10. snakeBlockCounter = 0; // keeps track of the snake blocks, increased on every frame
  11. foodCounter = 0;
  12. snakeEraseCounter = -1; // increased on every frame, erases the snake tail (setting this to -3 will result in a 3 block long snake at the beginning)
  13. VIDAS = 3;
  14. vida.gotoAndStop('v3');
  15.  
  16. keyListener = new Object(); // key listener
  17. keyListener.onKeyDown = function() {
  18.     var keyCode = Key.getCode(); // get key code
  19.    
  20.     if (keyCode > 36 && keyCode < 41) { // arrow keys pressed (37 = left, 38 = up, 39 = right, 40 = down)...
  21.         if (game.onEnterFrame != undefined) { // only allow moves if the game is running, and is not paused
  22.             if (keyCode-37 != turnQueue[0]) { // ...and it's different from the last key pressed
  23.                 turnQueue.unshift(keyCode-37); // save the key (or rather direction) in the turnQueue
  24.             }
  25.         }
  26.     } else if (keyCode == 32) { // start the game if it's not started (32 = SPACE)
  27.         if (!gameRunning) {
  28.             startGame();
  29.         }
  30.     } else if (keyCode == 80) { // pause/unpause (80 = 'P')
  31.         if (gameRunning) {
  32.             if (game.onEnterFrame) { // pause
  33.                 delete game.onEnterFrame; // remove main loop
  34.                 textMC.gotoAndStop("paused");
  35.             } else { // exit pause mode
  36.                 game.onEnterFrame = main; // start main loop
  37.                 textMC.gotoAndStop("hide");
  38.             }
  39.         }
  40.     }
  41. };
  42. Key.addListener(keyListener);
  43.  
  44. if (!gameRunning) {
  45.     startGame();
  46. }
  47.  
  48.  
  49. function startGame() {
  50.     x = int(gameWidth/2); // x start position in the middle
  51.     y = gameHeight-2;     // y start position near the bottom
  52.    
  53.     xVelocity = [-1, 0, 1, 0]; // x velocity when moving left, up, right, down
  54.     yVelocity = [0, -1, 0, 1]; // y velocity when moving left, up, right, down
  55.    
  56.     map = new Array(); // create an array to store food and snake
  57.     for (var n=0;n<gameWidth;n++) { // make map a 2 dimensional array
  58.         map[n] = new Array();
  59.     }
  60.  
  61.     turnQueue = new Array(); // a queue to store key presses (so that x number of key presses during one frame are spread over x number of frames)
  62.    
  63.     game.createEmptyMovieClip("food", 1); // create MC to store the food
  64.     game.createEmptyMovieClip("s", 2); // create MC to store the snake
  65.    
  66.  
  67.    
  68.     currentDirection = 1; // holds the direction of movement (0 = left, 1 = up, 2 = right, 3 = down)
  69.    
  70.     if(VIDAS<=0){
  71.         score = 0; // keeps track of the score
  72.         snakeBlockCounter = 0; // keeps track of the snake blocks, increased on every frame
  73.         foodCounter = 0;
  74.         snakeEraseCounter = -1; // increased on every frame, erases the snake tail (setting this to -3 will result in a 3 block long snake at the beginning)
  75.         VIDAS = 3;
  76.         vida.gotoAndStop('v3');
  77.     }
  78.     scoreTextField.text = "Puntos: "+score+" | Vidas: "+VIDAS; // type out score info
  79.     placeFood("new"); // place a new food block
  80.        
  81.    
  82.     textMC.gotoAndStop("hide"); // make sure no text is visible (like "game over ")
  83.     game.onEnterFrame = main; // start the main loop
  84.     gameRunning = true; // flag telling if the game is running. If true it does not necessarily mean that main is called (the game could be paused)
  85. }
  86.  
  87.  
  88. function main() { // called on every frame if the game is running and it's not paused
  89.     if (turnQueue.length > 0) { // if we have a turn to perform...
  90.         var dir = turnQueue.pop(); // ...pick the next turn in the queue...
  91.         if (dir % 2 != currentDirection % 2) { // not a 180 degree turn (annoying to be able to turn into the snake with one key press)
  92.             currentDirection = dir; // change current direction to the new value
  93.         }
  94.     }
  95.    
  96.     x += xVelocity[currentDirection]; // move the snake position in x
  97.     y += yVelocity[currentDirection]; // move the snake position in y
  98.    
  99.     if (map[x][y] != SNAKE_BLOCK && x > -1 && x < gameWidth && y > -1 && y < gameHeight) { // make sure we are not hitting the snake or leaving the game area
  100.         game.s.attachMovie("snakeMC", snakeBlockCounter, snakeBlockCounter, {_x: x*blockSize, _y: y*blockSize}); // attach a snake block movie clip
  101.         snakeBlockCounter++; // increase the snake counter
  102.    
  103.         if (typeof(map[x][y]) == "movieclip") { // if it's a not a vacant block then there is a food block on the position
  104.             score += SCORE_X_BLOCK; // add points to score
  105.             scoreTextField.text = "Puntos: "+score+" | Vidas: "+VIDAS; // type out score info
  106.             snakeEraseCounter -= 1; // make the snake not remove the tail for five loops
  107.             placeFood(map[x][y]); // place the food movie clip which is referenced in the map map[x][y]
  108.         }
  109.        
  110.         map[x][y] = SNAKE_BLOCK; // set current position to occupied
  111.    
  112.         var tailMC = game.s[snakeEraseCounter]; // get "last" MC according to snakeEraseCounter (may not exist)
  113.         if (tailMC) { // if the snake block exists
  114.             delete map[tailMC._x/blockSize][tailMC._y/blockSize]; // delete the value in the array m
  115.             tailMC.removeMovieClip(); // delete the MC
  116.         }
  117.         snakeEraseCounter++; // increase erase snake counter   
  118.     } else { // GAME OVER if it is on a snake block or outside of the map
  119.         gameOver();
  120.     }
  121. }
  122.  
  123.  
  124. function gameOver() {
  125.     VIDAS--;
  126.     if(VIDAS<=0){
  127.         textMC.gotoAndStop("gameOver"); // show "game over" text
  128.     }else{
  129.         textMC.gotoAndStop("vidamenos");
  130.         vida.gotoAndPlay('v'+VIDAS);
  131.     }
  132.     delete game.onEnterFrame; // quit looping main function
  133.     gameRunning = false; // the game is no longer running
  134.    
  135. }
  136.  
  137.  
  138. function placeFood(foodMC) {
  139.     do {
  140.         var xFood = random(gameWidth);
  141.         var yFood = random(gameHeight);
  142.     } while (map[xFood][yFood]); // keep picking a spot until it's a vacant spot (we don't want to place the food on a position occupied by the snake)
  143.    
  144.     if (foodMC == "new") { // create a new food movie clip
  145.         foodMC = game.food.attachMovie("foodMC", foodCounter, foodCounter);
  146.         foodCounter++;
  147.     }
  148.    
  149.     foodMC._x = xFood*blockSize; // place the food
  150.     foodMC._y = yFood*blockSize; // place the food
  151.    
  152.     map[xFood][yFood] = foodMC; // save a reference to this food movie clip in the map
  153. }
Gracias
Salu2
__________________
Half Music - www.halfmusic.com