Saturday, April 24, 2010

OOP Concepts in Actionscript 3.0: Inheritance vs. composition

A simple rule of thumb determines whether the relationship between classes is one that warrants inheritance or composition. If you can say class A "is a" class B, you're dealing with inheritance. If you can say class A "has a" class B, the relationship is one of composition.

Here are some examples of inheritance:

* Cat "is an" animal
* Engineer "is an" employee
* Rugby "is a" sport

Here are examples of composition:

* Wall "has a" brick
* Computer "has a" keyboard
* School "has a" teacher


So what is the difference in how inheritance and composition are implemented? Let's compare how this works, starting with inheritance:

Animal.as ====

package {
public class Animal {

public var furry:Boolean;
public var domestic:Boolean;

public function Animal() {
trace("new animal created");
}
}
}


The Animal.as code is the base Animal class, which you will now extend using inheritance with a Cat class:

Cat.as

package {
public class Cat extends Animal {
public var family:String;
public function Cat() {
furry = true;
domestic = true;
family = "feline";
}
}
}


Let's do a quick test of this class by instantiating it on the main Timeline of a blank FLA file:

import Animal;
var myCat:cat = new cat();


================================================================================

Let's take the Brick class created earlier. In this next example you'll create a Wall class that uses composition to instantiate instances of the Brick class:

Wall.as

package com.adobe.ooas3 {
import com.adobe.ooas3.Brick;
public class Wall {

public var wallWidth:uint;
public var wallHeight:uint;

public function Wall(w:uint, h:uint) {
wallWidth = w;
wallHeight = h;
build();
}
public function build():void {
for(var i:uint=0; i for(var j:uint=0; j var brick:Brick = new Brick();
}
}
}
}
}

In the code above, the Wall class accepts two arguments passed to its constructor, defining the width and height in bricks of the wall you want to create.

Let's do a quick test of this class by instantiating it on the main Timeline of a blank FLA file:

import Wall;
var myWall:Wall = new Wall(4,4);

Wednesday, April 21, 2010

Javascript Identifying Screen Resolution

<script type="text/javascript" language="javascript">
function checkResol()
{
alert("Current resolution is: "+screen.width+" X "+screen.height);
}
</script>

<body onload="checkResol();">

</body>

Tuesday, April 20, 2010

What is Failure?

Does it surprise you that only 400 cokes were sold the first year; Albert Einstein's Ph.D. dissertation was rejected; Henry Ford had two bankruptcies before his famous success; or Ulysses S. Grant was working as a handyman, written off as a failure, eight years before becoming President of the United States?

Rodin couldn't get into art school on three occasions yet became a great sculptor; Abraham Lincoln lost seven elections before winning the Presidency; Babe Ruth stuck out 1,330 times in route to hitting 714 home runs; and Oprah Winfrey publicly failed several diet attempts before becoming an inspiration for looking great after fifty.

Setbacks, disappoints, rejections and unsuccessful attempts were not failures to these people. They were steps to their success. That's the difference between people who are winning at working and people who aren't. How you deal with your setbacks (big or small) will determine your results. You see, failure is not the lack of success. Failure is staying down when you trip or stumble. It's giving up, checking out, or shutting down.

How you view your disappointments, falls, and setbacks will impact your success. Do you see them as stepping stones or brick walls? People who are winning at working live Ralph Waldo Emerson's words, "Men succeed when they realize that their failures are the preparation for their victories."

People who are winning at working don't blame others for what's happened to them, and they don't use other people's definitions for success and failure. They use their own. They know it's not failing to miss their mark, change paths, re-assess goals, try something new or adjust direction. To them, failure happens when they stop trying to achieve their personal best.

Monday, April 19, 2010

Remove unnecessary border from tabs, links in Firefox and IE

Here is the CSS:

<style type="text/css">
a
{
text-decoration: none;
color: #5780ca;
}
a:hover
{
text-decoration: underline;
color: #5780ca;
}
a:active
{
-moz-outline-style: none;
outline: none;
}
.linkSelected
{
-moz-outline-style: none;
outline: none;
}
</style>



Here is the HTML:

<a href="#" class="linkSelected">Link One</a>
<br />
<a href="#" class="linkSelected">Link Two</a>
<br />
<a href="#" class="linkSelected">Link Three</a>
<br />