공부놀이/ActionScripT 2007. 10. 24. 17:13

[3.0] 커스텀 버튼 클래스

package CustomButton {
 import flash.display.Sprite;
 import flash.events.MouseEvent;
 import flash.geom.ColorTransform;
 import flash.text.TextField;
 import flash.text.TextFormat;
 import flash.text.TextFormatAlign;

 public class CustomButton extends Sprite
 {
  private var btnTarget:Sprite;

  private var lbButton:TextField;
  private var lbFormat:TextFormat;
  private var btnName:String;
 
  private var btnColor:uint;
  private var isToggle:Boolean;
  private var isTargeted:Boolean;
 
  private var _toggled:Boolean;
 
  public function CustomButton(mcTarget:Sprite = null, color:uint = 0xffcc33)
  {
   if(mcTarget == null) {
    btnTarget = new Sprite();
    isTargeted = false;
    btnColor = color;
   } else {
    btnTarget = mcTarget;
    isTargeted = true;
    color = 0x000000;
   }
   btnName = "Button";
  }
 
  public function get toggled():Boolean { return _toggled; }
 
 
  public function activate():void
  {
   this.buttonMode = true;
   this.useHandCursor = true;
   draw_button();
  }
 
  public function destroy():void
  {
   isAlive(false);
   this.buttonMode = false;
   
   var dsFormat:TextFormat = new TextFormat();
   dsFormat.bold = false;
   dsFormat.color = 0x777777;
   
   lbButton.setTextFormat(dsFormat);
   
   btnTarget.removeEventListener(MouseEvent.MOUSE_OVER, over_listener);
   btnTarget.removeEventListener(MouseEvent.MOUSE_OUT, out_listener);
   btnTarget.removeEventListener(MouseEvent.MOUSE_UP, up_listener);
   btnTarget.removeEventListener(MouseEvent.MOUSE_DOWN, down_listener);
  }
 
  public function setToggle(val:Boolean):void
  {
   if(val) {
    isToggle = true;
    make_toggle();
   } else {
    isToggle = false;
   }
  }
 
  public function set label(button_name:String):void
  {
   btnName = button_name;
   addLabel();
  }
 
  public function get label():String
  {
   return btnName;
  }
 
  private function addLabel():void
  {
   if(lbButton == null){
    lbButton = new TextField();
    lbButton.width = btnTarget.width;
    lbButton.height = 20;
    lbButton.x = (this.width - lbButton.width)/2;
    lbButton.y = (this.height - lbButton.height)/2;
    lbButton.mouseEnabled = false;
   }
   
   lbFormat = new TextFormat();
   if(!isToggle)
    lbFormat.color = 0xFFFFFF;
   lbFormat.bold = false;
   lbFormat.size = 10;
   lbFormat.align = TextFormatAlign.CENTER;
   
   lbButton.text = btnName;
   
   lbButton.setTextFormat(lbFormat);
   
   this.addChild(lbButton);
  }
 
  private function addListeners():void
  {
   btnTarget.addEventListener(MouseEvent.MOUSE_OVER, over_listener);
   btnTarget.addEventListener(MouseEvent.MOUSE_OUT, out_listener);
   btnTarget.addEventListener(MouseEvent.MOUSE_DOWN, down_listener);
   btnTarget.addEventListener(MouseEvent.MOUSE_UP, up_listener);
  }
 
  private function over_listener(evt:MouseEvent):void
  {
   //trace("over");
   var ovFormat:TextFormat = new TextFormat();
   ovFormat.bold = false;
   ovFormat.color = 0xFFFF00;
   
   lbButton.setTextFormat(ovFormat);
  }
 
  private function down_listener(evt:MouseEvent):void
  {
   //trace("down");
   var dnFormat:TextFormat = new TextFormat();
   dnFormat.bold = false;
   dnFormat.color = 0xCCCCCC;
   
   lbButton.setTextFormat(dnFormat);
  }
 
  private function up_listener(evt:MouseEvent):void
  {
   //trace("up");
   lbButton.setTextFormat(lbFormat);
   //dispatchEvent(new MouseEvent("customCLICK", evt.bubbles, evt.cancelable, evt.localX, evt.localY, evt.relatedObject, evt.ctrlKey, evt.altKey, evt.shiftKey, evt.buttonDown, evt.delta));
   //dispatchEvent(evt);
  }
 
  private function out_listener(evt:MouseEvent):void
  {
   //trace("out");
   lbButton.setTextFormat(lbFormat);
  }
 
  private function draw_button():void
  {
   if(!isTargeted){
    //trace("drawing button elements");
    btnTarget.graphics.beginFill(btnColor);
    btnTarget.graphics.drawRoundRect(0, 0, 80, 20, 5);
    btnTarget.graphics.endFill();
   }
   this.addChild(btnTarget);
   addListeners();
   isAlive(true);
   addLabel();
  }
 
  private function isAlive(val:Boolean):void
  {
   //trace("Button status change");
   var rOffset:Number;
   var gOffset:Number;
            var bOffset:Number;
   if(val) {
             rOffset = 0;
             gOffset = 0;
             bOffset = 0;
   } else {
    rOffset = -125;
             gOffset = -125;
             bOffset = -125;
   }  
   btnTarget.transform.colorTransform = new ColorTransform(1, 1, 1, 1, rOffset, gOffset, bOffset);
  }
 
  private function make_toggle():void
  {
   btnTarget.removeEventListener(MouseEvent.MOUSE_OVER, over_listener);
   btnTarget.removeEventListener(MouseEvent.MOUSE_OUT, out_listener);
   btnTarget.removeEventListener(MouseEvent.MOUSE_UP, up_listener);
   btnTarget.removeEventListener(MouseEvent.MOUSE_DOWN, down_listener);
   
   btnTarget.addEventListener(MouseEvent.CLICK, toggle_listener);
   _toggled = false;
   
   var tgFormat = new TextFormat();
   tgFormat.bold = false;
   tgFormat.color = 0x777777;
   
   lbButton.setTextFormat(tgFormat);
  }
 
  private function toggle_listener(evt:MouseEvent):void
  {
   var tgFormat = new TextFormat();
   if(_toggled) {
    _toggled = false;
    tgFormat.bold = false;
    tgFormat.color = 0x777777;
    dispatchEvent(new MouseEvent("customOFF", evt.bubbles, evt.cancelable, evt.localX, evt.localY, evt.relatedObject, evt.ctrlKey, evt.altKey, evt.shiftKey, evt.buttonDown, evt.delta));
   } else {
    _toggled = true;
    tgFormat.bold = true;
    tgFormat.color = 0xFFFF00;
    dispatchEvent(new MouseEvent("customON", evt.bubbles, evt.cancelable, evt.localX, evt.localY, evt.relatedObject, evt.ctrlKey, evt.altKey, evt.shiftKey, evt.buttonDown, evt.delta));
   }
   lbButton.setTextFormat(tgFormat);
   //trace(_toggled);
  }
 }
}

한번 만들어봤습니다만.

3.0 에서는 굳이 이런식으로 만들 필요 없이 걍 SimpleButton 클래스를 사용하면 될 것 같습니다 -_-

그래도... 요녀석의 장점이라면... 그냥 쓰면 임의대로 Sprite 에서 버튼 모양을 그려준다는 겁니다.
그 버튼의 색도 지정할 수 있구요..
토글식으로 만들어 줄 수도 있습니다.
이경우에는 이벤트 리스너를 걸어줄 때 "customON", "customOFF" 라는 식으로 문자로 걸어주는거죠
뭐 저런건 걍 스태틱 클래스로 모아서 만들어줘도 됩니다만, 아직 제 라이브러리가 없어서 그냥 씁니다 ㅎ

사용법은 간단합니다.
import CustomButton.CustomButton;
// 우선은 Import 를 해줘야겠지요
// package 는 customUI.CustomButton 이지만.. 마음껏 수정하셔도 됩니당
var buttonInstance:CustomButton = new CustomButton(null, 0x000000);
// 앞에는 버튼이 될 무비클립, 요기서 아무것도 지정을 안해주면(null) 임의로 그려줍니다.
// 뒤에는 임의로 그려줄때 버튼의 색으로 쓸 uint 값 그냥 웹컬러 코드에 0x 붙는 시츄 -_-
buttonInstance.activate()
// 버튼 활성화를 해줘야 합니다.
buttonInstance.destroy()
// 버튼 비활성화 메서드
buttonInstance.setToggle(true)
// true, false 로 토글 버튼으로 쓸건지 말건지 정해줍니다


일단 동작은 잘 합니다.
충분한 테스트를 거치치 않은 미완성이긴 하지만... 공부겸 해서 올려둡니다.
,
공부놀이/ActionScripT 2007. 10. 16. 11:47

[3.0] Depth 처리

액션스크립트 3.0 에서는 기존의 액션스크립트 2.0 및 1.0 에서 사용하던
Stage 및 MovieClip 의 구조와 체계가 달라졌습니다.
그래서 Depth 관리 방식도 기존과는 많이 달라졌지요.
 
3.0 에서는 모든 MovieClip 뿐 아니라 Shape, 혹은 새로 생긴 Sprite 등등이 모두 DisplayObject 를 상속하며, 트리구조로 Stage 에(정확히는 DisplayObjectContainer에) 들어가게 됩니다.

뭐 E4X 의 Event 방식을 따르기 위해서 그리 되었는지는 모르겠지만, 관리면에 있어서는 한결 편해진듯 싶군요.

그러한 연유로 3.0 에서는 swapDepths와 getNextHighestDepth() 가 사라져 버렸습니다.
그 MovieClip 혹은 Shape, Sprite 가 언제 생성되었던 간에
addChild 메서드를 사용해서 Stage 에 발라주는 순서에 따라서 위아래가 바뀌게 됩니다.
먼저 addChild 한 녀석이 아래쪽에 붙게 되지요.

하지만 우리가 DisplayObject(MovieClip, Shape, Sprite) 를 사용하다 보면 아래에 있던 녀석을 위로 올린다던가 하는 처리가 필요할 경우가 생기기 마련입니다.

그래서 3.0 에서는 Depth를 처리하기 위해서 몇가지의 메서드를 제공합니다.
그중에서 setChildIndex 만 사용해도 쉽사리 Depth 를 바꿀 수 있습니다.
그러나 2.0 에서 쓰듯이 아무렇게나 Depth 를 지정해서 사용할 수는 없지요

예를 들자면 Stage 에는 TargetName 이라는 녀석을 포함한 DisplayObject 들이 세개정도가 있다고 하면,
setChildIndex(TargetName, 1000000)
이런식으로는 안된다는 겁니다.

그래서 간절해지는 녀석은 2.0 시절의 getNextHighestDepth 인데... 사라지고 없는 녀석입니다.

하지만 역시 또 제공되는 녀석이 있지요.
3.0 에서 새로 생긴 녀석인 Sprite에서 numChildren을 읽어올 수 있습니다.

이녀석은 자신의 자식 객체수를 담고 있습니다. (트리구조를 이루고 있으니까요)
물론 읽기 전용 변수로서 임의대로 갱신할 수는 없습니다.

getNextHighestDepth 처럼 최상위 Depth를 구하기 위해서는 저 변수에 -1 을 해주면 해당 객체의 자식들 중 최상위의 Depth 를 구할 수 있는 것이지요.

그렇게 최상위 위치를 구해서 SetChildIndex 를 해주면, 이전의 위치가 비어버리는것도 아니고,
기존 위치에 있던 녀석이 사라지는것도 아닙니다. 자동으로 Depth 들이 재조정 됩니다.
일일이 신경써줄 필요가 없어지는 거지요.

MovieClip 이나 Sprite 등 DisplayObject 에 대해서는 나중에 따로 다루도록 하지요~~
,
공부놀이/ActionScripT 2007. 2. 28. 04:33

[2.0] 플래시 타임라인, 다이나믹, 리버스 뎁스 관련

Zones of Depth

In specifying depths for your objects, you need to be aware of the three "zones" in the ranges of valid depth values in Flash. Depths themselves, for visual objects, cannot be less than -16,384 or greater than 2,130,690,045 (there is an exception for createEmptyMovieClip). Indeed, unusual numbers, but those are the acceptable values for an object's depth. No depth will be outside of this range and, as you can see, Flash starts with the lowest part of that range when adding objects to the screen when reading the timeline. Now, within that range there are more or less 3 other ranges. One is the range that Flash uses to handle the timeline, that being from depths -16,384 to -1. Following that is the range, 0 to 1,048,575, the range that specifies the depths at which a movieclip or textfield can be removed with removeMovieClip() or removeTextField(), something not possible within the depths of the previous range. After that range, and going to the end, 1,048,576 to 2,130,690,04, is a kind of reserve range where movieclips can be placed by you, but can't be removed. Respectively I call these ranges, or zones, timeline, dynamic and reserve:

Zone Range Note
 Timeline  -16,384 to -1 handled by timeline with Flash, dynamic removal disabled
 Dynamic  0 to 1,048,575 most dynamically accessible, allows dynamic removal
 Reserve  1,048,576 to 2,130,690,045 overflow - ignored by timeline, dynamic removal disabled

The timeline zone of depths is where Flash handles timeline placed instances, working from around -16384 up giving plenty of depths before reaching the dynamic zone at depth 0. These are all based on arrangement specified in the Flash authoring environment. The very low starting depth mentioned before, where the lowest clips in the Flash authoring environment are first placed, starts at around this lowest depth of -16,384 (usually -16,383). From there it places objects in sequentially higher depths until all of the objects on the timeline are accounted for. Since there are only 16,000 instances allowed on the screen at one time in Flash, that gives plenty of depths for Flash to handle those timeline instances. Now, there's a little more involved here, but we'll touch upon that a bit later.

Next is the dynamic zone. This represents the range in which most dynamic instances would be, ideally, created. After all, intuitively, if Flash asks you to give a depth for an attached movieclip, you would consider some reasonable positive number value and nothing negative or anything too terribly high such as anything above 1,048,575. This doesn't mean that any object created dynamically on the screen has to be within this range. It's just the supposed range for these clips to exists since it is only within this range, and this is the important part, that you are allowed to remove instances with actionscript. Dynamically created instances, however, can be initially attached within any valid depth within any of the 3 zones. Should you want to remove them, however, you would want to place them here. And if not, then you would have to use swapDepths to place them within this zone to remove them. Note, this is valid for instances placed on the timeline as well. Though you may not initially be able to remove any movieclip you place on the timeline in the Flash authoring environment, should you use swapDepths to place that clip in the Dynamic zone, you are then able to remove it with removeMovieClip.

The reserve zone kind of makes up what's left. Its basically just an extension of the dynamic zone with removal disabled. Objects can be created there or swapped to a depth within that zone using swapDepths, but it deems itself unorthodox in that there is no ability for removal and that the simple value of the depth seems ridiculously high. Nevertheless, this allows 2,129,641,470 more values of depths to work with when arranging your objects on the screen through actionscript giving you a lot of room for playing around.

// swap to an arbitrary depth within removable range
my_mc.swapDepths(10000);
// no ok to remove, no matter how it was created or where
my_mc.removeMovieClip();

The exception to this set of ranges is for the createEmptyMovieClip method. There is no real limit for the depth when creating an empty movieclip. You can very well define it with a depth below -99999999999 or above 999999999999 and Flash will still create the empty clip despite the fact its out of the 3 valid zone's ranges. The depth of such an empty clip, however, when set beyond 2147483647 on the positive side and -2147483648 on the negative, it will start to fluctuate from what you truly set it, sometimes even being negative if set at positive and vise versa. You never can tell. It seems as though this is the absolute range for timeline instances, -2147483647 to -2147483648, though really only fully attainable through empty movieclips. Other clips, such as those within the timeline or created with something like attachMovie are restricted to the depths of -16,384 to 2,130,690,045 as laid out in the 3 zones.

The Timeline Refresh

Lets go back to the timeline zone for a moment. There is something that happens within that range that can cause a lot of confusion when toying with the depths of clips there, both dynamic and those placed in the timeline. What happens is, whenever the timeline is "refreshed" and all of the instances are redrawn, Flash actually goes through the depths in the timeline zone and removes all of the clips which exist within those depths, whether they were created there through their existence on the timeline or dynamically with actionscript. Flash then redraws the screen for those depths based on the information within the timeline disregarding any movieclips that might have been placed in those depths through dynamic means. An example of this kind of refreshing is caused by using gotoAndPlay to go to a frame which has already passed; going to frame 5 from frame 10 for example. When this happens, Flash does this refresh allowing it to correctly display the screen at this new frame as the progression of the timeline has diverted from normal playback and therefore must be completely re-conceived by Flash.

Obviously, this can cause several problems. One being the obvious of you losing any dynamically place instances within that range during a refresh of this sort, placed there either during their creation or as a result of swapDepths. What can you do? Well, you can not put dynamic instances in that range for one. Alternatively, if you know that you wont run into a situation where something like that will occur, say a controlled movieclip consisting of only one stopped frame, then it can be pretty safe to have clips within the range of the timeline zone with minimal worry.

Another problem, which may not be so obvious, is that concerning depth-swapped timeline instances. If you use swapDepths to bring a movieclip which was placed on the timeline in Flash out of the timeline zone and into a depth above 0, then in a timeline refresh, that clip will not be removed in Flash's clearing of the timeline instances and a new instance of that clip will be placed on the screen at its original depth, effectively duplicating it. Similarly, to avoid this, don't do it. Again, its safe in controlled timelines, but otherwise, its risky. Something to be aware of nonetheless.

Kind of along those same lines is the progression of the Flash timeline from one scene to another. This does not constitute a timeline refresh, nor, really, any other particularly special event. Scenes themselves are only slices of the main timeline which, when the Flash movie is published, get added on to one another as one big, single main timeline. Because of this, any dynamic instance you create (or any instance swapped above 0 depth) in one scene will be present in any other scene as the timeline plays through them. If you would not like this to happen, then you would need to use removeMovieClip/removeTextField to get rid of them at the end of the scene which they were placed.

Execution Order

Another somewhat overlooked role in depths is their influence in determining execution order of ActionScript. Actually, the depths themselves don't necessarily affect execution order but rather the order in which instances are created does. However, since movieclips are created in the order relating to their arrangement (depth) on the screen, there is that connection, at least in terms of the timeline. In regards to actionscript, its more a matter of the order in which you create any of those instances that you decide to make. Those you create first (these in the same timeline) will have their script run before those you create afterwards. Otherwise, the Flash authoring environment decides the order of execution based on arrangement or order created. Oddly enough, for instances on the timeline, those created last are those who's enterFrame event scripts get run first.

By default, instances are created from the timeline starting with the lowest layer upward. This is at the Bottom Up load order setting in you Publish Settings options. You have two options for load order, Bottom Up and Top Down, both which control the order in which layers are created within Flash. Frame scripts in these layers are run in the order they are created, so in a Bottom Up situation, any scripts on lower layers would be technically run first. However, as mentioned, enterFrame events are opposite that -and this includes both onEnterFrame and onClipEvent(enterFrame). In a Top Down situation, the opposite is true. So, with the default Bottom Up setting, assuming you have two movieclips on your timeline, one in a high layer 1 and the other in a lower layer 2, if both contain an onEnterFrame event as well as some script in the first frame of their timeline, then the movieclip in the lower layer will first have its timeline script run followed by the timeline script of the movieclip on the higher layer. Then the enterFrame event of the clip in the higher layer will run followed by that corresponding to the lower layer. Note, however, that script order within

'공부놀이 > ActionScripT' 카테고리의 다른 글

[3.0] 커스텀 버튼 클래스  (0) 2007.10.24
[3.0] Depth 처리  (0) 2007.10.16
[2.0] 플래시 웹페이지 포커스  (0) 2007.02.28
,
공부놀이/ActionScripT 2007. 2. 28. 04:31

[2.0] 플래시 웹페이지 포커스

Giving keyboard focus to an embedded Flash movie

Issue

A Flash movie embedded in an HTML page does not receive key events when the page first loads in a Web browser.

Reason

In order for a Flash movie to capture key events, the movie itself must first be given focus from the browser. Web browsers do not automatically give keyboard focus to Flash movies, or any other embedded content.

Solution

There are two ways to give focus to the Flash movie.

  • Have the user click in the Flash movie Add a button to the first frame of the movie that the user must click in order to continue (an "Enter", "Start" or "Login" button, for example). Any subsequent key presses can be detected by Flash.
  • Use JavaScript to give focus to Flash The JavaScript focus() method can automatically give focus to the Flash movie when the HTML page containing it loads.

    Note: This method only works on Windows versions of Internet Explorer.
    1. Open the HTML page containing the Flash movie in an HTML editor (Dreamweaver, for example) or text editor.
    2. To the <BODY> tag, add an onLoad handler that gives focus to the Flash movie, as shown below:
      <body bgcolor="#FFFFFF" onLoad="window.document.movieID.focus();">
      Make sure that movieID in the onLoad handler matches the name given to the OBJECT tag used to embed the Flash movie, as shown below.
      <object classid="..." codebase="...."
          width=550 
          height=400     name="movieID">
      You can give any name to the OBJECT tag as long as it doesn't contain any spaces or other special characters.

Product versions affected

5

Last updated: October 17, 2001
Easy Link this TechNote

http://www.adobe.com/go/tn_15586

Easy Links give you a simpler URL to reference in emails, as bookmarks… anywhere.

,
TOTAL TODAY