Archive for the 'Actionscript Code' Category

A PHP ByteArray class that works like the one in as3

A lot of the data being sent from flash to php is just bytes to save bandwidth, so I wanted a php class that worked simular to the ByteArray class in actionscript 3. So I made this one.

Cheers

  1. <?php
  2. /**
  3.  * ByteArray class
  4.  *
  5.  * Should work close to the ActionScript 3 ByteArray class
  6.  *
  7.  * Copyright (c) 2009, Mikael Brandt.
  8.  * This code is free: you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation, either version 3 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this code.  If not, see <http://www.gnu.org/licenses/>.
  20.  *
  21.  * @author Mikael Brandt <mikaelbrandt@gmail.com> <www.mikaelbrandt.com>
  22.  * @version 1.0
  23.  *
  24.  * @copyright Copyright (c) 2009, Mikael Brandt.
  25.  * @license GNU General Public License
  26.  */
  27.  
  28. class ByteArray {
  29.  
  30.  /**
  31.   * The bytes.
  32.   *
  33.   * @var array
  34.   */
  35.  private $_bytes;
  36.  
  37.  /**
  38.   * Current byte stream position.
  39.   *
  40.   * @var int
  41.   */
  42.  public $position;
  43.  
  44.  /**
  45.   * Default constructor.
  46.   *
  47.   * @return void
  48.   * @param array[optional] $bytes
  49.   */
  50.  public function __construct($bytes = array())
  51.  {
  52.   $this->_bytes = $bytes;
  53.  
  54.   $this->position = 0;
  55.  }
  56.  
  57.  /**
  58.   * Get count of bytes in the byte stream.
  59.   *
  60.   * @return int
  61.   */
  62.  public function length()
  63.  {
  64.   return count($this->_bytes);
  65.  }
  66.  
  67.  /**
  68.   * Get the bytes left from the current position to the end of the byte array.
  69.   *
  70.   * @return int
  71.   */
  72.  public function bytesAvailable()
  73.  {
  74.   return count($this->_bytes) - $this->position;
  75.  }
  76.  
  77.  /**
  78.   * Replace the current position to a new value. If jump is true, then
  79.   * jump to the next position in the byte stream.
  80.   *
  81.   * @return void
  82.   * @param int[optional] $value
  83.   * @param boolean[optional] $jump
  84.   */
  85.  public function changeto($value = 0, $jump = false)
  86.  {
  87.   $this->_bytes[$this->position] = chr($value);
  88.  
  89.   if ($jump) $this->position++;
  90.  }
  91.  
  92.  /**
  93.   * Get the byte stream as a string.
  94.   *
  95.   * @return string
  96.   */
  97.  public function toString()
  98.  {
  99.   return implode($this->_bytes);
  100.  }
  101.  
  102.  /**
  103.   * Get the byte stream as base64.
  104.   *
  105.   * @return string
  106.   */
  107.  public function base64()
  108.  {
  109.   return base64_encode($this->toString());
  110.  }
  111.  
  112.  /**
  113.   * Reads a byte from the byte stream.
  114.   *
  115.   * @return int
  116.   */
  117.  public function readByte()
  118.  {
  119.   $returnByte = ord($this->_bytes[$this->position]);
  120.  
  121.   $this->position++;
  122.  
  123.   return $returnByte;
  124.  }
  125.  
  126.  /**
  127.   * Reads a 16-bit integer from the byte stream.
  128.   *
  129.   * @return int
  130.   */
  131.  public function readShort()
  132.  {
  133.   $returnShort = (ord($this->_bytes[$this->position])<<8) |
  134.       (ord($this->_bytes[$this->position+1]));
  135.  
  136.   $this->position +=2;
  137.  
  138.   return $returnShort;
  139.  }
  140.  
  141.  /**
  142.   * Reads a 32-bit integer from the byte stream.
  143.   *
  144.   * @return int
  145.   */
  146.  public function readInt()
  147.  {
  148.   $returnInt = (ord($this->_bytes[$this->position])<<24) |
  149.       (ord($this->_bytes[$this->position+1])<<16) |
  150.       (ord($this->_bytes[$this->position+2])<<8) |
  151.       (ord($this->_bytes[$this->position+3]));
  152.  
  153.   $this->position +=4;
  154.  
  155.   return $returnInt;
  156.  }
  157.  
  158.  /**
  159.   * Write a value to the byte stream at the current position.
  160.   *
  161.   * @return void
  162.   * @param int[optional] $value
  163.   */
  164.  public function writeByte($value = 0)
  165.  {
  166.   array_splice($this->_bytes, $this->position, 0, chr($value));
  167.  
  168.   $this->position++;
  169.  }
  170.  
  171.  /**
  172.   * Writes a 16-bit integer to the byte stream at the current position.
  173.   *
  174.   * @return void
  175.   * @param int[optional] $value
  176.   */
  177.  public function writeShort($value = 0)
  178.  {
  179.   array_splice(
  180.    $this->_bytes,
  181.    $this->position,
  182.    0,
  183.    array(
  184.     chr(($value >> 8) & 0xff),
  185.     chr($value & 0xff)
  186.    )
  187.   );
  188.  
  189.   $this->position += 2;
  190.  }
  191.  
  192.  /**
  193.   * Writes a 32-bit integer to the byte stream at the current position.
  194.   *
  195.   * @return void
  196.   * @param int[optional] $value
  197.   */
  198.  public function writeInt($value = 0)
  199.  {
  200.   array_splice(
  201.    $this->_bytes,
  202.    $this->position,
  203.    0,
  204.    array(
  205.     chr(($value >> 24) & 0xff),
  206.     chr(($value >> 16) & 0xff),
  207.     chr(($value >> 8) & 0xff),
  208.     chr($value & 0xff)
  209.    )
  210.   );
  211.  
  212.   $this->position += 4;
  213.  }
  214. }
  215. ?>

Main loop actionscript optimizing

Hello,

Allthough the engine runs fast on all computers Iv tested it on, Iv now started optimizing the code to get it to run faster. Then maybe I will be able to use even more particle effects and such. I though ill start posting some code on this blog in case someone is interested.

So, the first thing iv done is to get the main loop faster. It loops through every tile on the map every frame. So if you have a map with 100 horisontal tiles and 100 vertical tiles you have a total of 10 000 tiles, and those are looped through 30 times a second, so 300 000. So to get the main loop faster will make some diffrence.

At first the loop looked like this:

I run each loop 100 times with 100×100 tiles, tileArray is the array holding all the tiles

  1. // class Render
  2. var e:Element;
  3. for (var i:int = 0; i < this.tileArray.length; i++)
  4. {
  5.  for (var u:int = 0; u < this.tileArray[0].length; u++)
  6.  {
  7.   e = this.tileArray[i][u] as Element;
  8.   if (!e.empty)
  9.   {
  10.    e.update();
  11.   }
  12.  }
  13. }

This took: 1842ms

  1. // class Render
  2. var xlength:int = this.tileArray[0].length;
  3. var ylength:int = this.tileArray.length;
  4. var e:Element;
  5. for (var i:int = 0; i < ylength; i++)
  6. {
  7.  for (var u:int = 0; u < xlength; u++)
  8.  {
  9.   e = this.tileArray[i][u] as Element;
  10.   if (!e.empty)
  11.   {
  12.    e.update();
  13.   }
  14.  }
  15. }

This took: 1520ms, some improvment!

  1. // class Render
  2. for each(var a:Array in this.tileArray)
  3. {
  4.  for each(var e:Element in a) {
  5.   if (!e.empty)
  6.   {
  7.    e.update();
  8.   }
  9.  }
  10. }

This is what I ended up with after some testing, and it took 1422ms, a bit faster though the the best part is that the code looks much better ;)

Here is two pages i found about this subject

Seven tips about actionscript optimizing at gamedevjuice.wordpress.com
Optimizing actionscript 3 at www.nbilyk.com