Matt Kenefick
Contact Me Experiments Blog Portfolio

Posts Tagged ‘Actionscript 3.0’

What makes up an Android App ?

Sunday, February 8th, 2009

This is taken from the Google documentation but I’m going to attempt to explain it in common developer terms and relationships. Since they are specific, some examples will not be DIRECTLY related, but the idea will help you understand what it’s about a little.

Apps are typically built on a combination of 4 building blocks:
- Activity
- Broadcast Intent Receiver
- Service
- Content Provider

So I’ll jump into the first one which is Activity. This is basically each individual screen of your application. So if we were talking about a webpage… Your homepage is an Activity, your login page is an Activity, your success/error page is an Activity… Basically every screen is an Activity. Of course each screen, just like on a webpage, is built from different components. In the web world, these would be like DIV tags, INPUT components, IMAGES, etc… The page as a whole is an Activity.

Broadcast Intent Receivers (BIR) are similar to, but not identical to, events in Actionscript. In the Android world, a BIR would handle external events of the phone. So if the phone starts ringing, the time changes to midnight, or your lose service.. Your app receives an Event to the BIR. You can think of this as being similar to the Actionscript EventDispatcher. Like the EventDispatcher, you can also broadcast events instead of just listening for them. Some Android code examples would look like… “Context.registerReceiver();” or “Context.sendBroadcast();” .. Looks an awful lot like “addEventListener” and “dispatchEvent” doesn’t it?

A Service is something that will play in the background. The best example (and take THIS iPhone) is playing music. Attaching sound to an Service will allow you to listen to your song as you’re going through different screens in your app. Attaching sound to an Activity will cause your song to stop playing when you switch screens. Services are totally code based, obviously. Try to relate it to the services on your computer. You have services running all the time that manage your network connections, LAMP servers, audio controllers, etc. You don’t SEE them, but they’re handling code continuously in the background. You have control over these as well. You can communicate to see what song is playing, change the song, etc.

The Content Provider is a place to store data. SQLLite or other. It lets you share data with other apps on the phone. There is no specific directory, like “Uploads” or “Files” that every app can access. It must be passed through the Content Provider. This will be covered more later, but for now.. Just think of it as a database that you store/retreive information. Content Provider examples: Contacts, Media, Images, Groups, etc…

These will be covered more later, but hopefully gives a more ‘real-world’ approach to what these terms actually mean.

Actionscript to Android

Sunday, February 8th, 2009

Over the next few weeks.. I’m going to write out some very simple to progressively more advanced Actionscript to Android comparisons. Since it’s all Java based, it should be fairly simple to see the differences and merge your way from AS3 to Android. Instead of explaining a bunch of stuff, I’ll just jump directly into it.

Actionscript 3.0

package com.android.hello {
 
  import android.app.Activity;
  import android.os.Bundle;
  import android.widget.TextView;
 
  public class HelloAndroid extends Activity{
    /** Called when activity is first created */
    public override onCreate( savedInstanceState:Bundle ):void {
      super.onCreate( savedInstanceState );
 
      var tv:TextView = new TextView( this );
      tv.setText("Hello, Android");
      setContentView( tv );
    }
  }
}
Android (Java)

package com.android.hello;
 
  import android.app.Activity;
  import android.os.Bundle;
  import android.widget.TextView;
 
  public class HelloAndroid extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate( Bundle savedInstanceState ) {
       super.onCreate( savedInstanceState );
 
       TextView tv = new TextView(this);
       tv.setText("Hello, Android");
       setContentView(tv);
    }
}

This is a very simple example that helps show the differences in syntax.

Actionscript 3 Android (Java)
- Enclose package in brackets
- Override after “public”
- Return type (void) AFTER parameters
- Variable type “var varname:Type”
- Do not enclose package
- Override set before method
- Return type (void) BEFORE method name
- Variable type “Type varname”

Fractal4D coming to AIR

Thursday, November 6th, 2008

The test below was just the beginning. I am expanding it further to the web and also bringing it to AIR. Features will include the standard as well as:

  • attribute panels
  • live color monitoring
  • change background color
  • export to graphic
  • resize canvas
  • fullscreen mode
  • layering
  • glow effects
  • color sequencing
  • color selection
  • canvas rotation
  • canvas scaling

I am also working with converting it to Illustrator so you can take these vectors and make them huge. The color sequencing mentioned is a way to select the color spectrum you want it to automatically navigate through. Right now it goes through the whole rainbow, but will be able to select something like (red, blue, green, red, purple, red, black, white) and it will follow correctly.

This project is exciting. I hope to have it ready sometime next week. Watch out for it!

Classy drop shadows for objects

Wednesday, October 29th, 2008

Want to create these types of shadows?

The following code will analyze the width of your selected object, even if it is centered within the clip, and create a shadow for it. You can specify the offset from the object, the fade amount, and the height of the shadow (which would specify angle of view).

Actionscript 3.0

function createShadow( item:MovieClip, offset = 10, fade:Number = 1, shadowHeight:int = 20 ){
	var itemShadow:Sprite 	= new Sprite();
	var gfx:Graphics		= itemShadow.graphics;
	var matrix:Matrix 		= new Matrix();
	var realDimensions 	= item.getBounds(item.parent);
 
	matrix.createGradientBox( item.width, item.height );
	gfx.beginGradientFill("radial", [0x000000, 0x000000], [fade,0],[0,255],matrix);
	gfx.drawRect(0,0,item.width,item.height);
	gfx.endFill();
 
	itemShadow.y 		= 	realDimensions.y + item.height + offset;
	itemShadow.x 		= 	realDimensions.x;
	itemShadow.height 		=	shadowHeight;
	itemShadow.blendMode	=	"multiply"
 
	addChild( itemShadow );
}
 
createShadow( item1_mc, 50, .5, 10 );
createShadow( item2_mc, 20, .7, 15 );

Actionscript 2.0

import flash.geom.Matrix;
 
function createShadow( item:MovieClip, offset, fade:Number, shadowHeight:Number ){
	fade 	= fade ? fade : 50;
	offset 	= offset ? offset : 50;
	shadowHeight = shadowHeight ? shadowHeight : 10;
 
    var itemShadow:MovieClip   	= item._parent.createEmptyMovieClip('itemShadow', item._parent.getNextHighestDepth());
    var matrix:Matrix    		= new Matrix();
    var realDimensions   		= item.getBounds(item._parent);
 
    matrix.createGradientBox( item._width, item._height );
    itemShadow.beginGradientFill("radial", [0x000000, 0x000000], [fade,0],[0,255],matrix);
	itemShadow.moveTo(0,0);
	itemShadow.lineTo(item._width,0);
	itemShadow.lineTo(item._width,item._height);
	itemShadow.lineTo(0,item._height);
    itemShadow.endFill();
 
    itemShadow._y          =  realDimensions.yMin + item._height + offset;
    itemShadow._x          =  realDimensions.xMin;
    itemShadow._height     = shadowHeight;
    itemShadow.blendMode    =   "multiply"
}
 
createShadow( item1_mc, 50, 50, 10 );

Using custom event listeners in AS3.0

Wednesday, October 15th, 2008

This is a topic that I am a little surprised I do not see more about. Most people are used to using “Event”, “TimerEvent”, and “MouseEvent” but do they realize you can set it to be whatever you want? Technically, it is logical and sensible but I think some people still overlook its ease because they are used to what they see in tutorials or documentations. What I mean is this:

// custom listener
function mk_callback( e:* ){
	trace('This is a callback for MattKenefick event.');
}
addEventListener( "MattKenefick" , mk_callback );
 
 
// custom dispatcher
function mouseCallback(me:MouseEvent){
	// send event from this clip
	// does not affect anything because listener is set on the stage
	me.target.dispatchEvent( new Event("MattKenefick") );
 
	// send event from parent (stage)
	// this will run the mk_callback event
	me.target.parent.dispatchEvent( new Event("MattKenefick") );
}
MovieClip1.addEventListener( MouseEvent.CLICK, mouseCallback );

This is assuming you have a clip on your stage named “MovieClip1″. Your listener can be anything and anywhere. Other classes, functions, or whatnot can listen to an event from anywhere and act upon an event without being instantiated from a single function.

This is especially handy if you have some sort of Loader and you want a sequence of events to occur. You can loosely couple the structure so the event can occur on its own and the listeners will just act accordingly to the response it sends out.

High five to the observer pattern. http://en.wikipedia.org/wiki/Observer_pattern