2011-11-30 3 views
0

나는 게임 중입니다. 나는 플래시의 공정 줄을 설계하고 내가 무대에 var에 할당하고 기본 클래스 (main_c.as)에서 3. AS에 연결 한 :addChild가 예외를 throw합니다.

package { 

import flash.display.MovieClip; 
import flash.display.Stage; 


public class main_c extends MovieClip { 

    static public var stageRef:Stage; 
    public var s:start_b; 
    public var bar:timer_bar; 
    public function main_c() 
    { 
     // constructor code 
     stageRef = stage; 
     s = new start_b(); 
     addChild(s); 
     s.x = 260; 
     s.y = 225; 

    } 


} 

} 

다음에있는 start_b 클래스가 버튼을 만들고 클릭하여 세 번째 클래스 (game.as)의 생성자를 시작합니다. 여기 start_b의 코드는 다음과 같습니다

package { 

import flash.display.SimpleButton; 
import flash.events.MouseEvent; 

public class start_b extends SimpleButton { 

    public var g:game; 

    public function start_b() 
    { 
     // constructor code 
     this.addEventListener(MouseEvent.CLICK, start_g); 
    } 

    public function start_g(e:MouseEvent):void 
    { 
     g = new game(); 
     this.removeEventListener(MouseEvent.CLICK, start_g); 
     this.visible = false; 
    } 
} 

그리고

지난 시간 나는 무대를 기준으로 상태 표시 줄을하면 addChild 싶지만 실행할 때 오류 얻을에서 - 여기

TypeError: Error #1009: Cannot access a property or method of a null object reference. at game() at start_b/start_g()

을 당신이 나를 도울 수 있다면

package{ 

import flash.display.MovieClip; 
import flash.utils.Timer; 
import flash.events.TimerEvent; 
import main_c; 

public class game extends MovieClip { 
    public var points:Number; 
    public var ptw:Number; 
    public var time:Timer; 
    public var bar:timer_bar = new timer_bar(); 
    public var cnt:main_c; 

    public function game() 
    { 
     //restartirane na igrata (nulirane) 
     main_c.stageRef.addChild(bar); 
     points = 0; 
     time = new Timer(50); 
     time.addEventListener(TimerEvent.TIMER, flow); 
     time.start(); 
     trace("d"); 


    } 

    public function flow(t:TimerEvent):void 
    { 
     //code 
     //bar.y++; 
    } 

    public function addPoints():void 
    { 
     //function code here 
    } 

    public function removePoints():void 
    { 
     //function code here 
    } 

    public function checkTime():void 
    { 
     //function code here 
    } 

    public function end():void 
    { 
     //function code here 
    } 

} 

} 

나는 감사하고 좋은 하루 :-) 매우 기쁠 것 : 세 번째 클래스의 코드 (game.as)입니다! 당신의 무대가 준비 경우

+0

다른 사람이 솔루션을 작성해 주시면 감사하겠습니다. – Mariyan

+2

좋은 답변이 있지만 이미 좋은 코딩 방법에 대한 메모가 몇 개 있습니다. 1) 클래스 이름은 대문자로하고 UpperCamelCase를 사용해야합니다 표준 플래시 라이브러리 클래스의 이름은 ex : MovieClip입니다. 그러므로'timer_bar' 클래스의 이름은'TimerBar'이어야하고'main_c' 클래스의 이름은'MainC' 등이되어야합니다. 2) 일반적으로 단일 문자보다 사람이 읽을 수있는 이름을 사용하는 것이 바람직합니다 . 's = new start_b();''startBtn = new StartButton();'으로 훨씬 더 읽기 쉽습니다.어쨌든, 그냥 몇 가지 포인터 :) – Ian

답변

0

당신은 확인해야합니다

Main_c/생성자 :

public function main_c() 
    { 
    if (stage) 
    { 
     init(); 
    } 
    else 
    { 
     addEventListener(Event.ADDED_TO_STAGE, init); 
    } 
    } 

Main_c/초기화 :

private function init(e:Event = null):void 
{ 
    removeEventListener(Event.ADDED_TO_STAGE, init); 

    stageRef = stage; 
    s = new start_b(); 
    addChild(s); 
    s.x = 260; 
    s.y = 225; 
} 
+0

고마워,이 작품 ;-) – Mariyan

+0

Wellcome :) .. 그리고 항상 귀하의 질문에 대한 정답 이었다면 받아 들일 수있는 답변을 표시하고 다른 좋은 대답, 당신은 그들에게 투표를 제공 할 수 있습니다. 그리고 btw Ian은 의견에 대해 좋은 조언을하고 있습니다. – Jarno

0

단서 # 1 :

TypeError: Error #1009: Cannot access a property or method of a null object reference. at game() at start_b/start_g()

이것은 일부 obj game 생성자의 ect가 null이지만 해당 객체의 멤버 함수 나 속성에 액세스하려고합니다.

main_c.stageRef.addChild(bar); 
points = 0; 
time = new Timer(50); 
time.addEventListener(TimerEvent.TIMER, flow); 
time.start(); 
trace("d"); 

여기 오류의 유일한 원인은

main_c.stageRef.addChild(bar); 

그래서,이에 대한 해결책이 main_c.stageRef가 null인지 확인하는 것입니다 첫 번째 줄

, 그리고 :

하면 헤어지 그에 따라 행동하십시오

내 솔루션 : 게임 클래스의 생성자를 다시 정의하십시오.

관련이없는 참고 켜기 callLater 방법

10

public function game() { 
    init(); 
} 

public function init() { 
    if(main_c.stageRef) { 
     //restartirane na igrata (nulirane) 
     main_c.stageRef.addChild(bar); 
     points = 0; 
     time = new Timer(50); 
     time.addEventListener(TimerEvent.TIMER, flow); 
     time.start(); 
     trace("d"); 
    } else { 
     callLater(init); 
    } 
} 

문서는 ActionScript 클래스 이름은 규칙에 의한 대문자로 시작합니다. 이렇게하면 소문자로 시작하는 인스턴스 이름과 구별 할 수 있습니다.

+0

고마워, 나는 그것을 염두에두고있을거야 :-) 너 정말 날 도와 줘, 좋은 하루 보내! – Mariyan

관련 문제