2009-06-23 5 views
1

저는 플래시와 액션 스크립트 3에 대해 매우 새롭습니다. 저는 그것에 대해 많은 것을 읽었으며, 이것 또한 객체 지향 프로그래밍에 관한 첫 번째 aprouch입니다.AS3을 올바르게 작성하고 있습니까?

지금까지 로그인 버튼으로 애플리케이션을 만들었습니다. 그러나 나는 어떤 일이 잘못되었거나 다른 일을해야 하는지를 알고 싶다. 나는 3.

주요 액션 스크립트 파일이 Client2.as 어도비 플렉스 빌더를 사용하고 있습니다 : 그것은 로그인 인터페이스 객체를로드

package 
{ 
    //import required libraries 
    import flash.display.Sprite; 

    //set project properties 
    [SWF(width="800", height="600", frameRate="31", backgroundColor="#C0C0C0")] 

    //launch main class 
    public class Client2 extends Sprite 
    { 
     public function Client2() { //the constructor 
      trace("Client launched."); 
      var loginGui:LoginInterface = new LoginInterface(); //load the login interface object 
      loginGui.init(); //initialize the login interface (load it) 
      addChild(loginGui); //add login gui to the display tree 
     } 
    } 
} 

. 그게 좋은 일인가? 나는 그것을 올바른 방향으로하고 있는가? 무슨 약

package 
{ 
    //import required libraries 
    import flash.display.Sprite; 

    //the LoginInterface class 
    public class LoginInterface extends Sprite 
    { 
     public function LoginInterface() //the constructor 
     { 
      trace("LoginInterface object loaded."); 
     } 

     public function init():void //initialize the login interface (load it) 
     { 
      trace("LoginInterface init method was called."); 
      var loginButton:CustomButton = new CustomButton(300, 300, 100, 30, 3, 18, "Login!"); //create a new custom button 
      addChild(loginButton); //add the custom button to the display tree 
     } 
    } 
} 

:

그런 다음 LoginInterface.as 클래스 파일이있어? 다른하실 말씀 있나요? >

package 
{ 
    import flash.display.SimpleButton; 
    import flash.display.Sprite; 
    import flash.text.TextField; 
    import flash.text.TextFormat; 
    import flash.text.TextFormatAlign; 

    public class CustomButton extends Sprite 
    { 
     public function CustomButton(xLoc:int, yLoc:int, width:int, height:int, iLabelOffset:int, fontsize:uint, label:String) 
     { 
      //create new simple button instance 
      var myButton:SimpleButton = new SimpleButton(); 
      //create the look of the states 
      var normal:Sprite = new Sprite(); 
      normal.graphics.lineStyle(1, 0x000000); 
      normal.graphics.beginFill(0x6D7B8D); 
      normal.graphics.drawRect(xLoc, yLoc, width, height); 
      //the mouseover sprite 
      var over:Sprite = new Sprite(); 
      over.graphics.lineStyle(1, 0x000000); 
      over.graphics.beginFill(0x616D7E); 
      over.graphics.drawRect(xLoc, yLoc, width, height); 
      // assign the sprites 
      myButton.upState = normal; 
      myButton.downState = normal; 
      myButton.hitTestState = normal; 
      myButton.overState = over; 
      //add the button to the display tree 
      addChild(myButton); 

      //create button label 
      var tText:TextField = new TextField(); 
      tText.mouseEnabled = false, 
      tText.x = xLoc; 
      tText.y = yLoc + iLabelOffset; 
      tText.width = width; 
      tText.selectable = false 
      var Format:TextFormat = new TextFormat(); 
      Format.font = "Arial"; 
      Format.color = 0x000000; 
      Format.size = fontsize; 
      Format.bold = false; 
      Format.align = TextFormatAlign.CENTER; 
      tText.defaultTextFormat = Format; 
      tText.text = label; 
      addChild(tText) 
     } 
    } 
} 

이에 대해 언급 할 수 있나요 - 간단한 버튼의 창조 좀 더 쉽게하려면, 나는 다음 다른 클래스 파일이라고 CustomButton.as을 만들어? 나는 내가 많은 것을 잘못하고 있다고 확신한다. 아마도 객체 지향적 인 것을 얻지 못했을 것이다. 또한 클래스 선언 다음에 "extends ..."를 사용하는 방식에 대해 나쁘다는 느낌이 들었습니다. 주로 Sprite를 항상 사용하고 있기 때문에 왜 그런지 또는 무엇이 문제인지 이해하지 못하기 때문에 (문제가 생겼습니다. 인터넷에서도 알아 낸다). 내가 확신 할 수없는 또 다른 사실은 AS3의 변수 이름 지정입니다. 실제로 xLoc 또는 iLabelOffset과 같은 이름을 사용해야합니까? 적어도 내 이름을 짓는 변수에 일관성이 없다고 생각합니까?

나는이 동물에 대한 작업을 계속하기 전에 다른 사람이 나에게 AS3 코딩을 향상시켜야한다고 확신하기 때문에 누군가가 나에게 더 나은 트랙을 제공 할 수 있기를 바랍니다.

고마워요.

+0

프레임 속도를보고 좋은 31 신화 살아 있고 발로 차 있습니다! – spender

+0

FR31 신화 란 무엇입니까? 나는 그것을 찾았지만 많은 사람들이 31 세를 선호하는 것으로 나타났습니다. – invertedSpear

답변

3

내 의견 :

Client2라는 클래스는 잘못된 이름 지정 방법 일 가능성이 큽니다. Client2는 내게 많이 말하지 않습니다. 1 년 동안 당신에게 얼마나 말할 것입니까?

CustomButton에서는 초기화가 생성자에서 처리됩니다. LoginInterface에서 클래스 인스턴스를 사용하려면 init()에 대한 명시 적 호출이 필요합니다. 잊기 쉽고 불필요합니다. 그렇지 않은 경우가 아니라면 생성자에서 init을 호출하십시오.

iLabelOffset은 무엇을 의미합니까? 매개 변수 목록에 혼동을 덜 일으키는 이름을 사용하는 것이 좋습니다.

CustomButton 생성자의 매개 변수 목록은 꽤 길다. x와 y를 전달할 필요는 없습니다. Sprite는 x와 y 속성을 이미 가지고 있으므로, 모든 것을 0 오프셋으로 되돌리고 생성 된 CustomButton의 x 및 y 속성을 조작합니다.

CustomButton 생성자에 대한 나머지 매개 변수 중 매개 변수 목록의 끝에 갈 수있는 기본 매개 변수를 제공 할 수 있도록 매개 변수를 재정렬하는 것이 좋습니다. labelOffset 및 fontSize는 좋은 후보자처럼 보입니다.

반복 코드를 제거하여 기능 크기를 작게 유지하십시오. 버튼 상태를 만드는 함수를 만듭니다. 매개 변수에서 색상을 사용하는 스프라이트 (또는이 기능을 새로운 유형의 Sprite 파생 클래스로 옮기는 것이 더 좋음)를 만들고 createLabel 함수를 추가하여 해당 코드를 건설자.함수 크기를 작게 유지하려고하면 코드를 읽고 유지하기가 더 쉬워집니다. 그것은 또한 당신이 적은 코멘트를 써야한다는 것을 의미합니다 .-)

2

스펜더가 머리에 못을 박았습니다. 그것들은 분명히 코드를 살펴볼 때 제기되는 문제입니다. 그가 언급 한 것들은 네스 케어 적으로 Actionscript 이슈가 아닙니다. (옳은 말은 아닙니다. 아마도 "주목할 부분"입니다.) 이것은 모든 프로그래밍 언어에 일반적인 문제입니다. 예를 들어 설명적인 명명이 매우 중요합니다.

프로그래밍 측면에 초점을 맞춘 책은 많지 않으며 그 중 일부는 잘합니다. 당신이 너무하지 않으려는 경우에도 :

Code Complete

The pragmatic programmer

를 내가보기 엔 당신이이 지역에 더 성장하려면 다음 두 권의 책을 따기 추천, (나는 그것을 추천 해드립니다 것 모든 프로그래머가 읽어야하는 두 책이 있으므로 확인하십시오. 당신은 Actionscript의 관점에서 볼 때 코드가 좋지만, 그저 구문 일뿐입니다. 실제로 코드를 작성하지 않으면 이러한 기술이 발전하지 않을 것이라는 점에 유의해야합니다. 따라서 "짐승에 계속해서 작업하십시오" "나머지는 적합합니다.

0

스타일의 문제와 마찬가지로, 나는 변수를 생성자 외부에 선언하고 싶다. 대중과 개인 또는 범위에 대해 어떤 놀라움을 겪지 않을 것이라고 생각하는 데 도움이됩니다. 또한 가독성을 향상시킬 수있는 공백이 추가되었습니다.

public class CustomButton extends Sprite 
{ 
    private var myButton:SimpleButton; 
    private var normal:Sprite; 
    private var over:Sprite; 
    // etc ... 

    public function CustomButton(xLoc:int, yLoc:int, width:int, height:int, iLabelOffset:int, fontsize:uint, label:String) 
    { 
      //create new simple button instance 
      myButton = new SimpleButton(); 

      //create the look of the states 
      normal = new Sprite(); 
      normal.graphics.lineStyle(1, 0x000000); 
      normal.graphics.beginFill(0x6D7B8D); 
      normal.graphics.drawRect(xLoc, yLoc, width, height); 

      //the mouseover sprite 
      over = new Sprite(); 
      over.graphics.lineStyle(1, 0x000000); 
      over.graphics.beginFill(0x616D7E); 
      over.graphics.drawRect(xLoc, yLoc, width, height); 

      // etc ... 
관련 문제