Archive for the ‘how to’ Category

h1

How to set styles on text input fields in ActionScript 3.0

January 23, 2010

Problem: Setting the text style on an input TextField doesn’t work as expected; for example,


...
var t:TextField = new TextField();
var f:TextFormat = new TextFormat();
...
t.type = TextFieldType.INPUT;
f.font = "Arial";
f.size = 20;
t.setTextFormat(f);
addChild(t);
...

Text typed into the text field appears in the default serif font, not in Arial 20.

Solution You can’t style user input or replaced text in Actionscript 3.0 with setTextFormat, instead, you have to use defaultTextFormat. Just replace the line that calls setTextFormat with


t.defaultTextFormat = f;

and the input field will use the text format in f correctly. This won’t work if you’re using an ActionScript 3.0 style sheet with the textfield, though.

Advertisements
h1

Build your own burrito battery

November 20, 2007

Up until now, the best way to MacGyver a battery has been to stick a copper wire and a galvanized nail into a lemon (or an apple, or a potato, or…). Fruit or vegetable batteries produce a respectable voltage (around a volt) but only a tiny amount of current- not enough to light a flashlight bulb.

Now there’s the “burrito” battery. It produces 400 times as much current as a lemon battery- enough to actually light a little holiday lightbulb, or run a small DC motor. All you need is some duct tape. And a piece of aluminum foil, some table salt, a paper towel, some activated charcoal, and some copper wire to use for leads. You make a “burrito” with the aluminum foil as the tortilla. Line the foil with the paper toil, moistened with salt water. Fill the burrito with activated charcoal, also moistened saturated salt water. Sink one copper lead into the charcoal and duct-tape the other to the aluminum foil and voila! You should have about 1 V, with 100 milliamperes of current. Attach several cells in parallel for more current, and in series for more voltage!

You can find a step-by-step guide for building the battery at http://www.exo.net/~pauld/activities/AlAirBattery/alairbattery.html. The authors of that page have published a nice classroom activity sheet to go along with the battery in the December 2007 issue of the Journal of Chemical Education (M. Tamez, J. H. Yu, J. Chem. Ed. 2007, 84, 1936A-1937A).

How does it work?

All batteries work by running chemical reactions that release electrons in one place, and capture electrons in another. When you run a wire between these two places, an electric current flows through it. You can tap off some of the energy of that current to light a bulb or drive a motor.

In the aluminum-air battery, the aluminum in the foil is the electron source. Aluminum on the surface of the foil reacts with hydroxide ions in the salt water to form aluminum hydroxide. Every aluminum atom that reacts releases three electrons into the foil:

Al(s) + 3OH(aq) –> Al(OH)3(s) + 3e

This is called an oxidation (a reaction that involves a loss of electrons).

Air absorbed in the nooks and crannies of the activated charcoal acts as the electron sink. Specifically, oxygen gas captures electrons using this half reaction:

O2(g) + 2H2O(l) + 4e –> 4OH(aq)

This is called a reduction (a reaction that involves a gain of electrons). Notice that the activated charcoal doesn’t actually do anything in this reaction. It just provides a nice place for the reaction to occur. It has a high surface area, so it is in contact with a lot of oxygen molecules. It is also able to shuttle electrons from the copper wire to oxygen absorbed on its surface.

The battery won’t work unless the oxidation and reduction reactions can work together. By itself, the foil will build up a positive charge as it loses electrons. It’s hard to pull negatively charged electrons from the positively charged foil, so the oxidation reaction will stop if the charge on the foil isn’t neutralized somehow.

Similarly, if you keep dumping electrons into the oxygen in the charcoal, a negative charge would build up. The reduction reaction will shut down, because you couldn’t force any more negatively charged electrons onto the negatively charged surface of the charcoal.

This is where the wet, salty paper towel comes in. The towel acts as a salt bridge that prevents charge from building up on either the foil or the charcoal. Chloride ions in the salt (Cl) move towards the foil, neutralizing the positive charge buildup there. Sodium ions (Na+) migrate towards the charcoal, neutralizing the negative charge buildup caused by the reduction.

h1

Install Moodle 1.8 under Apache/Vista

June 7, 2007

Moodle is usually easy to install once you’ve got Apache/PHP 5/MySQL running.

But the installation script for Moodle 1.8 fails- hitting ‘Next’ after entering the database information gave me a blank screen. EDIT: This also is true of Moodle 1.9 beta.

The install works fine under XP. But the problem isn’t limited to Vista: John McGrath and others on moodle.org report a similar problem under XAMPP/Win2000. John manually built his configuration file to get Moodle running (http://moodle.org/mod/forum/discuss.php?d=64692).

I was able to install Moodle 1.8 successfully under Vista following his approach.

  1. Install the Apache webserver. I’m using Apache 2.2.4.
  2. Install MySQL 5 (The most recent installer (5.0.41) works without a hitch under Vista. Don’t forget to unblock port 3306 in your firewall, and set sql-mode=”” in your my.ini file; Moodle doesn’t like strict mode).
  3. Install PHP 5. I’m using version 5.2.3.
  4. Download the latest stable build of 1.8+ from http://download.moodle.org/download.php/stable18/moodle-latest-18.zip.
  5. Unzip the file into your Apache document root (c:\apache\htdocs, if you follow the instructions linked in step one). I renamed the directory c:\apache\htdocs\moodle18, so I could install multiple versions of moodle later for testing.
  6. Create a directory for storing data that is outside the document root (I used c:\apache\htdata).
  7. Create an empty database for Moodle 1.8. I used mysql:
    1. Get a command line (All Programs > Accessories > Command Line) and start up mysql:
      mysql -u root -p
    2. After giving mysql your root password, you should see a welcome message. At the mysql prompt, type:
       create database moodle18;
       grant select,insert,update,delete,create,drop,
          index,alter on moodle18.* to moodle@localhost
          identified by 'put-a-moodle-password-here';
       quit;
      
  8. Double check your php.ini file. Moodle requires the following to be set somewhere in php.ini:
    session.auto_start = 0
    magic_quotes_gpc = On
    magic_quotes_runtime = Off
    register_globals = Off
    sql.safe_mode = Off
    file_uploads = On
    memory_limit = 128M
    upload_max_filesize = 64M
    post_max_size = 64M
    extension=php_gd2.dll
    

    Moodle won’t complain if you leave magic_quotes_gpc off, but Joomla will.

    64 megabytes is just a little larger than my largest course backups- if I set it any smaller, I won’t be able to upload them through Moodle.

  9. Create a Notepad file in your Moodle directory and call it config.php (c:\Apache\htdocs\moodle18\config.php, for me):
    <?php /// Moodle Configuration File
    
    unset($CFG);
    
    $CFG->dbtype = 'mysql';
    $CFG->dbhost = 'localhost';
    $CFG->dbname = 'moodle18';
    $CFG->dbuser = 'moodle';
    $CFG->dbpass = 'put-a-moodle-password-here';
    $CFG->dbpersist = false;
    $CFG->prefix = 'mdl_';
    
    $CFG->wwwroot = 'http://localhost/moodle18';
    $CFG->dirroot = 'C:\Apache\htdocs\moodle18';
    $CFG->dataroot = 'C:\Apache\htdata';
    $CFG->admin = 'admin';
    
    $CFG->directorypermissions = 00777; // try 02777 on a server in Safe Mode
    
    $CFG->unicodedb = true; // Database is utf8
    
    require_once("$CFG->dirroot/lib/setup.php");
    // MAKE SURE WHEN YOU EDIT THIS FILE THAT THERE ARE NO SPACES, BLANK LINES,
    // RETURNS, OR ANYTHING ELSE AFTER THE TWO CHARACTERS ON THE NEXT LINE.
    ?>
    

    Put in your own paths and passwords.

  10. Browse to your Moodle directory (http://localhost/moodle18, for me) and the install script should take it from there!
h1

Install PHP under Apache and Vista

June 6, 2007

I initially tried the msi installer for PHP 5. Unfortunately, it installs only PHP under CGI, which although it has some performance and security issues, might be ok for a quick and dirty development environment.

But the installer is badly broken. You’ll get an error something like

PHP Fatal error:  require_once()[function.require]: Failed opening
required 'SAM/php_sam.php' (include_path='.;C:php5pear') in
sam_factory on line 1

After fighting this for a couple of hours, I ended up installing manually anyway. I used this procedure to get PHP 5 running under Vista:

  1. Install the Apache webserver.
  2. Get the current version of PHP 5 from http://www.php.net/downloads.php#v5. (It’s 5.2.3 at this time). Get both the zip file and the PECL zip file (which includes extensions).
  3. Uninstall any previous installations of PHP 5 (Start > Control Panel > Programs and Features). You may have to reboot your machine.
  4. Disconnect from the Internet. Turn off your firewall. Turn off your virus checker.
  5. Turn off User Account Control (UAC).
  6. Get an administrator prompt by going to All Programs > Accessories. Right-Click “Command Prompt” and choose “Run as Administrator”
  7. Use the command prompt to manually remove directories containing previous PHP installations (like C:\Program Files\PHP…)
  8. Go to the directory where you’ve downloaded the PHP5 and PECL zipfiles. Extract the PHP zipfile. Rename the extracted php directory (which has a name something like php-5.2.3-Win32) to c:\php. Extract the PECL zipfile into c:\php\ext.
  9. In the command prompt, type:
    cd c:\php
    mkdir upload
    mkdir session
    copy php.ini-recommended php.ini
  10. Open php.ini in Notepad:
    1. There are many variables… the following are important. Notice that starting a line with a semicolon in the ini file comments it out.
      upload_tmp_dir="C:\php\upload"
      session.save_path="C:\php\session"
      cgi.force_redirect=0
      extension_dir ="C:\php\ext"
      display_errors = Off
      log_errors = On
      error_log = "C:\php\error_log"
    2. You might want to look at upload_max_filesize. I’ve set mine to 16M. I also set post_max_size = 16M.
    3. Enable the extensions you need by deleting the semicolon at the beginning of the line. These will depend on your application… I’m going to be using MySQL (which I’ve already installed) and Moodle (which I’ll install later). My list looks like the following:
      extension=php_bz2.dll
      extension=php_curl.dll
      extension=php_dba.dll
      extension=php_dbase.dll
      ;extension=php_exif.dll
      extension=php_fdf.dll
      extension=php_gd2.dll
      extension=php_gettext.dll
      ;extension=php_gmp.dll
      ;extension=php_ifx.dll
      ;extension=php_imap.dll
      ;extension=php_interbase.dll
      extension=php_ldap.dll
      extension=php_mbstring.dll
      extension=php_mcrypt.dll
      extension=php_mhash.dll
      extension=php_mime_magic.dll
      ;extension=php_ming.dll
      ;extension=php_msql.dll
      ;extension=php_mssql.dll
      extension=php_mysql.dll
      extension=php_mysqli.dll
      ;extension=php_oci8.dll
      extension=php_openssl.dll
      ;extension=php_pdo.dll
      ;extension=php_pdo_firebird.dll
      ;extension=php_pdo_mssql.dll
      ;extension=php_pdo_mysql.dll
      ;extension=php_pdo_oci.dll
      ;extension=php_pdo_oci8.dll
      ;extension=php_pdo_odbc.dll
      ;extension=php_pdo_pgsql.dll
      ;extension=php_pdo_sqlite.dll
      ;extension=php_pgsql.dll
      ;extension=php_pspell.dll
      extension=php_shmop.dll
      ;extension=php_snmp.dll
      extension=php_soap.dll
      extension=php_sockets.dll
      ;extension=php_sqlite.dll
      ;extension=php_sybase_ct.dll
      extension=php_tidy.dll
      extension=php_xmlrpc.dll
      extension=php_xsl.dll
      extension=php_zip.dll
  11. Right-Click on Start > Computer. Choose “Properties”, then “Advanced”. Click on “Environment Variables”. Click on PATH, then Edit… and add “;C:\php;c:\php\ext” (without quotes) to the end of the variable.
  12. Open your Apache configuration file (All Programs > Apache HTTP Server 2.2.x > Configure Apache Server > Edit the Apache httpd.conf Configuration File). Add the following lines:
    LoadModule php5_module "C:\php\php5apache2_2.dll"
    AddType application/x-httpd-php .php
    AcceptPathInfo on
    PHPIniDir "C:\php"

    (UPDATE: When I installed PHP 5.2.13 using this procedure (July 2010) under Apache 2.2.15, I found that I had to delete that last line (the PHPIniDir directive) or Apache wouldn’t start.)

    Look for the DirectoryIndex line. Add ” index.php” (no quotes) to the end of that line.

    Save the file.

  13. Create a file named test.php in your Apache htdocs directory, containing this text: <?php phpinfo(); ?>
  14. Reboot your machine.
  15. Browse to http://localhost/test.php. You should see tabulated information about your working PHP installation.
h1

Stop User Account Control under Vista

June 6, 2007

Vista constantly asks for confirmation of actions when you’re installing software. This is designed to prevent worms and trojans from doing their thing, but some installations that worked under XP will fail under Vista because of this behavior. You can fix it (and stop the constant nagging) by stopping “User Account Control” as follows:

  1. Go to Start > Control Panel > User Accounts.
  2. Click on “Turn user account control on or off”.
  3. Uncheck “Use user account control (UAC) to protect your computer”.
  4. Click “OK”.
  5. Reboot.
h1

Install Apache on Windows Vista (Updated)

June 6, 2007

Installing Apache under Windows XP was trivial. Not so, under Vista. Creation of the Apache service fails. The conf directory can’t be set up by the installer, probably due to permission problems.

I finally got it working with the following procedure. I used the latest version of Apache (2.2.4) and Windows Vista Home Premium.

Update 11/20/07: I had to do a factory reset on my machine a few weeks ago (due to a video card failure) and had to run through this procedure a second time to install Apache 2.2.6. I streamlined it a bit. The problem with the Apache Monitor noted in the old set of instructions seems to have been fixed in Apache 2.2.6… when you reboot in step 12, the Apache monitor should appear on your task bar.

  1. Uninstall any previous installations of Apache Web server (Start > Control Panel > Programs and Features).
  2. Stop any other services (like IIS) that use port 80 (if you still want to use IIS, give it another port). If you’re running Skype you have to uncheck Advanced Options > Connection: Use Port 80 and 443 as alternatives (Thanks Jacob!)
  3. Stop User Account Control (UAC). (This isn’t absolutely necessary, but there will be less nagging if you do.)
  4. Get the most recent version of the apache Win32 binaries from http://httpd.apache.org/download.cgi and put it on your desktop. Rename it to apache (without changing the .msi extension, if that’s showing).
  5. Start > All Programs > Accessories
  6. Right-Click “Command Prompt” and choose “Run as Administrator”
  7. Manually remove directories containing previous apache installations (like C:\Program Files\Apache Software Foundation…)
  8. Change to your desktop folder directory (At the prompt type: cd desktop)
  9. At the prompt, type: msiexec /i apache.msi
  10. Run through the Apache installer. I’m running a development server, so I left the domain and computer name blank. You must specify an email address, though. Choose the default server on port 80 for all users option. Change the installation directory to c:\apache.
  11. Reboot.
  12. Browse to http://localhost. It should say “It works!” If it doesn’t, check your httpd.conf file by going to All Programs > Apache HTTP Server 2.2.x > Configure Apache Server > Test Configuration. Follow the directions for fixing the configuration file.
  13. You can turn UAC back on, if you like that sort of thing. To each his own!