2012-03-29 4 views
0

메인 스테이지가 있고 두 개의 객체 (블록)가 있는데이 두 객체는 ​​모두 "블록"클래스에서 확장됩니다. "블록"클래스는 메인 클래스에서 확장되지 않습니다.다른 확장 클래스에서 함수 호출하기

메인 스테이지 클래스에서 "블록"클래스 나 서브 클래스에있는 함수를 호출하고 싶습니다. 함수는 함수를 호출하는 객체에 따라 약간 다른 작업을 수행합니다 (배열에 다른 항목과 다른 수를 추가 함). 이것을 구현하는 가장 좋은 방법은 무엇입니까?

죄송합니다 지금 당장 표시 할 코드가 없습니다. 지금 앉아서 시도하고 있지만 매우 실종 상태입니다.

+0

두 블록 모두 동일한 기능을 가져야하며 동일한 기능을 확장하더라도 함수가 어떤 블록을 호출하는지에 따라 두 가지 다른 작업을 수행해야한다는 것을 의미합니까? – Marty

답변

0

나는 이것이 당신이 이것을 의미한다고 가정 할 것입니다.

당신은 당신이 블록의 두 가지를 만들 가능성이 귀하의 기본 클래스에서 배열에 저장할 블록

라는 클래스가 있습니다. 당신의 블록 클래스 이제

//stage base class 
var blockArray:Array = new Array() 

private function createBlocks():void{ 

    var blockOne:Block = new Block(1); //passing in an int to block, could be anything but this 
             // will be used to do slightly different things 

    var blockTwo:Block = new Block(2); 
    blockArray.push(blockOne...blockTwo) 
} 

이제 다시 메인 클래스

//stage base class 
private function doSomethingToBlocks():void{ 
    //lets call doSomething on each block 
    Block(blockArray[0]).doSomething() //this will trace 1 because we passed that into the block in our array slot 0 
    Block(blockArray[1]).doSomething() //this will trace 2 
} 

을에

//block class 
class Block{ 
    var somethingDifferent:int; //this is where we will store the int you pass in when the blocks are made 
    public function Block(aInt:int){ 
     somethingDifferent = aInt //grabbing the int 
    } 

    public function doSomething():void{ 
     trace(somethingDifferent); //will trace out the number passed 
    } 

} 

희망이 당신이

+0

전체 자격 둘 다 고마워. –

0

일반적인 생각이 정의하는 것입니다 후있어 무엇인가 부모 클래스의 함수를 호출 한 다음 하위 클래스의 함수를 재정 의하여 다른 작업을 수행합니다. 그런 다음 여러 하위 클래스에서 함수를 호출 할 수 있으며 블록에 따라 다른 작업을 수행합니다.

간략한 예 :

블록 분류 :

public function getBlockType():String 
{ 
    return "I am a plain block"; 
} 

번째 블록의 서브

public override function getBlockType():String 
{ 
    return "I am a cool block"; 
} 

번째 블록의 서브 클래스

public override function getBlockType():String 
{ 
    return "I am an even cooler block"; 
} 

단계 :

//add the first block 
var coolBlock:CoolBlock = new CoolBlock(); 
addChild(coolBlock); 

//add the second block 
var coolerBlock:EvenCoolerBlock = new EvenCoolerBlock(); 
addChild(coolerBlock); 

//call the functions 
trace(coolBlock.getBlockType());//outputs "I am a cool block" 
trace(coolerBlock.getBlockType());//outputs "I am an even cooler block" 
+0

지부입니다. 둘 다 고마워. –

관련 문제