Skip to main content
Participant
June 26, 2012
Question

Help - Addon Script Action in AS3 Flash Project

  • June 26, 2012
  • 1 reply
  • 535 views

Hello,

I need to add an action to which my script when I press a specific key and

a description to appear when I click another button the specific description of this product will disappear.

import caurina.transitions.*;
import flash.events.MouseEvent;
import flash.display.MovieClip;
import flash.events.Event;

flashmo_graphic.visible = false;
loading_info.text = "Carregando Dados...";

var folder:String = "thumbnails/";
var auto_align:Boolean = true;
var enable_fullscreen:Boolean = true;
var tween_duration:Number = 2;
var tn_border_size:Number = 0;
var tn_border_color:Number = 0xFFFFFF;
var tn_alpha:Boolean = true;
var x_spacing:Number = 300;
var y_spacing:Number = 0;
var side_tn_count:int = 8;

var stop_move:Boolean = false;
var min_x:Number;
var decrement:Number = 2;
var slope:Number;
var side_pic_scale:Number = 0.85;
var scale_diff:Number = 0.5;
var i:int;
var tn:int = 0;
var current_pic_no:int = -1;
var total_items:int;
var tn_loader:Loader = new Loader();
var flashmo_xml:XML;
var flashmo_tn_list = new Array();
var x_position_array:Array;
var scale_array:Array;
var mc:MovieClip = new MovieClip();
var thumbnail_group:MovieClip = new MovieClip();

stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.addEventListener( Event.RESIZE, resize_listener );
stage.dispatchEvent( new Event( Event.RESIZE ) );

this.addChild(thumbnail_group);

function resize_listener( e:Event 😞 void
{  
this.x = Math.floor( stage.stageWidth * 0.5 );
this.y = Math.floor( stage.stageHeight * 0.5 );
}

function switch_screen_mode( me:MouseEvent )
{  
if( stage.displayState == StageDisplayState.NORMAL )
  stage.displayState = StageDisplayState.FULL_SCREEN;
else
  stage.displayState = StageDisplayState.NORMAL;
}

function load_gallery(xml_file:String):void
{
var xml_loader:URLLoader = new URLLoader();
xml_loader.load( new URLRequest( xml_file ) );
xml_loader.addEventListener( Event.COMPLETE, create_gallery );
}

function create_gallery(e:Event):void
{
flashmo_xml = new XML(e.target.data);
total_items = flashmo_xml.thumbnail.length();

auto_align = flashmo_xml.config.@auto_align.toString() == "false" ? false : true;
enable_fullscreen = flashmo_xml.config.@enable_fullscreen.toString() == "false" ? false : true;
tn_alpha = flashmo_xml.config.@tn_alpha.toString() == "false" ? false : true;

if( flashmo_xml.config.@folder.toString() != "" )
  folder = flashmo_xml.config.@folder.toString();
  
if( flashmo_xml.config.@tween_duration.toString() != "" )
  tween_duration = parseFloat( flashmo_xml.config.@tween_duration.toString());
   
if( flashmo_xml.config.@tn_border_size.toString() != "" )
  tn_border_size = parseInt( flashmo_xml.config.@tn_border_size.toString());

if( flashmo_xml.config.@tn_border_color.toString() != "" )
  tn_border_color = flashmo_xml.config.@tn_border_color.toString();
 
if( flashmo_xml.config.@x_spacing.toString() != "" )
  x_spacing = parseFloat( flashmo_xml.config.@x_spacing.toString());

if( flashmo_xml.config.@y_spacing.toString() != "" )
  y_spacing = parseFloat( flashmo_xml.config.@y_spacing.toString());
 
if( flashmo_xml.config.@side_tn_count.toString() != "" )
  side_tn_count = parseInt( flashmo_xml.config.@side_tn_count.toString());

if( side_tn_count <= 4 )
{
  scale_diff = 0;
  side_pic_scale = 0.3;
}

fill_arrays();

for( i = 0; i < total_items; i++ )
{
  flashmo_tn_list.push( {
   filename: flashmo_xml.thumbnail.filename.toString(),
   url: flashmo_xml.thumbnail.url.toString(),
   target: flashmo_xml.thumbnail.target.toString()
  } );
}
load_tn();
}

function fill_arrays():void
{
var d:int;
var row:int;
var column:int;
var power:Number = 1;
var row_one_x:Number = x_spacing;
var row_one_scale:Number = side_pic_scale;
var x_first_row:Array = new Array( side_tn_count );
var scale_first_row:Array = new Array( side_tn_count );
var scale_ratio:Number = scale_diff / x_spacing;

slope = - y_spacing / x_spacing;

x_position_array = new Array( x_spacing + 1 );
x_position_array[0] = x_first_row;
x_first_row[0] = row_one_x;

scale_array = new Array( x_spacing + 1 );
scale_array[0] = scale_first_row;
scale_first_row[0] = row_one_scale;

for( column = 1; column < side_tn_count; column++ )
{
  power *= decrement;
  row_one_x += x_spacing * power;
  x_first_row[column] = Math.round( row_one_x );
 
  row_one_scale -= scale_diff;
  scale_first_row[column] = row_one_scale;
}
 
for( row = 1; row <= x_spacing; row++ )
{
  var x_each_row:Array = new Array( side_tn_count );
  var scale_each_row:Array = new Array( side_tn_count );
 
  x_position_array[row] = x_each_row;
  scale_array[row] = scale_each_row;
 
  power = decrement;
  d = Math.round( row * power );
  x_each_row[0] = x_position_array[0][0] + d;
  scale_each_row[0] = scale_array[0][0] - d * scale_ratio;
 
  for( column = 1; column < side_tn_count; column++ )
  {
   power *= decrement;
   d = Math.round( row * power );
   x_each_row[column] = x_position_array[0][column] + d;  
   scale_each_row[column] = scale_array[0][column] - d * scale_ratio;
  }
}
}

function load_tn():void
{
var pic_request:URLRequest = new URLRequest( folder + flashmo_tn_list[tn].filename );
 
tn_loader = new Loader();
tn_loader.load(pic_request);
tn_loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, tn_progress);
tn_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, tn_loaded);
tn++;
}

function tn_progress(e:ProgressEvent):void
{
loading_info.text = "Carregando " + tn + " de " + total_items;
loading_info_bg.width = loading_info.width;
}

function tn_loaded(e:Event):void
{
var flashmo_tn_bm:Bitmap = new Bitmap();
var flashmo_tn_mc:MovieClip = new MovieClip();

flashmo_tn_bm = Bitmap(e.target.content);
flashmo_tn_bm.smoothing = true;
flashmo_tn_bm.x = - flashmo_tn_bm.width * 0.5;
flashmo_tn_bm.y = - flashmo_tn_bm.height * 0.5;

if( tn_border_size > 0 )
{
  var bg_width:Number = flashmo_tn_bm.width + tn_border_size * 2;
  var bg_height:Number = flashmo_tn_bm.height + tn_border_size * 2;

  flashmo_tn_mc.graphics.beginFill(tn_border_color);
  flashmo_tn_mc.graphics.drawRect( - bg_width * 0.5, - bg_height * 0.5, bg_width, bg_height );
  flashmo_tn_mc.graphics.endFill();
}

flashmo_tn_mc.addChild(flashmo_tn_bm);
flashmo_tn_mc.name = "flashmo_tn_" + thumbnail_group.numChildren;
flashmo_tn_mc.alpha = 0;

thumbnail_group.addChild( flashmo_tn_mc );

if( tn < total_items )
  load_tn();
else
{
  tn_loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, tn_progress);
  tn_loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, tn_loaded);
  tn_loader = null;
 
  activate_stack_flow();
}
}

function activate_stack_flow():void
{
current_pic_no = Math.floor( total_items >> 1 );
flashmo_stack_flow( 0, false );

loading_info.text = "";
loading_info_bg.visible = false;

thumbnail_group.scaleX = thumbnail_group.scaleY = thumbnail_group.alpha = 0;
Tweener.addTween( thumbnail_group, { alpha: 1, scaleX: 1.1, scaleY: 1.1,
    time: tween_duration, transition: "easeOutBack", onComplete: activate_actions } );
}

function activate_actions():void
{
for( i = 0; i < total_items; i++ )
{
  mc = MovieClip( thumbnail_group.getChildByName("flashmo_tn_" + i) );
  mc.addEventListener( MouseEvent.CLICK, tn_click );
  mc.enabled = false;
}

stage.addEventListener( MouseEvent.MOUSE_DOWN, drag);
stage.addEventListener( MouseEvent.MOUSE_UP, drop);
stage.addEventListener( Event.MOUSE_LEAVE, stage_leave );
stage.addEventListener( MouseEvent.CLICK, stage_click, true );
 
if( enable_fullscreen )
{
  stage.doubleClickEnabled = true;
  stage.addEventListener( MouseEvent.DOUBLE_CLICK, switch_screen_mode );
}
}

function tn_click( e:MouseEvent ):void
{
thumbnail_group.removeEventListener( Event.ENTER_FRAME, tn_update );

mc = MovieClip(e.target);
current_pic_no = parseInt(mc.name.slice(11,14));

if( mc.x == 0 && mc.y == 0 )
{


  /* 


/////OPEN DESCRIPTION HERE /////
  function p_click(me:MouseEvent)
{
var sp:Sprite = me.target as Sprite;
var s_no:Number = parseInt(sp.name.slice(8,10));

tn_title.text = title_list[s_no];
tn_desc.text = description_list[s_no];
tn_valor.text = url_list[s_no];
tn_title.visible = tn_desc.visible = tn_valor.visible = false;

pic_loader.contentLoaderInfo.addEventListener(Event.OPEN, on_open);
pic_loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, on_progress);
pic_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, on_pic_loaded);
pic_loader.load(new URLRequest( folder + filename_list[s_no]));
pic_loader.alpha = 0;

url_button.visible = false;
url_button.addEventListener(MouseEvent.CLICK, goto_URL);
Tweener.addTween( container, { y: 1990, time: 0.5, transition:"easeInExpo" } );
}

//////CLOSE DESCRIPTION HERE//////

function remove_desc(e:Event):void
{
tn_title.text = "";
tn_desc.text = "";
tn_valor.text = "";
url_button.visible = false;
}
addEventListener(Event.ENTER_FRAME, render);
function render(e:Event):void
{
var distance_x:Number = (stage.mouseX - 400) * 0.0001;
angle += distance_x;
cam.x = - Math.cos(angle) * 150;
cam.z = Math.sin(angle) * 150;
scene.renderCamera(cam);
}
 
  */
}
else
{
  flashmo_stack_flow( tween_duration, false );
}
}

function flashmo_stack_flow( duration:Number, align:Boolean ):void

var move_x:Number;
var move_y:Number;
var pic_index:int;
var pic_scale:Number;
var pic_alpha:Number = 1;
var pic_depth:int;
 
if( duration > 0 )
  stage.removeEventListener( MouseEvent.MOUSE_DOWN, drag );

for( i = current_pic_no - 1; i >= 0; i-- )
{
  pic_index = current_pic_no - i - 1; 
  mc = MovieClip( thumbnail_group.getChildByName( "flashmo_tn_" + i ) );
 
  if( pic_index < side_tn_count )
  {
   move_x = - x_position_array[0][ pic_index ];
   move_y = Math.round( slope * Math.abs( move_x ) );
   pic_scale = scale_array[0][pic_index];
  
   if( tn_alpha )
    pic_alpha = pic_scale;
  
   if( duration > 0 )
    mc.addEventListener( MouseEvent.CLICK, tn_click );
  }
  else
  {
   move_x = -2000;
   move_y = 0;
   pic_alpha = 0;
   mc.removeEventListener( MouseEvent.CLICK, tn_click );
  }
  
  if( mc.x == - 2000 && move_x != -2000 )
  {
   if( align )
    mc.x = move_x;
   else
   {
    mc.x = - x_position_array[0][side_tn_count - 1] - ( x_spacing >> 1 );
    mc.scaleX = mc.scaleY = 0.2;
   }
   mc.y = - slope * mc.x;
  }
     
  mc.alpha = pic_alpha;
   
  thumbnail_group.setChildIndex( MovieClip( thumbnail_group.getChildByName("flashmo_tn_" + i) ), 0 );
 
  if( duration > 0 )
   Tweener.addTween( mc, { x: move_x, y: move_y,
         scaleX: pic_scale, scaleY: pic_scale,
         time: duration, transition: "easeOutQuart" });
  else
  {
   mc.x = move_x;
   mc.y = move_y;
   mc.scaleX = mc.scaleY = pic_scale;
  }
}

for( i = current_pic_no; i < thumbnail_group.numChildren; i++ )
{
  pic_index = i - current_pic_no - 1;
  mc = MovieClip( thumbnail_group.getChildByName( "flashmo_tn_" + i ) );
   
  if( pic_index == -1 )
  {
   move_x = move_y = 0;
   pic_depth = i;
   pic_alpha = pic_scale = 1;
  
   if( duration > 0 )
    mc.addEventListener( MouseEvent.CLICK, tn_click );
  }
  else if( pic_index < side_tn_count )
  {
   move_x = x_position_array[0][ pic_index ];
   move_y = Math.round( slope * move_x );
   pic_depth = 0;
   pic_scale = scale_array[0][pic_index];
  
   if( duration > 0 )
    mc.addEventListener( MouseEvent.CLICK, tn_click );
  
   if( tn_alpha )
    pic_alpha = pic_scale;
   else
    pic_alpha = 1;
  } 
  else
  {
   pic_alpha = 0;
   move_x = 2000;
   move_y = 0;
   mc.removeEventListener( MouseEvent.CLICK, tn_click );
  }

  if( mc.x == 2000 && move_x != 2000 )
  {
   if( align )
    mc.x = move_x;
   else
   {
    mc.x = x_position_array[0][side_tn_count - 1] + ( x_spacing >> 1 );
    mc.scaleX = mc.scaleY = 0.2;
   }
  
   mc.y = slope * mc.x;
  }
    
  mc.alpha = pic_alpha;
 
  thumbnail_group.setChildIndex( MovieClip( thumbnail_group.getChildByName("flashmo_tn_" + i) ), pic_depth );
 
  if( duration > 0 )
   Tweener.addTween( mc, { x: move_x, y: move_y, scaleX: pic_scale, scaleY: pic_scale,
        time: duration, transition: "easeOutQuart",
        onComplete: after_tween, onCompleteParams: [i, mc]});
  else
  {
   mc.x = move_x;
   mc.y = move_y;
   mc.scaleX = mc.scaleY = pic_scale;
  }
}
}

function after_tween( current_index:int, my_mc:MovieClip ):void
{
if( current_index == total_items - 1 )
  remove_update();
}

function remove_update(): void
{
stop_move = false;
stage.addEventListener( MouseEvent.MOUSE_DOWN, drag );
}

var mouse_down:Boolean;
var first_mouse_x:Number;
var old_mouse_x:Number;
var diff:Number;
var d_ease:Number;

function drag( me:MouseEvent ):void
{
mouse_down = true;
first_mouse_x = old_mouse_x = this.mouseX;
Tweener.removeAllTweens();
thumbnail_group.addEventListener( Event.ENTER_FRAME, tn_update );
}

function drop( me:MouseEvent ):void
{
mouse_down = false;
old_mouse_x = this.mouseX;
}

function stage_leave( e:Event ):void
{
mouse_down = false;
}

function stage_click( me:MouseEvent ):void
{
if( this.mouseX != first_mouse_x )
{
me.stopPropagation();
}
}

function tn_update( e:Event ):void

if( mouse_down )

  diff = this.mouseX - old_mouse_x;
  old_mouse_x = this.mouseX;
 
  if( diff > 0 )
  {
   var first_mc:MovieClip = MovieClip( thumbnail_group.getChildByName("flashmo_tn_0") );
  
   if( first_mc.x + diff > 0 )
    diff = - first_mc.x;
   else if( first_mc.x == 0 )
    diff = 0;
  }
  else if( diff < 0 )
  {
   var last_mc:MovieClip = MovieClip( thumbnail_group.getChildByName("flashmo_tn_" + ( total_items - 1 ) ) );
  
   if( last_mc.x + diff < 0 )
    diff = - last_mc.x;
   else if( last_mc.x == 0 )
    diff = 0;
  }
 
  d_ease = diff;
}
else
{
  d_ease = diff * 0.2;
  diff -= d_ease;
   
  if( Math.abs( diff ) < 1 )
  {
   thumbnail_group.removeEventListener( Event.ENTER_FRAME, tn_update);
   stop_move = true;
  
   if( auto_align )
    flashmo_stack_flow( tween_duration, true );
  }
}

if( d_ease != 0 )
{
  var mid_pic_right:int = -1;
  var mid_pic_left:int = -1;
  var d:Number = 0;
 
  mc = MovieClip( thumbnail_group.getChildByName("flashmo_tn_" + current_pic_no) );
  min_x = mc.x;
 
  while( true )
  {
   if( d_ease > 0 )
   {
    if( min_x < 0 )
    {
     d = - min_x;
     if( d > d_ease )
      d = d_ease;
     mid_pic_left = current_pic_no;
     mid_pic_right = current_pic_no + 1;
    }
    else
    {
     d = x_spacing - min_x;
     if( d > d_ease )
      d = d_ease;
     mid_pic_left = current_pic_no - 1;
     mid_pic_right = current_pic_no;
    }
   }
   else
   {
    if( min_x <= 0 )
    {
     d = - ( x_spacing + min_x );
     if( d < d_ease )
      d = d_ease;
     mid_pic_left = current_pic_no;
     mid_pic_right = current_pic_no + 1;
    }
    else
    {
     d = - min_x;
     if( d < d_ease )
      d = d_ease;
     mid_pic_left = current_pic_no - 1;
     mid_pic_right = current_pic_no;
    }
   }
  
   move_d( d, mid_pic_left, mid_pic_right );
   d_ease -= d;
  
   if( d_ease == 0 )
   {
    break;
   }
  }
}
}

function move_d( d:Number, mid_pic_left:int, mid_pic_right:int ):void
{
try
{
  if( stop_move )
  {
   stop_move = false;
   return;
  }
   
  var new_x:Number;
  var new_scale:Number;
  var new_alpha:Number = 1;
  var left_wing_row:int;
  var right_wing_row:int;
  var scale_ratio:Number = scale_diff * d / x_spacing;
  var abs_x:Number;
  var right_pic_x:Number;
  
  if( mid_pic_left > -1 )
  {
   mc = MovieClip( thumbnail_group.getChildByName("flashmo_tn_" + mid_pic_left) );
   mc.addEventListener( MouseEvent.CLICK, tn_click );
  
   if( d > 0 )
   {
    if( mc.x < 0 )
     mc.scaleX = mc.scaleY = mc.scaleX  + scale_ratio; 
    else
     mc.scaleX = mc.scaleY = mc.scaleX  - scale_ratio;
   }
   else
   {
    mc.scaleX = mc.scaleY =  mc.scaleX + scale_ratio;
   }
  
   if( mc.scaleX > 1 )
    mc.scaleX = mc.scaleY = 1;
    
   mc.x = mc.x + d;
   mc.y = - slope * mc.x;
  
   if( tn_alpha )
    mc.alpha = mc.scaleX;
   else
    mc.alpha = 1;
     
   left_wing_row = get_position_row( mc.x );
   min_x = Math.abs( mc.x );
   current_pic_no = mid_pic_left;
  }
 
  if( mid_pic_right > -1 && mid_pic_right < total_items )
  {
   mc = MovieClip( thumbnail_group.getChildByName("flashmo_tn_" + mid_pic_right) );
   mc.addEventListener( MouseEvent.CLICK, tn_click );
  
   if( d > 0 )
   {
    mc.scaleX = mc.scaleY = mc.scaleX - scale_ratio;  
   }
   else
   {
    if( mc.x > 0 )
     mc.scaleX = mc.scaleY = mc.scaleX - scale_ratio;
    else
     mc.scaleX = mc.scaleY = mc.scaleX + scale_ratio;
   }
   if( mc.scaleX > 1 )
    mc.scaleX = mc.scaleY = 1;
    
   mc.x = mc.x + d;
   mc.y = slope * mc.x;
   right_pic_x = mc.x;
  
   if( tn_alpha )
    mc.alpha = mc.scaleX;
   else
    mc.alpha = 1;
    
   right_wing_row = get_position_row( mc.x );
   abs_x = Math.abs( mc.x );
    
   if( abs_x < min_x )
   {
    min_x = abs_x;
    current_pic_no = mid_pic_right;
   }
  }
 
  var pic_index:int;
 
  for( i = mid_pic_left - 1; i >= 0; i-- )
  {
   pic_index = mid_pic_left - i - 1;
  
   mc = MovieClip( thumbnail_group.getChildByName("flashmo_tn_" + i) );
  
   if( pic_index < side_tn_count )
   {
    mc.addEventListener( MouseEvent.CLICK, tn_click );
    mc.x = - x_position_array[left_wing_row][pic_index];
    mc.y = - slope * mc.x;
    mc.scaleX = mc.scaleY = scale_array[left_wing_row][pic_index];
   
    if( mc.scaleX > 1 )
     mc.scaleX = scaleY = 1;
    
    if( tn_alpha )
     mc.alpha = mc.scaleX;
    else
     mc.alpha = 1;
    
    if( left_wing_row > 0 && pic_index == side_tn_count - 1 )
    {
     mc.x = - 2000;
     mc.y = 0;
     mc.alpha = 0;
    }
   }
   else
   {  
    mc.removeEventListener( MouseEvent.CLICK, tn_click );
    mc.x = - 2000;
    mc.y = 0;
    mc.alpha = 0;
   }
    
   abs_x = Math.abs( mc.x );
  
   if( abs_x < min_x )
   {
    min_x = abs_x;
    current_pic_no = i;
   }
  
   sort_group( thumbnail_group );
  }
 
  for( i = mid_pic_right + 1; i < total_items; i++ )
  {  
   if( right_pic_x == x_spacing )
    pic_index = i - mid_pic_right;
   else
    pic_index = i - mid_pic_right - 1;
  
   mc = MovieClip( thumbnail_group.getChildByName("flashmo_tn_" + i) );
  
   if( pic_index < side_tn_count )
   {
    mc.addEventListener( MouseEvent.CLICK, tn_click );
    mc.x = x_position_array[right_wing_row][pic_index];
    mc.y = slope * mc.x; 
    mc.scaleX = mc.scaleY = scale_array[right_wing_row][pic_index];
   
    if( tn_alpha )
     mc.alpha = mc.scaleX;
    else
     mc.alpha = 1;

    if( right_wing_row > 0 && pic_index == side_tn_count - 1 )
    {
     mc.x = 2000;
     mc.y = 0;
     mc.alpha = 0;
    }
   }
   else
   {
    mc.removeEventListener( MouseEvent.CLICK, tn_click );
    mc.x = 2000;
    mc.y = 0;
    mc.alpha = 0;
   }
      
   abs_x = Math.abs( mc.x );
  
   if( abs_x < min_x )
   {
    min_x = abs_x;
    current_pic_no = i;
   }
  
   sort_group( thumbnail_group );
  }
}
catch( e:Error )
{
  trace( e.message );
}
}

function get_position_row( x_position:Number ):int
{
for( var r:int = 9; r <= x_spacing; r++ )
{
  if( x_position_array[0] == x_position )
  {
   return r;
  }
}

return Math.abs( x_position );
}

function sort_group( group:MovieClip ):void
{
var i:int;
var child_list:Array = new Array();

i = group.numChildren;

while(i--)
  child_list = group.getChildAt(i);

child_list.sortOn( "scaleX", Array.NUMERIC );
i = group.numChildren;

while(i--)
{
  if( child_list != group.getChildAt(i) )
   group.setChildIndex(child_list, i);
}
}

>>>>> full PROJECT www.solutionvix.com/projec_as3_help.rar

This topic has been closed for replies.

1 reply

Ned Murphy
Legend
June 26, 2012

With what part of what you want to do are you having a problem?  Post only the code that is giving you a problem - I doubt anyone is going to read all of what you just provided to try to figure out how your vague description fits in with it..