Download - All IT eBooks

Transcript
3.3. Crafty Pong
45
< script src = " app . js ? v = <? php
echo filemtime ( " app . js " ); ? > > </ script >
< link rel = " stylesheet "
href = " style . css ? v = <? php echo filemtime ( " style . css " ); ? >" >
</ head >
< body >
< img src = " myimage . png ? v = <? php
echo filemtime ( " myimage . png " ); ? > >
Now, PHP will append the file’s modified time to the end of the v= parameter.
A change in file modification will change the value for v. This will automatically
force browsers to fetch new versions. This is very useful since browsers tend
to cache aggressively.
If you are using a web server, such as XAMPP, WAMP, or MAMP mentioned in Part I, your server supports PHP. I recommend switching to the
automatic versioning method now. Browsers will sometimes cache even if you
try to tell them not to with no-cache headers. However, they always fetch
when given new version numbers. A stale cache can cause lots of lost time
during development.
3.3.1
Hello Crafty
Crafty was chosen at this point for revised Pong because it is the lightest of the
engines, but all the engines being reviewed in Part II could easily implement
this simple game. Crafty just happens to be a good candidate for this sort
of quick task. We will need to refactor our code for the Crafty way of doing
things. For now, let’s initialize our Canvas and display some text. This verifies
we are set up correctly. See Listing 3.2 for the code and Figure 3.1 for the
output.
function onload () {
if ( Modernizr . canvas && Modernizr . canvastext ) {
Crafty . init (320 , 480);
Crafty . background ( ’# dbdbdb ’);
Crafty . e ( " HelloWord , Canvas , 2D , Text " )
. attr ({ x : 20 , y : 20 , w : 100 , h : 20})
. text ( " Hello Crafty ! " );
} else {
var yes = confirm ( " Download a better browser ? " );
if ( yes )
{
window . location = " http :// google . com / chrome " ;
}
}
}
Listing 3.2. Initialize Crafty