Archive for the ‘ActionScript3’ Category

[AS3] When I load local movie file, security error has occured.

Sunday, October 23rd, 2011

I usually develop flash content with Flash Builder.

First I output the swc file from Flash CS 5 application.
And then, I import it from FB.
And I compile it to swf.

I tried to load the movie file in local file system.
But an Error dialog has been appeared.

The dialog said this.

SecurityError: Error #2148: SWF file file:///Users/kinkuma/Documents/temp/main.swf cannot access local resource ./video/samplef4v.f4v. Only local-with-filesystem and trusted local SWF files may access local resources.

I googled it. And I found how to solve it.
This page shows
Only local-with-filesystem and trusted local SWF files may access local resources

It says I have to add the compiler settings.
Project > Properties > ActionScript Compiler > Additional compiler arguments

-use-network=false

I can fix it!

Caution
If you load external swf files, you have to make all swf files same network policy.

Additional info
You can select the above settings with Flash Application.

File > Publish Settings > Local playback security

[AS3] Debugging utilities KKConsole and KKLogger

Tuesday, August 9th, 2011

I made two classes for debugging.

KKConsole

It output to the browser’s console.log.
It is difficult to use trace command after you upload content to server.
But it is easy to read logs in browser.

console_fig1

Source code

package kinkuma.util
{
	import flash.external.ExternalInterface;

	public class KKConsole
	{
		public static function log(text:String):void
		{
			if(ExternalInterface.available){
				ExternalInterface.call("console.log", text);
			}
		}
	}
}

Usage

package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import kinkuma.util.KKConsole;
	
	[SWF(width="450",height="450",backgroundColor="0xffffff")]
	public class KKConsoleUsage extends Sprite
	{
		private var _clickTimes:int = 0;
		
		public function KKConsoleUsage()
		{
			addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
		}
		
		protected function onAddedToStage(event:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			stage.addEventListener(MouseEvent.CLICK, onStageMouseClick);
		}
		
		protected function onStageMouseClick(event:MouseEvent):void
		{
			_clickTimes++;
			var logText:String = "You clicked " + _clickTimes + " time";
			if(_clickTimes > 1){
				logText += "s";
			}
			KKConsole.log(logText);
		}
	}
}

The class like this one has already exited.
It can parse argument’s string to be easy to read.

>> AS3 でどこからでも log();(in Japanese)

KKLogger

It output log on a black rectangle.
It is very useful when you can’t use browser’s console.
For example, Android device can’t be used console.

Source code

package kinkuma.util
{
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	public class KKLogger extends Sprite
	{
		private var _textField:TextField;
		private var _bg:Shape;
		
		public function KKLogger(aWidth:int = 200, aHeight:int = 150)
		{
			_textField = new TextField();
			_textField.multiline = true;
			_textField.width = aWidth;
			_textField.height = aHeight;
			addChild(_textField);
			var tf:TextFormat = new TextFormat();
			tf.font = "_sans";
			tf.color = 0xffffff;
			tf.size = 10;
			_bg = new Shape();
			addChildAt(_bg, 0);
			var g:Graphics = _bg.graphics;
			g.beginFill(0x000000, 0.8);
			g.drawRect(0,0,aWidth, aHeight);
			g.endFill();
			_textField.defaultTextFormat = tf;
			mouseChildren = mouseEnabled = false;
		}
		
		public function log(text:String):void
		{
			if(_textField.text == ""){
				_textField.text = text;
			}else{
				_textField.appendText("\n" + text);
			}
			var tft:String = _textField.text;
			var ar:Array = tft.split("\r");
			while(_textField.textHeight + 5 > _textField.height){
				ar.shift();
				_textField.text = ar.join("\n");
			}
		}
	}
}

Usage

package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import kinkuma.util.KKLogger;
	
	[SWF(width="450",height="450",backgroundColor="0xffffff")]
	public class KKLoggerUsage extends Sprite
	{
		private var _clickTimes:int = 0;
		private var _logger:KKLogger;
		
		public function KKLoggerUsage()
		{
			_logger = new KKLogger();
			_logger.x = 10;
			_logger.y = 10;
			addChild(_logger);
			
			addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
		}
		
		protected function onAddedToStage(event:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			stage.addEventListener(MouseEvent.CLICK, onStageMouseClick);
		}
		
		protected function onStageMouseClick(event:MouseEvent):void
		{
			_clickTimes++;
			var logText:String = "You clicked " + _clickTimes + " time";
			if(_clickTimes > 1){
				logText += "s";
			}
			_logger.log(logText);
		}
	}
}

I uploaded it to wonderfl

KKLogger – wonderfl build flash online

It was also uploaded to github

I uploaded to github.
>> https://github.com/KinkumaDesign/KKDebug

[AS3] How to create one line editable TextFlow of Text Layout Framework

Monday, July 25th, 2011

I want to create one line editable TextFlow, not multiple line, with Text Layout Framework(TLF).

First, you need to create a subclass of EditManager.
This class can change the behavior when keyboard is pressed.

package
{
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	
	import flashx.textLayout.edit.EditManager;
	import flashx.undo.IUndoManager;
	
	public class CustomEditManagner extends EditManager
	{
		public function CustomEditManagner(undoManager:IUndoManager=null)
		{
			super(undoManager);
		}
		
		override public function keyDownHandler(event:KeyboardEvent):void
		{
			if(event.keyCode != Keyboard.ENTER){
				super.keyDownHandler(event);
			}		
		}
	}
}

Second, you create the TextFlow, and assign above custom class to its interactionManager.

package
{
	import flash.display.Sprite;
	import flashx.textLayout.formats.LineBreak;
	
	import flashx.textLayout.container.ContainerController;
	import flashx.textLayout.conversion.TextConverter;
	import flashx.textLayout.edit.EditManager;
	import flashx.textLayout.elements.TextFlow;
	
	[SWF(width="450", height="450", frameRate="30")]
	public class OneLineEditableTLF extends Sprite
	{
		public function OneLineEditableTLF()
		{
			createTextLine();
		}
		
		private function createTextLine():void
		{
			var sp:Sprite = new Sprite();
			addChild(sp);
			sp.x = 100;
			sp.y = 50;
			
			var markup:XML = 

Hello, World

; var textFlow:TextFlow = TextConverter.importToFlow(markup, TextConverter.TEXT_LAYOUT_FORMAT); var containerController:ContainerController = new ContainerController(sp, 200, 16); textFlow.flowComposer.addController(containerController); var editManager:EditManager = new CustomEditManagner(); textFlow.interactionManager = editManager; textFlow.fontSize = 16; textFlow.lineBreak = LineBreak.EXPLICIT; textFlow.flowComposer.updateAllControllers(); } } }

[iOS] EventDispatcher Class in Objective-C

Tuesday, December 28th, 2010

I am a iOS developer, and also a Flash Developer.
I wanted to EventDispatcher class in objective-c.
And I implemented it.

*update Feb.8.2011

IEventDispatcher.h

#import <Foundation/Foundation.h>

@protocol IEventDispatcher
- (void)addEventListener:(NSString *)type target:(id)target listener:(SEL)listener;
- (void)removeEventListener:(NSString *)type target:(id)target;
- (void)dispatchEvent:(NSString *)type;
@end

EventDispatcher.h

#import <Foundation/Foundation.h>
#import "IEventDispatcher.h"

@interface EventDispatcher : NSObject <IEventDispatcher> {
	NSMutableDictionary *targets;
}
@property (nonatomic, retain) NSMutableDictionary *targets;
- (void)addEventListener:(NSString *)type target:(id)target listener:(SEL)listener;
- (void)removeEventListener:(NSString *)type target:(id)target;
- (void)dispatchEvent:(NSString *)type;
@end

EventDispatcher.m


#import "EventDispatcher.h"


@implementation EventDispatcher
@synthesize targets;

- (id)init
{
	if ((self = [super init])) {
		NSMutableDictionary *mytargets = [[NSMutableDictionary alloc] init];
		self.targets = mytargets;
		[mytargets release];
	}
	return self;
}

- (void)addEventListener:(NSString *)type target:(id)target listener:(SEL)listener
{
	NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
	NSString *identifier = [type stringByAppendingFormat:@"_%d", &(*target)];
	if ([targets valueForKey:identifier] == nil) {
		[center addObserver:target selector:listener name:type object:self];
		[targets setObject:target forKey:identifier];
	}
}

- (void)removeEventListener:(NSString *)type target:(id)target
{
	NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
	NSString *identifier = [type stringByAppendingFormat:@"_%d", &(*target)];
	if ([targets valueForKey:identifier] != nil) {
		[center removeObserver:target name:type object:self];
		[targets removeObjectForKey:identifier];
	}
}

/**
 * Override this method. if you want to custom userInfo
 */
- (void)dispatchEvent:(NSString *)type
{
	NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
	[center postNotificationName:type object:self userInfo:nil];
}

- (void)dealloc
{
	NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
	for(NSString *identifier in targets){
		NSArray *idenntifierArr = [identifier componentsSeparatedByString:@"_"];
		[center removeObserver:[targets objectForKey:identifier] 
						  name:[idenntifierArr objectAtIndex:0] 
						object:self];
	}
	[targets removeAllObjects];
	[targets release];
	[super dealloc];
}
@end

how to use

set handler method

- (void)testHandler:(NSNotification *)aNotification
{
	NSLog(@"test handler");
	
	NSLog(@"name is %@", aNotification.name);
	NSLog(@"object is %@", aNotification.object);
	NSLog(@"info is %@", aNotification.userInfo);
}

and test

    EventDispatcher *myEd = [[[EventDispatcher alloc] init] autorelease];
	[myEd addEventListener:@"EventComplete" target:self listener:@selector(testHandler:)];
	[myEd dispatchEvent:@"EventComplete"];

Sample project file is here.

This article is very useful. Thanks
>> ustom Events in Objective-C | Raphael Wichmann