Thursday, July 29, 2010

How to get a custom cursor in an AIR Window

I was testing custom cursors in an AIR Window this past week and I found some strange behavior. If tried to change the default cursor in an AIR Window by calling :

CursorManager.setBusyCursor()

The cursor in the main WindowedApplication changed rather than the cursor in my AIR Window. So, how could I change just the cursor in my AIR Window. The answer is to use a lower case “cursorManager” on the Window. So, I needed to call:

myAIRWindow.cursorManager.setBusyCursor();


Source from http://butterfliesandbugs.wordpress.com/2008/09/04/how-to-get-a-custom-cursor-in-an-air-window/

Thursday, July 22, 2010

Programming in Object-C 2.0

This book is divided into three logical parts. Part I,“The Objective-C 2.0 Language,”
teaches the essentials of the language. Part II,“The Foundation Framework,” teaches how
to use the rich assortment of predefined classes that form the Foundation framework. Part
III,“Cocoa Programming and the iPhone SDK,” gives you an overview of Cocoa’s Application
Kit framework and then walks you through the process of developing a simple
iPhone application using the UIKit framework, and developing and debugging the code
with Xcode and Interface Builder.
A framework is a set of classes and routines that have been logically grouped together to
make developing programs easier. Much of the power of programming in Objective-C
rests on the extensive frameworks that are available.
Chapter 2,“Programming in Objective-C,” begins by teaching you how to write your
first program in Objective-C.
Because this is not a book on Cocoa programming, graphical user interfaces (GUIs)
are not extensively taught and are hardly even mentioned until Part III. So an approach
was needed to get input into a program and produce output. Most of the examples in this
text take input from the keyboard and produce their output in a window: a Terminal
window if you’re using gcc from the command line, or a Console window if you’re using
Xcode.
Chapter 3,“Classes, Objects, and Methods,” covers the fundamentals of object-oriented
programming.This chapter introduces some terminology, but it’s kept to a minimum.
I also introduce the mechanism for defining a class and the means for sending
messages to instances or objects. Instructors and seasoned Objective-C programmers will
notice that I use static typing for declaring objects. I think this is the best way for the student
to get started because the compiler can catch more errors, making the programs
more self-documenting and encouraging the new programmer to explicitly declare the
data types when they are known.As a result, the notion of the id type and its power is not
fully explored until Chapter 9,“Polymorphism, Dynamic Typing, and Dynamic Binding.”

Chapter 4,“Data Types and Expressions,” describes the basic Objective-C data types
and how to use them in your programs.
Chapter 5,“Program Looping,” introduces the three looping statements you can use in
your programs: for, while, and do.
Making decisions is fundamental to any computer programming language. Chapter 6,
“Making Decisions,” covers the Objective-C language’s if and switch statements in detail.
Chapter 7,“More on Classes,” delves more deeply into working with classes and objects.
Details about methods,multiple arguments to methods, and local variables are discussed
here.
Chapter 8,“Inheritance,” introduces the key concept of inheritance.This feature makes
the development of programs easier because you can take advantage of what comes from
above. Inheritance and the notion of subclasses make modifying and extending existing
class definitions easy.
Chapter 9 discusses three fundamental characteristics of the Objective-C language.
Polymorphism, dynamic typing, and dynamic binding are the key concepts covered here.
Chapters 10–13 round out the discussion of the Objective-C language, covering issues
such as initialization of objects, protocols, categories, the preprocessor, and some of the
underlying C features, including functions, arrays, structures, and pointers.These underlying
features are often unnecessary (and often best avoided) when first developing objectoriented
applications. It’s recommended that you skim Chapter 13,“Underlying C
Features,” the first time through the text and return to it only as necessary to learn more
about a particular feature of the language.
Part II begins with Chapter 14,“Introduction to the Foundation Framework,” which
gives an introduction to the Foundation framework and how to access its documentation.
Chapters 15–19 cover important features of the Foundation framework.These include
number and string objects, collections, the file system, memory management, and the
process of copying and archiving objects.
By the time you’re done with Part II, you will be able to develop fairly sophisticated
programs in Objective-C that work with the Foundation framework.
Part III starts with Chapter 20,“Introduction to Cocoa.” Here you’ll get a quick
overview of the Application Kit that provides the classes you need to develop sophisticated
graphical applications on the Mac.
Chapter 21,“Writing iPhone Applications,” introduces the iPhone SDK and the UIKit
framework.This chapter illustrates a step-by-step approach to writing a simple iPhone (or
iTouch) application, followed by a calculator application that enables you to use your
iPhone to perform simple arithmetic calculations with fractions.
Because object-oriented parlance involves a fair amount of terminology,Appendix A,
“Glossary,” provides definitions of some common terms.
Appendix B,“Objective-C Language Summary,” gives a summary of the Objective-C
language, for your quick reference.
Appendix C,“Address Book Source Code,” gives the source code listing for two classes
that are developed and used extensively in Part II of this text.These classes define address card and address book classes. Methods enable you to perform simple operations such as
adding and removing address cards from the address book, looking up someone, listing
the contents of the address book, and so on.
After you’ve learned how to write Objective-C programs, you can go in several directions.
You might want to lean more about the underlying C programming language—or
you might want to start writing Cocoa programs to run on Mac OS X, or develop more
sophisticated iPhone applications. In any case,Appendix D,“Resources,” will guide you in the right direction.
Download

Tuesday, July 20, 2010

Build ObjectC on Windows with Foundation frameworks

Use

gcc `gnustep-config --objc-flags` -o hello hello.m -L C:/GNUstep/GNUstep/System/Library/Libraries -lobjc -lgnustep-base

on Shell command line.

view more

Monday, July 19, 2010

Flipbook AS2



AS2 Pageflip Open source free at pageflip.hu

iPhone touch motion

View

import flash.events.MouseEvent;
import flash.events.Event;

var totalScene:uint = 4;
var thumbWidth:Number = 317;
var currentIndex:int = 0;
var startMoveX: Number;
var lastMoveX: Number;
/*var startMoveY: Number;
var lastMoveY: Number;*/

var isDragging: Boolean = false;
var desX:Number = 0;

// FOR TEST ON MOUSE

//scene_mc: a top-left movie, contains 4 images with image's width = 317
scene_mc.addEventListener(MouseEvent.MOUSE_DOWN, onSceneMouseBegin);
this.stage.addEventListener(MouseEvent.MOUSE_UP, onSceneMouseUp);
function onSceneMouseBegin(e: MouseEvent)void {        
    isDragging = true;
    startMoveX = this.mouseX;
    //startMoveY = this.mouseY;
    
    scene_mc.startDrag(falsenew Rectangle(-scene_mc.width + thumbWidth/20
                        scene_mc.width, 0));
    this.removeEventListener(Event.ENTER_FRAME, ef);
    this.addEventListener(Event.ENTER_FRAME, ef);    
}

function onSceneMouseUp(e: MouseEvent)void {        
    scene_mc.stopDrag();
    isDragging = false;

    var diff = lastMoveX - startMoveX;
    if (Math.abs(diff30) {
        if (diff > 0) {
            activeSlide(currentIndex - 1);
        else {
            activeSlide(currentIndex+1)
        }
        return;
    }
    
    //check pos x if out of range
    var posX: int = Math.abs(scene_mc.x / thumbWidth);
    var plusX: Number = Math.abs(scene_mc.x % thumbWidth);    
    if (Math.abs(plusX>= thumbWidth/2posX ++;    
    if (posX >= totalSceneposX = totalScene - 1;
    activeSlide(posX);
}

function getMouseY(): Number {
    return this.mouseY;
}
function ef(e: Event)void {
    lastMoveX = this.mouseX;
    //lastMoveY = this.mouseY;
    if (!isDraggingscene_mc.x += (desX - scene_mc.x)*0.15;
}

function activeSlide(index: int) {
    if (index < 0index = 0;
    if (index >= totalSceneindex = totalScene - 1;
    currentIndex = index;
    desX = -index*thumbWidth;
}

DisplayUtils and fitIntoRect

Useful to create thumbnails and resizing

From http://blog.soulwire.co.uk/code/actionscript-3/fit-a-displayobject-into-a-rectangle

Sunday, July 18, 2010

Slide Transition


View DEMO

How to use:
import binhpro.SlideTransition;
import binhpro.ClickPageEvent;
var mcs: Array = [mc_1, mc_2,mc_3,mc_4];

//width of the mask in NORMAL, MAX, MIN width
var options: Object = new Object();
options["normal_width"150;
options["max_width"317;
options["min_width"94;

var slideTran: SlideTransition = new SlideTransition(this, mcs, options, 0true);
slideTran.addEventListener(ClickPageEvent.CLICK_PAGE_EVENT, onPageClicked);
    
function onPageClicked(event: ClickPageEvent) {
    var page: MovieClip = event.mc;
    trace("page is clicked: " + page.name);
}


SlideTransition.as
package binhpro //binhpro.SlideTransition
    import binhpro.utils.ImageUtils;
    import flash.display.MovieClip;
    import flash.events.EventDispatcher;
    import flash.events.MouseEvent;
    import gs.TweenMax;
    
    /**
     * ...
     @author binhdocco
    
    Event:  ClickPageEvent, type: ClickPageEvent.CLICK_PAGE_EVENT
    Usage:
    
    var options: Object = new Object();
    options["normal_width"] = 348;
    options["max_width"] = 792;
    options["min_width"] = 200;

    var slideTransition: SlideTransition = new SlideTransition(this, 
                            [page1, page2, page3, page4], 
                            options);
    slideTransition.addEventListener(ClickPageEvent.CLICK_PAGE_EVENT, onPageClicked);
    
    function onPageClicked(event: ClickPageEvent) {
        var page: MovieClip = event.mc;
        trace("page is clicked: " + page.name);
    }
     */
    public class SlideTransition extends EventDispatcher {
        
        private var mainmc: MovieClip;
        private var mcs: Array;
        private var verGap: Number;
        private var useBW: Boolean;
        
        private var NORMAL_WIDTH: Number = 210;// 212;
        private var MAX_WIDTH: Number = 650;// 353.3;
        private var MIN_WIDTH: Number = 122;    
        
        private var activeMC: MovieClip;
        
        public function SlideTransition(mmc: MovieClip, mcs: Array, 
                                        options: Object = null, vergap: Number = 4,
                                        useBlackAndWhiteAsNormal: Boolean = true) {
            this.mcs = mcs;
            this.mainmc = mmc;
            this.verGap = vergap;        
            this.useBW = useBlackAndWhiteAsNormal;
            if (options) {
                NORMAL_WIDTH = options['normal_width'];
                MAX_WIDTH = options['max_width'];
                MIN_WIDTH = options['min_width'];
            }
            reArrange();            
        }
        
        private function reArrange():void {
            var xpos: Number = NORMAL_WIDTH/2;            
            for (var i:int = 0; i < mcs.length; i++) {
                var mc: MovieClip = mcs[ias MovieClip;
                mc.x = xpos;
                xpos += NORMAL_WIDTH + verGap;    
                
                mc.buttonMode = true;
                mc.pos = i;
                mc.active = false;
                if (mc.text_mc) {
                    mc.textX = mc.text_mc.x;
                    mc.text_mc.mouseEnabled = false;
                }
                if (useBW) {
                    ImageUtils.blackAndWhite(mc, true);
                    mc.addEventListener(MouseEvent.MOUSE_OVER, onOver);
                    mc.addEventListener(MouseEvent.MOUSE_OUT, onOut);
                }
                
                if (mc.imgmc.img.addEventListener(MouseEvent.CLICK, onClick);
                if (mc.close_btnmc.close_btn.addEventListener(MouseEvent.CLICK, onClose);
            }
        }    
        
        private function onOut(e:MouseEvent):void {
            if (activeMC != e.currentTarget) {
                ImageUtils.blackAndWhite(e.currentTarget as MovieClip, true);
            }
        }
        
        private function onOver(e:MouseEvent):void {
            if (activeMC != e.currentTarget) {
                ImageUtils.blackAndWhite(e.currentTarget as MovieClip, false);
            }
        }
        
        private function onClose(e:MouseEvent):void {
            //trace("onClose: " + e.eventPhase);
            if (useBW && activeMCImageUtils.blackAndWhite(activeMC, true);
            activeMC = null;
            doArrange();
        }
        
        private function onClick(e:MouseEvent):void {
            //trace("onClick: " + e.eventPhase);
            var clickedMC: MovieClip = e.currentTarget.parent as MovieClip;
            if (activeMC) {
                if (useBWImageUtils.blackAndWhite(activeMC, true);
                if (activeMC.name != clickedMC.name) {                    
                    activeMC = clickedMC;                                        
                else {                    
                    if (useBW && activeMCImageUtils.blackAndWhite(activeMC, false);
                    //activeMC = null;
                    //jump page event
                    var jumpEvent: ClickPageEvent = new ClickPageEvent();
                    jumpEvent.mc = activeMC;
                    this.dispatchEvent(jumpEvent);
                    return;
                }
            else {
                activeMC = clickedMC;                
            }                    
            if (useBW && activeMCImageUtils.blackAndWhite(activeMC, false);
            doArrange();            
        }
        
        private function doArrange():void {
            //trace("doArrange");
            TweenMax.killAllTweens();
            var xpos: Number = NORMAL_WIDTH / 2;    
            for (var i:int = 0; i < mcs.length; i++) {
                var mc: MovieClip = mcs[ias MovieClip;
                if (activeMC) {        
                    if (activeMC.name == mc.name) {
                        if (i == 0xpos = 0;
                        xpos += MAX_WIDTH / 2;
                    else {
                        if (i == 0xpos = 0;
                        xpos += MIN_WIDTH / 2;
                    }                    
                }                 
                TweenMax.to(mc, 0.4x: xpos } );
                if (activeMC) {                    
                    if (activeMC.name == mc.name) {
                        xpos += MAX_WIDTH/+ verGap;
                        TweenMax.to(activeMC.mask_mc, 0.4width: MAX_WIDTH } );
                        if (mc.text_mc)
                            TweenMax.to(mc.text_mc, 0.4x: MAX_WIDTH/} );
                    else {
                        xpos += MIN_WIDTH/+ verGap;
                        TweenMax.to(mc.mask_mc, 0.4width: MIN_WIDTH } );
                        if (mc.text_mc)
                            TweenMax.to(mc.text_mc, 0.4x: MIN_WIDTH/} );
                    }
                else {
                    xpos += NORMAL_WIDTH + verGap;
                    TweenMax.to(mc.mask_mc, 0.4width: NORMAL_WIDTH } );
                    if (mc.text_mc)
                        TweenMax.to(mc.text_mc, 0.4x: mc.textX } );
                }
            }
        }
        
    }
    
}


ClickPageEvent.as
package binhpro //binhpro.ClickPageEvent
    import flash.display.MovieClip;
    import flash.events.Event;
    
    /**
     * ...
     @author binhdocco

     */
    public class ClickPageEvent extends Event {
        
        public static const CLICK_PAGE_EVENT: String = "CLICK_PAGE_EVENT";
        
        public var mc: MovieClip;
        
        public function ClickPageEvent(type: String = CLICK_PAGE_EVENT) {
            super(type);
        }
        
    }
    
}


ImageUtils.as
package binhpro.utils // binhpro.utils.ImageUtils
    import flash.display.DisplayObject;
    import flash.filters.ColorMatrixFilter;
    /**
     * ...
     @author binhdocco

     */
    public class ImageUtils{    
        
        // from http://snipplr.com
        public static function blackAndWhite($target:DisplayObject, $enabled:Boolean):void {
            if ($enabled) {
                var rc:Number = 1/3;
                var gc:Number = 1/3;
                var bc:Number = 1/3;
                var cmf:ColorMatrixFilter = new ColorMatrixFilter(
                            [rc, gc, bc, 
                            00, rc, gc, bc, 
                            00, rc, gc, bc, 
                            0000010]);            
                $target.filters = [cmf];
            else {
                $target.filters = [];
            }
        }
        
    }

}

Friday, July 16, 2010

Grid of Items Container

This post introduces to you a container that can help you create a grid of items that list in horizontal or vertical direction.

You can customize the ItemRenderer and ScrollBar by passing the name of it's Linkage Class.

HORIZONTAL CANVAS:

VIEW

VERTICAL CANVAS:

VIEW

EXAMPLE CODE:

import binhdocco.containers.canvas.Canvas;
import binhdocco.containers.events.ItemEvent;

var canvas: Canvas = new Canvas();

// dataProvider is an array of objects, contains information of an item
canvas.dataProvider = [11112222333444];

canvas.itemRenderer = SampleItemRenderer;
canvas.vScrollbar = vScrollBar;
//canvas.hScrollbar = hScrollBar;
canvas.limitValue = 5;

canvas.viewWidth = 475;
canvas.viewHeight = 280;
canvas.x = 60;
canvas.y = 60;

//canvas.itemPaddingX = 30;
//canvas.itemPaddingY = 5;
//canvas.direction = "horizontal";
this.addChild(canvas);

canvas.addEventListener(ItemEvent.ITEM_CLICK, onItemClick);
canvas.addEventListener(ItemEvent.ITEM_ROLL_OVER, onItemOver);
canvas.addEventListener(ItemEvent.ITEM_ROLL_OUT, onItemOut);

function onItemClick(e: ItemEvent) {
    output_txt.text = "Item Clicked: " + e.itemRenderer.name + "\n";
    (output_txt as TextField).appendText("Item data info: " + e.itemRenderer.data.toString());
}
function onItemOver(e: ItemEvent) {
    output_txt.text = "Item Over: " + e.itemRenderer.name + "\n";
    (output_txt as TextField).appendText("Item data info: " + e.itemRenderer.data.toString());
}
function onItemOut(e: ItemEvent) {
    output_txt.text = "Item Out: " + e.itemRenderer.name + "\n";
    (output_txt as TextField).appendText("Item data info: " + e.itemRenderer.data.toString());
}


Checkout SVN: https://flashtraining.googlecode.com/svn/trunk/AS3/BDCFramework

Wednesday, July 14, 2010

Testing local or online

AS3
if(stage.loaderInfo.url.indexOf("file:"!= -1)
{
    trace("Local");
}else{
    trace("Server");
}

AS2
if(_url.indexOf("file"!= -1){ 
    trace("Local");
}else{
    trace("Server");
}


From Snipplr

Tuesday, July 13, 2010

Access the _root (AS2) in AS3

In AS2, we can use _root to access the first level movie.

In AS3, we use

(stage.getChildAt(0as MovieClip)

Useful Flash MXP Tool

This tool help you design faster, with 5 figures below:

1.MouseEvent: auto generate Click, RollOver, RollOut code for selected instance.
2.Reflection: auto generate a reflection of selected instance, a selected instance must has registration point at Top-Left, and does not be scaled, please scale its children if you want to make it larger.
3. Clean library: make the library clean, automatic does arrange movies into Movies folder, buttons to Buttons folder, ...
4. Object to movie-button: does create a movie-button of selected object in stage, such as text, shape, image. Frame 1 is normal state, frame 2 is roll-over state.
5. Create transparent movie-button: does create a transparent movie-button and add it into your stage, it is useful to cover the 'popup movie', don't allow users click through the behind interactive objects.

Supported IDE: Flash CS3, CS4, CS5, with AS2 and AS3.

Download here

Monday, July 12, 2010

Image Book

VIEW

Easy mouse panning

Make the movie panning with moving mouse.


Example:
View

Source code:

stage.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoved)
stage.addEventListener(Event.ENTER_FRAME,enterFrame)
var newXPos = stage.stageWidth/-img_mc.width/2;
var newYPos = stage.stageHeight/-img_mc.height/2;
function mouseMoved(e){
    var xPercent = mouseX / stage.stageWidth;
    newXPos = (stage.stageWidth - img_mc.width* xPercent;
    var yPercent = mouseY / stage.stageHeight;
    newYPos = (stage.stageHeight - img_mc.height* yPercent;    
}

function enterFrame(e: Event) {
    img_mc.x += (newXPos - img_mc.x)*0.15;
    img_mc.y += (newYPos - img_mc.y)*0.15;
}



Code on the thumbnail map:
this.addEventListener(Event.ENTER_FRAME, onEF);
var theImage:MovieClip = (this.parent as MovieClip).img_mc;
var px:Number = 0;
var py:Number = 0;

function onEF(e: Event) {
    px = theImage.x;
    py = theImage.y;
    
    thumb_mc.x = px*thumb_mc.width/theImage.width;
    thumb_mc.y = py*thumb_mc.height/theImage.height;
    
    thumb2_mc.x = thumb_mc.x;
    thumb2_mc.y = thumb_mc.y;
}